-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfindSquidJumps.m
227 lines (186 loc) · 6.9 KB
/
findSquidJumps.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
function artifact_Jump = findSquidJumps(data,ID)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
%clear all
%%
%Change the folder to where MEG data is contained
cd('/mnt/homes/home024/jschipp/Surprise_Drug/meg_data/')
%Store all the seperate data files
%restingpaths = dir('*.mat');
%Loop all data files into seperate jobs
%for icfg = 1:length(restingpaths)
% cfgin{icfg}.restingfile = restingpaths(icfg).name;%128 works.
% select only meg data
cfg = [];
cfg.channel = 'MEG';
data = ft_selectdata(cfg,data);
%load data
%load(pathname.restingfile)
doplot=1;
%calculate squid jumps
cfg = [] ;
cfg.length = 7; %5; % CG uses 7s
%lengthsec = 7;
%cfg.toilim =[0 7];
%cfg.offset = 1:1200*lengthsec:1200*301;
cfg.overlap = 0.2; %0.8; % CG uses 0.2
data = ft_redefinetrial(cfg,data) ;
data.cfg.previous = [];
% detrend and demean
cfg = [];
cfg.detrend = 'yes';
cfg.demean = 'yes';
%cfg1.trials = cfg1.trial{1}
data = ft_preprocessing(cfg, data);
data.cfg.previous = [];
% compute the intercept of the loglog fourier spectrum on each trial
disp('searching for trials with squid jumps...');
% get the fourier spectrum per trial and sensor
cfgfreq = [];
cfgfreq.method = 'mtmfft';
cfgfreq.output = 'pow';
cfgfreq.taper = 'hanning';
cfgfreq.channel = 'MEG';
cfgfreq.foi = 1:130;
cfgfreq.keeptrials = 'yes';
freq = ft_freqanalysis(cfgfreq, data);
% compute the intercept of the loglog fourier spectrum on each trial
disp('searching for trials with squid jumps...');
intercept = nan(size(freq.powspctrm, 1), size(freq.powspctrm, 2));
x = [ones(size(freq.freq))' log(freq.freq)'];
for t = 1:size(freq.powspctrm, 1),
for c = 1:size(freq.powspctrm, 2),
b = x\log(squeeze(freq.powspctrm(t,c,:)));
intercept(t,c) = b(1);
end
end
% detect jumps as outliers, actually this returns the channels too...
[~, idx] = deleteoutliers(intercept(:));
%subplot(4,4,cnt); cnt = cnt + 1;
if isempty(idx),
artifact_Jump = [];
fprintf('no squid jump trials found \n');
%title('No jumps'); axis off;
else
% getting trial/channel numbers of all jumps
[y,x] = ind2sub(size(intercept),idx);
jump_details = [y x];
% storing info about jumps
jumps_total=length(unique(jump_details(:,1)));
artifact_Jump = data.sampleinfo(unique(jump_details(:,1)),:);
cd('/mnt/homes/home024/jschipp/Surprise_Drug/meg_preprocessing/Log_files')
fid=fopen('logfile_squidJumps','a+');
c=clock;
fprintf(fid,sprintf('\n\nNew entry for %s at %i/%i/%i %i:%i\n\n',ID,fix(c(1)),fix(c(2)),fix(c(3)),fix(c(4)),fix(c(5))))
fprintf(fid,'Number of squid jumps: %i',jumps_total)
fclose(fid)
%For each detected jump, loop and get the name
if doplot
for iout = 1:length(idx)
%I belive that y is trial and x is channel.
[y,x] = ind2sub(size(intercept),idx(iout));
%Store the name of the channel
channelJump{iout} = freq.label(x);
%subplot...
plot(data.trial{y}( ismember(data.label,channelJump{iout}),:))
end
title(sprintf([num2str(jumps_total),' jumps found']));
end
%error('Finally a jump')
% fprintf('removing %d squid jump trials \n', length(unique(t)));
% [t,~] = ind2sub(size(intercept),idx);
%
% % remove those trials
% cfg = [];
% cfg.trials = true(1, length(data.trial));
% cfg.trials(unique(t)) = false; % remove these trials
% data = ft_selectdata(cfg, data);
%
% % plot the spectrum again
% cfgfreq.keeptrials = 'no';
% freq = ft_freqanalysis(cfgfreq, data);
% loglog(freq.freq, freq.powspctrm, 'linewidth', 0.1); hold on;
% loglog(freq.freq, mean(freq.powspctrm), 'k', 'linewidth', 1);
% axis tight; axis square; box off;
% set(gca, 'xtick', [10 50 100], 'tickdir', 'out', 'xticklabel', []);
% title(sprintf('%d jumps removed', length(unique(t))));
end
% % detect jumps as outliers
% cfg = [];
% %cfg.trl = trl;
% %cfg.datafile = 'ArtifactMEG.ds';
% %cfg.headerfile = 'ArtifactMEG.ds';
% cfg.continuous = 'yes';
%
% % channel selection, cutoff and padding
% cfg.artfctdef.zvalue.channel = 'MEG';
% cfg.artfctdef.zvalue.cutoff = 20;
% cfg.artfctdef.zvalue.trlpadding = 0;
% cfg.artfctdef.zvalue.artpadding = 0;
% cfg.artfctdef.zvalue.fltpadding = 0;
%
% % algorithmic parameters
% cfg.artfctdef.zvalue.cumulative = 'yes';
% cfg.artfctdef.zvalue.medianfilter = 'yes';
% cfg.artfctdef.zvalue.medianfiltord = 9;
% cfg.artfctdef.zvalue.absdiff = 'yes';
%
% % make the process interactive
% cfg.artfctdef.zvalue.interactive = 'yes';
%
% [cfg, artifact_Jump] = ft_artifact_zvalue(cfg,data);
%
%subplot(2,3,cnt); cnt = cnt + 1;
% if isempty(artifact_Jump),
% fprintf('no squid jump trials found \n');
% title('No jumps'); axis off;
% else
%
% %Figure out the actual number of jumps.
% baselineSample = artifact_Jump(:,1)-artifact_Jump(1,1);
%
% %first jump
% it_jump = 1;
%
% %set distance between jumps threshold in samples
% thresh_jumps = 1200;
%
% %Iteratively find the number of jumps
% while sum(baselineSample(:)>thresh_jumps)>0
% %then there are more than 1 jumps
%
% %total number of iterations
% it_jump = it_jump + 1;
%
%
%
% %find the next one further away than threshold
% if (baselineSample(it_jump)-baselineSample(it_jump-1))>thresh_jumps
% baselineSample(it_jump:end)=baselineSample(it_jump:end)-baselineSample(it_jump,1);
% end
% end
%
%
%
% %I need to get the start and end samples and then add some samples to
% %remove.
% jumpPadding = 1200*0.2;
%
% %index the startsamples of the jumps
% jumpStart = artifact_Jump(baselineSample(:,1)==0)-jumpPadding;
% jumpEnd = artifact_Jump(baselineSample(:,1)==0)+jumpPadding;
% artifact_Jump=[jumpStart jumpEnd];
%
% %delete jump data artifact
% %[ data ] = delete_artifact_Numbers(artifact_Jump, data, sampleinfo);
%
%
% plot(artifact_Jump, 'k', 'linewidth', 1);
% axis tight; axis square; box off;
% %set(gca, 'xtick', [10 50 100], 'tickdir', 'out', 'xticklabel', []);
% title(sprintf('jump removed'));
% end
%
%cfgin=cfgin{1}
%end
end