-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathchapter14.m
523 lines (397 loc) · 17.7 KB
/
chapter14.m
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
%% Analyzing Neural Time Series Data
% Matlab code for Chapter 14
% Mike X Cohen
%
% This code accompanies the book, titled "Analyzing Neural Time Series Data"
% (MIT Press). Using the code without following the book may lead to confusion,
% incorrect data analyses, and misinterpretations of results.
% Mike X Cohen assumes no responsibility for inappropriate or incorrect use of this code.
%% Figure 14.1
% create cosine
time = 0:.001:1;
cosine = cos(2*pi*5*time);
figure
plot(time,cosine)
hold on
plot(time(1:5:end),real(hilbert(cosine(1:5:end))),'ko') % plot every 5th point because of overlap
plot(time,imag(hilbert(cosine)),'r')
plot(time,angle(hilbert(cosine)),'m')
ylabel('Angle or amplitude')
legend({'cosine';'real part';'imag part';'angle'})
%% the FFT-based hilbert transform
% generate random numbers
n = 21;
randomnumbers = randn(n,1);
% take FFT
f = fft(randomnumbers);
% create a copy that is multiplied by the complex operator
complexf = 1i*f;
% find indices of positive and negative frequencies
posF = 2:floor(n/2)+mod(n,2);
negF = ceil(n/2)+1+~mod(n,2):n;
% rotate Fourier coefficients
% (note 1: this works by computing the iAsin(2pft) component, i.e., the phase quadrature)
% (note 2: positive frequencies are rotated counter-clockwise; negative frequencies are rotated clockwise)
f(posF) = f(posF) + -1i*complexf(posF);
f(negF) = f(negF) + 1i*complexf(negF);
% The next two lines are an alternative and slightly faster method.
% The book explains why this is equivalent to the previous two lines.
% f(posF) = f(posF)*2;
% f(negF) = f(negF)*0;
% take inverse FFT
hilbertx = ifft(f);
% compare with Matlab function hilbert
hilbertm = hilbert(randomnumbers);
% plot results
figure
subplot(211)
plot(abs(hilbertm))
hold on
plot(abs(hilbertx),'ro')
legend({'Matlab Hilbert function';'"manual" Hilbert'})
title('magnitude of Hilbert transform')
subplot(212)
plot(angle(hilbertm))
hold on
plot(angle(hilbertx),'ro')
legend({'Matlab Hilbert function';'"manual" Hilbert'})
title('phase of Hilbert transform')
%% Figure 14.2
load sampleEEGdata
% first, filter data (filter mechanisms will be explained more below; for now, focus on
% using the phases from the Hilbert transform to test whether the matrix input was correct)
nyquist = EEG.srate/2;
lower_filter_bound = 4; % Hz
upper_filter_bound = 10; % Hz
transition_width = 0.2;
filter_order = round(3*(EEG.srate/lower_filter_bound));
% create the filter shape (this is explained more in the text around figure 14.4)
ffrequencies = [ 0 (1-transition_width)*lower_filter_bound lower_filter_bound upper_filter_bound (1+transition_width)*upper_filter_bound nyquist ]/nyquist;
idealresponse = [ 0 0 1 1 0 0 ];
filterweights = firls(filter_order,ffrequencies,idealresponse);
% apply the filter kernal to the data to obtain the band-pass filtered signal
filtered_data = zeros(EEG.nbchan,EEG.pnts);
for chani=1:EEG.nbchan
filtered_data(chani,:) = filtfilt(filterweights,1,double(EEG.data(chani,:,1)));
end
% apply hilbert transform in correct and incorrect orientations
hilbert_oops = hilbert(filtered_data);
hilbert_yes = hilbert(filtered_data')'; % time should be in the first dimension.
% Note that the output of the hilbert transform is transposed to bring us back to an electrode X time matrix.
figure
subplot(221)
plot(EEG.times,angle(hilbert_yes(1,:)'),'b');
title('correct matrix orientation')
xlabel('Time (ms)'), ylabel('Phase angle (rad.)')
set(gca,'xlim',[-1000 1500])
subplot(222)
plot(EEG.times,angle(hilbert_oops(1,:)),'r');
title('incorrect matrix orientation')
xlabel('Time (ms)'), ylabel('Phase angle (rad.)')
set(gca,'xlim',[-1000 1500])
subplot(223)
plot(EEG.times,real(hilbert_yes(1,:)),'b');
title('correct matrix orientation')
xlabel('Time (ms)'), ylabel('Amplitude')
set(gca,'xlim',[-1000 1500])
subplot(224)
plot(EEG.times,real(hilbert_oops(1,:)),'r');
title('incorrect matrix orientation')
xlabel('Time (ms)'), ylabel('Amplitude')
set(gca,'xlim',[-1000 1500])
%% Figure 14.3
center_freq = 20; % in Hz
filter_frequency_spread = 6; % Hz +/- the center frequency
wavelet_frequency_spread = 4; % number of wavelet cycles
% create wavelet...
time = -1000/EEG.srate/10:1/EEG.srate:1000/EEG.srate/10;
wavelet = zscore(exp(2*1i*pi*center_freq.*time) .* exp(-time.^2./(2*(wavelet_frequency_spread/(2*pi*center_freq))^2)));
% ... and compute its power spectrum
fft_wavelet = abs(fft(wavelet));
fft_wavelet = fft_wavelet./max(fft_wavelet); % normalized to 1.0 for visual comparison ease
hz_wavelet = linspace(0,nyquist,length(time)/2+1);
% construct filter kernel (the mechanics of filter construction will be
% discussed in the text around figure 14.4)
transition_width = 0.2;
ffrequencies = [ 0 (1-transition_width)*(center_freq-filter_frequency_spread) (center_freq-filter_frequency_spread) (center_freq+filter_frequency_spread) (1+transition_width)*(center_freq+filter_frequency_spread) nyquist ]/nyquist;
idealresponse = [ 0 0 1 1 0 0 ];
filterweights = zscore(firls(200,ffrequencies,idealresponse));
% also compute weights using fir1
filterweights1 = zscore(fir1(200,[center_freq-filter_frequency_spread center_freq+filter_frequency_spread]./nyquist));
% compute its power spectrum
fft_filtkern = abs(fft(filterweights));
fft_filtkern = fft_filtkern./max(fft_filtkern); % normalized to 1.0 for visual comparison ease
fft_filtkern1 = abs(fft(filterweights1));
fft_filtkern1 = fft_filtkern1./max(fft_filtkern1); % normalized to 1.0 for visual comparison ease
hz_filtkern = linspace(0,nyquist,101); % list of frequencies in Hz corresponding to filter kernel
figure
subplot(211)
% plot wavelet and filter kernel
plot(real(wavelet))
hold on
plot(filterweights,'r')
legend({'wavelet';'filter kernel'})
set(gca,'xlim',[0 200],'ylim',[-5 5])
xlabel('Time')
% plot power spectra
subplot(212)
plot(hz_wavelet,fft_wavelet(1:ceil(length(fft_wavelet)/2)))
hold on
plot(hz_filtkern,fft_filtkern(1:ceil(length(fft_filtkern)/2)),'r')
legend({'wavelet';'filter kernel'})
set(gca,'ylim',[-.1 1.1])
xlabel('Frequency (Hz)')
%% Figure 14.4
center_freq = 20; % in Hz
filter_frequency_spread_wide = 10; % Hz +/- the center frequency
ffrequencies = [ 0 (1-transition_width)*(center_freq-filter_frequency_spread_wide) (center_freq-filter_frequency_spread_wide) (center_freq+filter_frequency_spread_wide) (1+transition_width)*(center_freq+filter_frequency_spread_wide) nyquist ]/nyquist;
idealresponse = [ 0 0 1 1 0 0 ];
filterweightsW = zscore(firls(200,ffrequencies,idealresponse));
figure
plot(ffrequencies*nyquist,idealresponse,'r')
hold on
fft_filtkern = abs(fft(filterweightsW));
fft_filtkern = fft_filtkern./max(fft_filtkern); % normalized to 1.0 for visual comparison ease
plot(hz_filtkern,fft_filtkern(1:ceil(length(fft_filtkern)/2)),'b')
set(gca,'ylim',[-.1 1.1],'xlim',[0 nyquist])
legend({'ideal';'best fit'})
%% Figure 14.5
center_freq = 20; % in Hz
filter_frequency_spread_wide = 10; % Hz +/- the center frequency
filter_frequency_spread_naro = 2; % Hz +/- the center frequency
% construct filter kernels
ffrequencies = [ 0 (1-transition_width)*(center_freq-filter_frequency_spread_wide) (center_freq-filter_frequency_spread_wide) (center_freq+filter_frequency_spread_wide) (1+transition_width)*(center_freq+filter_frequency_spread_wide) nyquist ]/nyquist;
idealresponse = [ 0 0 1 1 0 0 ];
filterweightsW = zscore(firls(200,ffrequencies,idealresponse));
ffrequencies = [ 0 (1-transition_width)*(center_freq-filter_frequency_spread_naro) (center_freq-filter_frequency_spread_naro) (center_freq+filter_frequency_spread_naro) (1+transition_width)*(center_freq+filter_frequency_spread_naro) nyquist ]/nyquist;
filterweightsN = zscore(firls(200,ffrequencies,idealresponse));
figure
subplot(211)
fft_filtkern = abs(fft(filterweightsW));
fft_filtkern = fft_filtkern./max(fft_filtkern); % normalized to 1.0 for visual comparison ease
plot(hz_filtkern,fft_filtkern(1:ceil(length(fft_filtkern)/2)))
hold on
fft_filtkern = abs(fft(filterweightsN));
fft_filtkern = fft_filtkern./max(fft_filtkern); % normalized to 1.0 for visual comparison ease
plot(hz_filtkern,fft_filtkern(1:ceil(length(fft_filtkern)/2)),'r')
set(gca,'ylim',[-.1 1.1])
legend({'10 Hz width';'2 Hz width'})
subplot(223)
plot(filterweightsW)
hold on
plot(filterweightsN,'r')
set(gca,'xlim',[0 200],'ylim',[-4 7])
legend({'10 Hz spread';'2 Hz spread'})
xlabel('Time')
subplot(224)
plot(abs(hilbert(filterweightsW)),'b')
hold on
plot(abs(hilbert(filterweightsN)),'r')
set(gca,'xlim',[0 200],'ylim',[-4 7])
legend({'10 Hz spread';'2 Hz spread'})
xlabel('Time')
%% Figure 14.6
figure
subplot(211)
% plot wavelet and filter kernel
plot(filterweights1)
hold on
plot(filterweights,'r')
legend({'fir1';'firls'})
set(gca,'xlim',[0 200])
xlabel('Time')
% plot power spectra
subplot(212)
plot(hz_filtkern,fft_filtkern1(1:ceil(length(fft_filtkern1)/2)))
hold on
plot(hz_filtkern,fft_filtkern(1:ceil(length(fft_filtkern)/2)),'r')
legend({'fir1';'firls'})
set(gca,'ylim',[-.1 1.1])
xlabel('Frequency (Hz)')
% This next figure shows that the filter kernel computed by fir1 is the same as
% that produced by firls if you set the transition zone to zero and then
% smooth the filter kernel with a Hamming window. In the plot, the red and magenta
% lines overlap, which is why you don't see the fir1 filter kernel. You can
% subtract them and show that the difference (which is due to re-scaling
% of the kernel after windowing) is 3-4 orders of magnitude smaller than
% the kernel itself, hence, nearly identical.
freqL = center_freq-filter_frequency_spread;
freqU = center_freq+filter_frequency_spread;
ffrequencies = [ 0 freqL freqL freqU freqU nyquist ]/nyquist; % transition zone of 0
filterweights = firls(200,ffrequencies,idealresponse);
filterweights1 = fir1(200,[freqL freqU]./nyquist);
figure
subplot(211)
plot(filterweights)
hold on
plot(filterweights1,'r.')
plot(filterweights.*hamming(length(filterweights))','m')
legend({'firls';'fir1';'firls with hamming window'})
%% Figure 14.7a
center_freq = 60; % in Hz
filter_frequency_spread_wide = 10; % Hz +/- the center frequency
ffrequencies = [ 0 (1-transition_width)*(center_freq-filter_frequency_spread_wide) (center_freq-filter_frequency_spread_wide) (center_freq+filter_frequency_spread_wide) (1+transition_width)*(center_freq+filter_frequency_spread_wide) nyquist ]/nyquist;
idealresponse = [ 0 0 1 1 0 0 ];
filterweightsW = zscore(firls(200,ffrequencies,idealresponse));
figure
plot(ffrequencies*nyquist,idealresponse,'r')
hold on
fft_filtkern = abs(fft(filterweightsW));
fft_filtkern = fft_filtkern./max(fft_filtkern); % normalized to 1.0 for visual comparison ease
plot(hz_filtkern,fft_filtkern(1:ceil(length(fft_filtkern)/2)),'b')
set(gca,'ylim',[-.1 1.1],'xlim',[0 nyquist])
legend({'ideal';'best fit'})
freqsidx = dsearchn(hz_filtkern',ffrequencies'*nyquist);
title([ 'SSE: ' num2str(sum( (idealresponse-fft_filtkern(freqsidx)).^2 )) ])
center_freq = 60; % in Hz
filter_frequency_spread_wide = 15; % Hz +/- the center frequency
ffrequencies = [ 0 (1-transition_width)*(center_freq-filter_frequency_spread_wide) (center_freq-filter_frequency_spread_wide) (center_freq+filter_frequency_spread_wide) (1+transition_width)*(center_freq+filter_frequency_spread_wide) nyquist ]/nyquist;
idealresponse = [ 0 0 1 1 0 0 ];
filterweightsW = zscore(firls(200,ffrequencies,idealresponse));
figure
plot(ffrequencies*nyquist,idealresponse,'r')
hold on
fft_filtkern = abs(fft(filterweightsW));
fft_filtkern = fft_filtkern./max(fft_filtkern); % normalized to 1.0 for visual comparison ease
plot(hz_filtkern,fft_filtkern(1:ceil(length(fft_filtkern)/2)),'b')
set(gca,'ylim',[-.1 1.1],'xlim',[0 nyquist])
legend({'ideal';'best fit'})
freqsidx = dsearchn(hz_filtkern',ffrequencies'*nyquist);
title([ 'SSE: ' num2str(sum( (idealresponse-fft_filtkern(freqsidx)).^2 )) ])
%% Figure 14.7b (note: takes a while to run...)
centerfreqs = linspace(5,80,60);
transwidths = linspace(0.01,0.2,40);
filterwidths = linspace(0.05,0.3,40);
sse = zeros(length(centerfreqs),length(transwidths));
for centfreqi = 1:length(centerfreqs)
center_freq = centerfreqs(centfreqi);
for transwidi = 1:length(transwidths)
filter_frequency_spread_wide = center_freq*.2;
transition_width = transwidths(transwidi);
ffrequencies = [ 0 (1-transition_width)*(center_freq-filter_frequency_spread_wide) (center_freq-filter_frequency_spread_wide) (center_freq+filter_frequency_spread_wide) (1+transition_width)*(center_freq+filter_frequency_spread_wide) nyquist ]/nyquist;
filterweightsW = zscore(firls(200,ffrequencies,idealresponse));
fft_filtkern = abs(fft(filterweightsW));
fft_filtkern = fft_filtkern./max(fft_filtkern); % normalized to 1.0 for visual comparison ease
freqsidx = dsearchn(hz_filtkern',ffrequencies'*nyquist);
sse(centfreqi,transwidi) = sum( (idealresponse-fft_filtkern(freqsidx)).^2 );
end
end
figure
contourf(transwidths,centerfreqs,sse,40,'linecolor','none')
xlabel('transwidths'), ylabel('center frequencies')
set(gca,'clim',[0 1]), colorbar
sse = zeros(length(centerfreqs),length(transwidths));
for centfreqi = 1:length(centerfreqs)
center_freq = centerfreqs(centfreqi);
for transwidi = 1:length(transwidths)
filter_frequency_spread_wide = center_freq*filterwidths(transwidi);
transition_width = .2;
ffrequencies = [ 0 (1-transition_width)*(center_freq-filter_frequency_spread_wide) (center_freq-filter_frequency_spread_wide) (center_freq+filter_frequency_spread_wide) (1+transition_width)*(center_freq+filter_frequency_spread_wide) nyquist ]/nyquist;
filterweightsW = zscore(firls(200,ffrequencies,idealresponse));
fft_filtkern = abs(fft(filterweightsW));
fft_filtkern = fft_filtkern./max(fft_filtkern); % normalized to 1.0 for visual comparison ease
freqsidx = dsearchn(hz_filtkern',ffrequencies'*nyquist);
sse(centfreqi,transwidi) = sum( (idealresponse-fft_filtkern(freqsidx)).^2 );
end
end
figure
contourf(filterwidths,centerfreqs,sse,40,'linecolor','none')
xlabel('filterwidths (% center freq.)'), ylabel('center frequencies')
set(gca,'clim',[0 1]), colorbar
%% Figure 14.8
center_freq = 10;
freqspread = 4; % Hz +/- the center frequency
transwid = .10;
ffrequencies = [ 0 (1-transwid)*(center_freq-freqspread) (center_freq-freqspread) (center_freq+freqspread) (1+transwid)*(center_freq+freqspread) nyquist ]/nyquist;
data2filter = squeeze(double(EEG.data(47,:,1)));
filterweights = firls(200,ffrequencies,idealresponse); % recompute without z-scoring
filter_result = filtfilt(filterweights,1,data2filter);
convol_result = conv(data2filter,filterweights,'same'); % could also use ifft(fft(data2filter...
figure
plot(EEG.times,filter_result)
hold on
plot(EEG.times,convol_result,'r')
set(gca,'xlim',[-200 1000]) % zoom-in
xlabel('Time (ms)'), ylabel('Amplitude (\muV)');
legend({'filtfilt';'conv'})
%% Figure 14.9
center_freq = 10; % in Hz
nyquist = EEG.srate/2;
% create short sine wave
time = -2000/EEG.srate/10:1/EEG.srate:2000/EEG.srate/10;
wavelet = cos(2*pi*center_freq.*time) .* exp(-time.^2./(2*(3/(2*pi*center_freq))^2));
freqspread = 4; % Hz +/- the center frequency
transwid = .10;
% construct filter kernels
ffrequencies = [ 0 (1-transwid)*(center_freq-freqspread) (center_freq-freqspread) (center_freq+freqspread) (1+transwid)*(center_freq+freqspread) nyquist ]/nyquist;
idealresponse = [ 0 0 1 1 0 0 ];
filterweights = firls(250,ffrequencies,idealresponse);
forward_filt = filter(filterweights,1,wavelet);
reverse_filt = filter(filterweights,1,forward_filt(end:-1:1));
final_filt_result = reverse_filt(end:-1:1); % must reverse time again!
figure
plot(wavelet)
hold on
plot(forward_filt,'r')
plot(reverse_filt,'m')
set(gca,'xlim',[0 length(wavelet)])
legend({'signal';'forward filter';'reverse filter'})
%% Figure 14.10
% 5th-order butterworth filter
[butterB,butterA] = butter(5,[(center_freq-filter_frequency_spread)/nyquist (center_freq+filter_frequency_spread)/nyquist],'bandpass');
butter_filter = filtfilt(butterB,butterA,data2filter);
figure
subplot(211)
% plot real part of the filtered signal
plot(EEG.times,filter_result)
hold on
plot(EEG.times,butter_filter,'r')
set(gca,'xlim',[-200 1000])
xlabel('Time (ms)'), ylabel('Amplitude (\muV)');
legend({'FIR';'Butterworth'})
% now plot phases
subplot(212)
plot(EEG.times,angle(hilbert(filter_result)))
hold on
plot(EEG.times,angle(hilbert(butter_filter)),'r')
set(gca,'xlim',[-200 1000])
xlabel('Time (ms)'), ylabel('Phase angles (rad.)');
legend({'FIR';'Butterworth'})
%% Figure 14.12
elap_time = [0 0];
num_iter = 100;
freqspread = 4; % Hz +/- the center frequency
center_freq = 20;
transwid = .15;
% construct filter kernels
ffrequencies = [ 0 (1-transwid)*(center_freq-freqspread) (center_freq-freqspread) (center_freq+freqspread) (1+transwid)*(center_freq+freqspread) nyquist ]/nyquist;
idealresponse = [ 0 0 1 1 0 0 ];
filterweights = firls(3*round(EEG.srate/(center_freq-freqspread)),ffrequencies,idealresponse);
for i=1:num_iter
tic
data2filter_cat = squeeze(double(reshape(EEG.data(47,:,:),1,EEG.pnts*EEG.trials)));
filtdat_cat = reshape(filtfilt(filterweights,1,data2filter_cat),EEG.pnts,EEG.trials);
elap_time(1) = elap_time(1) + toc;
end
for i=1:num_iter
tic
data2filter_sep = squeeze(double(EEG.data(47,:,:)));
filtdat_sep = zeros(size(data2filter_sep));
for triali=1:EEG.trials
filtdat_sep(:,triali) = filtfilt(filterweights,1,data2filter_sep(:,triali));
end
elap_time(2) = elap_time(2) + toc;
end
elap_time = elap_time/num_iter;
% plot
figure
plot(EEG.times,mean(filtdat_cat,2))
hold on
plot(EEG.times,mean(filtdat_sep,2),'r')
legend({'concatenated';'separated'})
figure
bar(elap_time)
set(gca,'xlim',[0 3],'xticklabel',{'Concatenated';'Separated'})
ylabel('Time (s)')
title([ 'Speed increase of ' num2str(elap_time(2)/elap_time(1)) '!' ])
%% end.