-
Notifications
You must be signed in to change notification settings - Fork 0
/
rd_simTAMEG.m
140 lines (111 loc) · 3.21 KB
/
rd_simTAMEG.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
% rd_simTAMEG.m
%% setup
sinewave = @(A,f,t,ph) A*sin(2*pi*f*t + ph);
Fs = 1000;
dur = 5;
ssvefFreq = 20;
ssvefAmp = 1;
Fbp = ssvefFreq + [-1.6 1.6];
erfT = 2.3;
erfWidth = .2;
erfAmp = 1;
EndoT = 2;
EndoWidth = 0.1;
EndoAmp = 1;
noiseStd = 0;
t = 0:1/Fs:dur;
nTrials = 2;
plotFigs = 1;
%% run simulated trials
for iTrial = 1:nTrials
%% make time series
ssvef = sinewave(ssvefAmp,ssvefFreq,t,0);
erf = makeGaussian(t,erfT,erfWidth,erfAmp) - makeGaussian(t,erfT+0.5*erfWidth,erfWidth,erfAmp);
attnGain = makeGaussian(t,EndoT,EndoWidth,EndoAmp) + ones(size(t));
noise = noiseStd.*randn(size(t));
% % contrast modulation from target
% targetOn = find(t==3):find(t==3.05);
% ssvef(targetOn) = ssvef(targetOn)*0.5;
response = (ssvef + erf).*attnGain + noise;
responses(iTrial,:) = response;
if plotFigs
figure
subplot(2,1,1)
hold on
plot(t, ssvef)
plot(t, erf, 'g')
plot(t, attnGain, 'r')
subplot(2,1,2)
plot(t, response, 'k')
end
%% filter
% [filt] = ft_preproc_bandpassfilter(dat,Fs,Fbp,N,type,dir,instabilityfix,df,wintype,dev,plotfiltresp)
filt = ft_preproc_bandpassfilter(response,Fs,Fbp);
if plotFigs
figure
subplot(2,1,1)
hold on
plot(t,ssvef.*attnGain)
plot(t, filt, 'r')
subplot(2,1,2)
hold on
plot(t,ssvef.*attnGain)
plot(t, filt, 'r')
xlim([1.5 2.5])
end
%% Hilbert transform
h = hilbert(filt);
hAmp = abs(h);
% hAmp = ft_preproc_hilbert(filt, 'abs');
hAmps(iTrial,:) = hAmp;
if plotFigs
figure
plot(t, hAmp)
end
%% wavelet
foi = ssvefFreq;
width = 12;
% [spectrum,freqoi,timeoi] = ft_specest_wavelet(response, t, 'freqoi', foi, 'width', width);
[spectrum,freqoi,timeoi] = ft_specest_wavelet(response, t);
specAmp = abs(squeeze(spectrum));
freqIdx = find(abs(freqoi-ssvefFreq) == min((abs(freqoi-ssvefFreq))));
wAmp = specAmp(freqIdx,:);
wAmpNorm = wAmp./mean(wAmp(500:1000));
wAmps(iTrial,:) = wAmpNorm;
end
%% Hilbert then average
meanH = mean(hAmps);
%% average then Hilbert
meanResponse = mean(responses); % mean of raw time series
meanResponseF = ft_preproc_bandpassfilter(meanResponse,Fs,Fbp);
meanResponseFH = abs(hilbert(meanResponseF));
%% wavelet then average
meanW = mean(wAmps);
%% average then wavelet
% [spectrum,freqoi,timeoi] = ft_specest_wavelet(dat, time, varargin)
[spectrum,freqoi,timeoi] = ft_specest_wavelet(meanResponse, t);
specAmp = abs(squeeze(spectrum));
freqIdx = find(abs(freqoi-ssvefFreq) == min((abs(freqoi-ssvefFreq))));
wavAmp = specAmp(freqIdx,:);
wavAmpNorm = wavAmp./mean(wavAmp(500:1000));
if plotFigs
figure
subplot(2,1,1)
imagesc(flipud(specAmp))
subplot(2,1,2)
plot(t, wavAmpNorm)
end
%% plots
% figure
% hold on
% plot(t, ssvef.*attnGain, 'r')
% plot(t, meanResponse)
% legend('orig signal', 'mean response')
figure
hold on
plot(t, attnGain, 'g')
plot(t, meanResponseFH)
plot(t, meanH, 'r')
plot(t, wavAmpNorm, 'k')
plot(t, meanW, 'c')
legend('true attn gain','average then Hilbert','Hilbert then average','average then wavelet','wavelet then average')