ImaGIN_spm_eeg_downsample.m
3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function D = ImaGIN_spm_eeg_downsample(S)
% function used for down-sampling EEG/MEG data
% FORMAT D = spm_eeg_downsample(S)
%
% S - optional input struct
% (optional) fields of S:
% D - filename of EEG mat-file
% Radc_new - new sampling rate
%_______________________________________________________________________
% Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
% Stefan Kiebel
% $Id: spm_eeg_downsample.m 805 2007-04-27 11:26:53Z vladimir $
[Finter,Fgraph,CmdLine] = spm('FnUIsetup','EEG downsample setup',0);
try
DD = S.D;
catch
DD = spm_select(inf, 'mat', 'Select EEG mat file');
end
for i1=1:size(DD,1)
D=deblank(DD(i1,:));
P = spm_str_manip(D, 'H');
try
D = spm_eeg_load(D);
catch
error(sprintf('Trouble reading file %s', D));
end
try
Radc_new = S.Radc_new;
catch
str = 'New sampling rate';
YPos = -1;
while 1
if YPos == -1
YPos = '+1';
end
[Radc_new, YPos] = spm_input(str, YPos, 'r');
if Radc_new < D.fsample, break, end
str = sprintf('Sampling rate must be less than original (%d)', round(D.fsample));
end
S.Radc_new=Radc_new;
end
spm('Pointer', 'Watch');drawnow;
% Prepare for writing data
% treat continuous and epoched data differently because of different scaling method
if size(D, 3) > 1
% epoched
spm_progress_bar('Init', D.ntrials, 'Trials downsampled'); drawnow;
if D.ntrials > 100, Ibar = floor(linspace(1, D.ntrials,100));
else, Ibar = 1:D.ntrials; end
nsampl=length(resample(squeeze(D(1, :, 1)), Radc_new, round(D.fsample)));
try
Dnew = clone(D, S.FileOut, [D.nchannels nsampl, D.ntrials]);
catch
Dnew = clone(D, ['d' fname(D)], [D.nchannels nsampl, D.ntrials]);
end
for i2 = 1:D.ntrials
for i3=1:D.nchannels
d = squeeze(D(i3, :, i2));
d2 = resample(d, Radc_new,round(D.fsample));
Dnew(i3,:,i2)=(mean(abs(d))/mean(abs(d2)))*d2;
if ismember(i2, Ibar)
spm_progress_bar('Set', i2); drawnow;
end
end
end
else
% continuous
% adjust the timing information
try
Events=D.events;
Events.time = round(Events.time*Radc_new/D.fsample);
end
spm_progress_bar('Init', D.nchannels, 'Channels downsampled'); drawnow;
if D.nchannels > 100, Ibar = floor(linspace(1, D.nchannels, 100));
else, Ibar = 1:D.nchannels; end
nsampl=length(resample(squeeze(D(1,:,:)), Radc_new, round(D.fsample)));
try
Dnew = clone(D, S.FileOut, [D.nchannels nsampl, 1]);
catch
Dnew = clone(D, ['d' fname(D)], [D.nchannels nsampl, 1]);
end
for i = 1:D.nchannels
d = squeeze(D(i, :, :));
d2 = resample(d, Radc_new, round(D.fsample));
Dnew(i,:,1)=(mean(abs(d))/mean(abs(d2)))*d2;
if ismember(i, Ibar)
spm_progress_bar('Set', i); drawnow;
end
end
end
spm_progress_bar('Clear');
Dnew=fsample(Dnew,Radc_new);
save(Dnew);
end
spm('Pointer', 'Arrow');