This repository has been archived by the owner on Apr 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TF_highlight_burst.m
152 lines (132 loc) · 3.36 KB
/
TF_highlight_burst.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
function TF_highlight_burst(cfg, TFRwave, freq_lim, time_lim)
% This function averages for each trial the power response between
% boundaries given by freq_lim and time_lim. It finally plots the whole
% averaged data to highlight the burst in individual trials.
%
% The data used is the output of the FieldTrip ft_freqanalysis
% function used with cfg.keeptrials = 'yes'.
%
% Last edited 24/08/2016
% Charles Gaydon
freq_l = freq_lim(1);
freq_h = freq_lim(2);
x_l = time_lim(1);
x_h = time_lim(2);
%% Extract relevant data
pow = squeeze(TFRwave.powspctrm);
time = TFRwave.time;
freq = TFRwave.freq;
ntrials = size(pow,1);
nfreq = length(freq);
ntime = length(time);
%% Apply baseline normalization
nt = ntime;
nf = nfreq;
if isfield(cfg,'baseline')
%% Index of the baseline
base_index = [];
baseb = cfg.baseline(1);
basee = cfg.baseline(2);
for t = 1:nt
if time(1,t)>=baseb
base_index = t;
break
end
end
for t = base_index(1):nt
if time(1,t)>basee
base_index = [base_index t];
break
end
end
%% Apply baseline
freq_mp = zeros(1,nf);
for j = 1:ntrials
for i = 1:(nf)
freq_mp(j,i) = nanmean(pow(j,i,base_index(1):base_index(2)));
if ~isfield(cfg,'baselinetype')
elseif strcmp(cfg.baselinetype,'db')
pow(j,i,:) = 10*log10(pow(j,i,:)/freq_mp(j,i));
elseif strcmp(cfg.baselinetype,'perc')
pow(j,i,:) = 100*(pow(j,i,:)-freq_mp(j,i))./freq_mp(j,i);
elseif strcmp(cfg.baselinetype,'z')
var = (1/nt)*nansum((pow(j,i,:)-freq_mp(j,i)).^2);
pow(j,i,:) = (pow(j,i,:)-freq_mp(j,i))./sqrt(var);
end
end
end
end
%% Determine the frequency index between wich we keep the data
if freq_l < freq(1)
index_beg = 1;
else
if freq_l > freq(end)
error('Wrong freq_l input')
else
for i = 1:nfreq
if freq(i) > freq_l
index_beg = i;
break
end
end
end
end
if freq_h > freq(end)
index_end = nfreq;
else
if freq_h < freq(1)
error('Wrong freq_h input')
else
for i = 1:nfreq
if freq(i) > freq_h
index_end = i;
break
end
end
end
end
%% Determine the time index between wich we keep the data
if x_l < time(1)
time_beg = 1;
else
if x_l > time(end)
error('Wrong x_l input')
else
for i = 1:ntime
if time(i) > x_l
time_beg = i;
break
end
end
end
end
if x_h > time(end)
time_end = ntime;
else
if x_h < time(1)
error('Wrong x_h input')
else
for i = 1:ntime
if time(i) > x_h
time_end = i;
break
end
end
end
end
%% Select, average and plot the result.
pow = pow(:,index_beg:index_end,time_beg:time_end);
avpow = squeeze(nanmean(pow,2));
set(gca,'YDir','normal')
imagesc([time(time_beg) time(time_end)], [1 size(avpow,1)],avpow);
if ~isfield(cfg,'zlim')
if isfield(cfg,'baselinetype')
m = max(max(abs(pow(:,:))));
cfg.zlim = [-m m];
set(gca,'Clim',cfg.zlim)
end
end
colorbar
xlabel('Temps (s)')
ylabel('Trial')
end