-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxl_TimeFreq.m
308 lines (249 loc) · 8.73 KB
/
xl_TimeFreq.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
%% TIME-FREQ ANALYSIS - for iES-iEEG
% Xianqing Bella Liu
% 09/17/2023
% REQUIRED TOOLBOXES
% FIELDTRIP
clear; clc;
close all;
rootpath = '/Users/xianqliu/Library/CloudStorage/OneDrive-UniversityofIowa/1_TMS_iEEG';
cd(rootpath);
addpath /Users/xianqliu/Documents/MATLAB/fieldtrip-20230206
addpath(genpath('./0_Scripts'), ...
genpath('./3_ProcessedData_Backup'));
StimType = 'esTT_L-DLPFC';
patient_list = [460 534 625 634];
chlinfo = readtable([rootpath '/3_ProcessedData_Backup/Selected_channel_AMY.xlsx'], 'Sheet', 'esTT L-DLPFC');
ft_defaults;
%% 1. Load Data for Each Patient
% Create a cell array to hold the data from each patient
all_patients_data = cell(1, length(patient_list)); % replace numPatients with the actual number of patients
% Loop through each patient's dataset
for i = 1:length(patient_list)
% Load data (Replace this line with actual code to load data)
data = load([rootpath '/3_ProcessedData_Backup/xl_Data_Processed/' StimType '/' ...
num2str(patient_list(i)) '_' StimType '_0_5Hz_4B-UnfilterEpoched.mat']);
% Store in cell array
all_patients_data{i} = data;
end
clear data ftData trigLengths trigShift trigTimes sessionDir
%% 2. Select Channels and Trials, and Combine Channels Across Patients
% specify channels in amyg
if strcmp(StimType, 'esTT_L-DLPFC')
amygChannel{1} = {'LFPx105' 'LFPx106' 'LFPx107'};
amygChannel{2} = {'LFPx228' 'LFPx229' 'LFPx230' 'LFPx231'};
amygChannel{3} = {'LFPx132' 'LFPx133' 'LFPx134'};
amygChannel{4} = {'LFPx57'};
end
amygChannelAll = [amygChannel{1} amygChannel{2} amygChannel{3} amygChannel{4}];
nChannel = length(amygChannelAll);
% Apply the channel selection to each patient's data
for i = 1:length(patient_list)
cfg = [];
cfg.channel = amygChannel{i}; % amyg channel each patient
all_patients_data{i} = ft_selectdata(cfg, all_patients_data{i}.ftData);
end
% Append the data across patients
cfg = [];
combined_data = ft_appenddata(cfg, all_patients_data{:});
combined_data_org = combined_data;
removeTrials = cell(1,nChannel);
for i = 1:nChannel
removeTrials{i} = str2double(strsplit(chlinfo{i,4}{1}, ';'));
removeTrials{i} = removeTrials{i}(~isnan(removeTrials{i}));
end
% Loop through each channel and set the data in the trials to be removed to NaN
for i = 1:nChannel % assuming patient_data is your FieldTrip data structure
% Check if there are trials to remove for this channel
if ~isnan(removeTrials{i})
% Loop over the trials and set the data values to NaN
for trl = removeTrials{i}
patient_data.trial{trl}(i, :) = NaN;
end
end
end
% For each channel
singleChannelData = cell(1,nChannel);
for i = 1:nChannel
cfg = [];
cfg.channel = amygChannelAll{i}; % Select the specific channel
cfg.trials = ones([1 length(combined_data.trial)]);
trl_deleted = str2double(strsplit(chlinfo{i,4}{1}, ';'));
trl_deleted = trl_deleted(~isnan(trl_deleted));
cfg.trials(trl_deleted) = 0;
cfg.trials = logical(cfg.trials); % logical value of whether the trials should be kept
singleChannelData{i} = ft_selectdata(cfg, combined_data);
% Now singleChannelData will have only the desired trials for the specific channel.
end
% Append single channel data
cfg = [];
combined_data = ft_appenddata(cfg, singleChannelData{:});
% Medial and lateral division
cfg = [];
cfg.channel = {'LFPx105' 'LFPx228' 'LFPx229' 'LFPx230' 'LFPx231'};
medial_data = ft_selectdata(cfg, combined_data);
cfg = [];
cfg.channel = {'LFPx106' 'LFPx107' 'LFPx132' 'LFPx133' 'LFPx134' 'LFPx57'};
lateral_data = ft_selectdata(cfg, combined_data);
%% 3. Time-Frequency Analysis
freq = cell(1,nChannel);
freq_blc = cell(1,nChannel);
for i = 1:nChannel
cfg = [];
cfg.output = 'pow';
cfg.method = 'wavelet';
% cfg.method = 'mtmfft';
% cfg.method = 'mtmconvol';
% cfg.taper = 'dpss';
% cfg.tapsmofrq = 2; % Smoothing parameter for the multitaper
% cfg.t_ftimwin = 0.5 * ones(size(cfg.foi)); % length of time window, which typically scales with frequency
cfg.width = 7;
cfg.foi = 2:1:200;
cfg.toi = -0.2:0.005:1.5;
cfg.keeptrials = 'yes';
freq{i} = ft_freqanalysis(cfg, singleChannelData{i});
% Baseline Correction
cfg = [];
cfg.baseline = [-0.2 -0.01];
cfg.baselinetype = 'relative';
freq_blc{i} = ft_freqbaseline(cfg, freq{i});
end
%% 4.1 Multi-Channel Power Spectrum Plot
for i = 1:nChannels
subplot(ceil(sqrt(nChannels)), ceil(sqrt(nChannels)), i);
% Extract Data for the channel
data = squeeze(mean(freq_blc{i}.powspctrm(:, 1, :, :), 1));
% Log-scale frequencies
log_frequencies = log(freq_blc{i}.freq);
% Plot
imagesc(freq_blc{i}.time, log_frequencies, data);
% Flip y-axis
set(gca, 'YDir', 'normal');
% Customize Axes
yticks(log([2, 4, 10, 30, 70, 150]));
yticklabels({'2', '4', '10', '30', '70', '150'});
% Set color bar limits
caxis([-0. 0.5]);
title(['Channel: ' amygChannelAll{i}]);
end
colorbar;
%% 4.2 Average power spectrum across channels
% average_powspctrm - a matrix of size [N_Frequencies x N_Times]
average_powspctrm = nanmean(freq_blc{:}.powspctrm, 1);
average_powspctrm = nanmean(average_powspctrm, 2);
average_powspctrm = squeeze(average_powspctrm);
% Plot averaged power spectrum
% Log-scale frequencies
log_frequencies = log(freq_blc.freq);
figure; % Create a new figure for the averaged power spectrum
imagesc(freq_blc.time, log_frequencies, average_powspctrm);
% Flip y-axis
set(gca, 'YDir', 'normal');
% Customize Axes
yticks(log([2, 4, 10, 30, 70, 150]));
yticklabels({'2', '4', '10', '30', '70', '150'});
% Set color bar limits
caxis([0 5]);
% Add labels and title
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('Averaged Power Spectrum Across Channels');
% Add color bar
colorbar;
%% Plot power spectrum: Multi-Channel Overview
% layout = [];
% layout.label = combined_data.label';
% layout.pos = [
% 0.1 0.9; 0.5 0.9; 0.9 0.9; 1.3 0.9; % Row 1
% 0.1 0.6; 0.5 0.6; 0.9 0.6; 1.3 0.6; % Row 2
% 0.1 0.3; 0.5 0.3; 0.9 0.3];
%
% cfg.showlabels = 'yes';
% cfg.fontsize = 8;
% cfg.layout = layout;
% cfg.colorbar = 'yes';
% cfg.ylim = [2 150];
% cfg.zlim = [-1 5];
% cfg.baseline = [-0.1 -0.01];
%
% ft_multiplotTFR(cfg, freq_blc);
%% Plot normalized data for each channel
% % Extract the baseline window data
% baseline_indices = (freq_blc.time >= -0.2) & (freq_blc.time <= -0.01);
%
% for ch = 1:nChannels
% subplot(ceil(sqrt(nChannels)), ceil(sqrt(nChannels)), ch);
%
% % Extract Data for the channel
% channel_data = squeeze(freq_blc.powspctrm(:, ch, :, :));
%
% % Compute the baseline mean for this channel
% baseline_data = channel_data(:, :, baseline_indices);
% baseline_mean = nanmean(baseline_data, 3);
%
% % Normalize the data by the baseline mean
% normalized_data = bsxfun(@rdivide, channel_data, baseline_mean);
%
% % Average over trials if necessary
% avg_normalized_data = nanmean(normalized_data, 1);
%
% % Log-scale frequencies for visualization
% log_frequencies = log(freq_blc.freq);
%
% % Plot the normalized data
% imagesc(freq_blc.time, log_frequencies, squeeze(avg_normalized_data));
%
% % Flip y-axis
% set(gca, 'YDir', 'normal');
%
% % Customize Axes
% yticks(log([2, 10, 30, 70, 150]));
% yticklabels({'2', '10', '30', '70', '150'});
%
% % Set color bar limits
% caxis([-1 8]); % or use a different scale depending on your normalized data
%
% title(['Channel: ' freq_blc.label{ch}]);
% end
%
% colorbar;
%
% %% Plot averaged channel after normalization
% baseline_data = freq_blc.powspctrm(:,:,:,baseline_indices);
%
% % Compute the baseline mean for each channel and frequency
% baseline_mean = nanmean(baseline_data, 4); % The 4th dimension is time
%
% % Normalize each channel by its own baseline mean
% normalized_data = bsxfun(@rdivide, freq_blc.powspctrm, baseline_mean);
%
% % Average across channels (normalized)
% average_powspctrm = nanmean(normalized_data, 1);
% average_powspctrm = nanmean(average_powspctrm, 2);
% average_powspctrm = squeeze(average_powspctrm);
%
%
% % Plot averaged power spectrum
% % Log-scale frequencies
% log_frequencies = log(freq_blc.freq);
%
% figure; % Create a new figure for the averaged power spectrum
%
% imagesc(freq_blc.time, log_frequencies, average_powspctrm);
%
% % Flip y-axis
% set(gca, 'YDir', 'normal');
%
% % Customize Axes
% yticks(log([2, 4, 10, 30, 70, 150]));
% yticklabels({'2', '4', '10', '30', '70', '150'});
%
% % Set color bar limits
% caxis([-1 5]);
%
% % Add labels and title
% xlabel('Time (s)');
% ylabel('Frequency (Hz)');
% title('Averaged Power Spectrum Across Channels');
%
% % Add color bar
% colorbar;