-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnr_aomatstruct_ipad.m~
261 lines (200 loc) · 8.19 KB
/
nr_aomatstruct_ipad.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
function nr_aomatstruct_ipad(aomatconv_filename,M1_ch,ipad_filename,aux_lbl,aomatstruct_file_dir,anl_dir)
% converts mat file from AlphaOmega Mapfile converter program to a format
% that can be run on existing code for ecogPSD analysis and new ipad
% analyses
% Created by SAS (12/15/2008)pl
% Edited by SAS (5/29/09) to process EMG/accel data.
%
% Input:
% 1) "fname.mat" = output file form Mapfileconverter
% Output:
% 1) "fname_ecog.mat" = ecog/LFP data
%% load data
% [aomatconv_filename pathname]=uigetfile('*.mat','Select .mat file');
% cd(pathname);
load(aomatconv_filename);
aomatconv_filename_ind = strfind(aomatconv_filename,'/');
aomatconv_filename = [aomatconv_filename(aomatconv_filename_ind(end)+1:strfind(aomatconv_filename,'.mat')-1)];
%aomatconv_filename = strr(aomatconv_filename,'.mat','');
%assignin('base','aomatconv_filename',aomatconv_filename)
%assignin('base','aomatconv_filename',aomatconv_filename)
% make sure sampling frequencies are equal for ecog/LFP/task recordings, then
% use CECOG_1_KHz varible as the sampling rate
% if isequal(CECOG_1_KHz,CECOG_2_KHz,CECOG_3_KHz,CECOG_4_KHz,CECOG_5_KHz, CLFP_KHz,CAn_In__1_KHz,CAn_In__2_KHz,CAn_In__3_KHz,CEMG_1_KHz,CEMG_2_KHz,CEMG_3_KHz)
if exist('CECOG1_KHz')
Fs = CECOG1_KHz;
Fs = Fs*1000;
else
Fs = CECOG_1_KHz;
Fs = Fs*1000;
end
% multiply by 1000 for KHz->Hz conversion
% else
% error('Sampling frequencies are different for ecog/LFP data');
% end
%% M1 contact
% M1_ch = input('Enter channel # for M1 contact (usually 4 or 5): '); % added 9/9/09 to be carried over for later programs
%% force ecog/lfp data to be the same lengths for montage-ing
minlength=min([length(CECOG_1) length(CECOG_2) length(CECOG_3)...
length(CECOG_4) length(CECOG_5) length(CLFP)]);
CECOG_1 = CECOG_1(1:minlength);
CECOG_2 = CECOG_2(1:minlength);
CECOG_3 = CECOG_3(1:minlength);
CECOG_4 = CECOG_4(1:minlength);
CECOG_5 = CECOG_5(1:minlength);
CLFP = CLFP(1:minlength);
%% ecog/LFP voltage calibration
C1 = 5/32768; % constant for calibration
% note: value 32768 is equal to 5V in the Alpha Omega system
C2 = 1e6; % constant to convert ecog/lfp channel voltage level from V->microV to match GL4k system
Gecog = 7000; % ecog channel gain
Glfp = 25000; % LFP channel gain
CAn_In__1 = CAn_In__1*C1;
CAn_In__2 = CAn_In__2*C1;
CAn_In__3 = CAn_In__3*C1;
% CEMG_1 = CEMG_1*C1;
% CEMG_2 = CEMG_2*C1;
% CEMG_3 = CEMG_3*C1;
%
CECOG_1 = CECOG_1*C1*C2/Gecog;
CECOG_2 = CECOG_2*C1*C2/Gecog;
CECOG_3 = CECOG_3*C1*C2/Gecog;
CECOG_4 = CECOG_4*C1*C2/Gecog;
CECOG_5 = CECOG_5*C1*C2/Gecog;
CLFP = CLFP*C1*C2/Glfp;
%% store processed ecog/LFP data into ecog structure array
% frq1 = menu('Select one','resample1K','resample2K','no');
d = round(Fs/1000);
if int32(Fs) == 1502
frq1 = 2;
frq2 = 3;
d=Fs/1000;
else
frq1=1;
frq2=d;
d=Fs/1000;
end
% ecog = struct('contact_pair',{},'rest_time',{},'active_time',{});
% evt = menu('Select one','LFP','No LFP');
ecog(1).contact_pair(1).raw_ecog_signal=resample(CECOG_1,frq1,frq2);
ecog(1).contact_pair(2).raw_ecog_signal=resample(CECOG_2,frq1,frq2);
ecog(1).contact_pair(3).raw_ecog_signal=resample(CECOG_3,frq1,frq2);
ecog(1).contact_pair(4).raw_ecog_signal=resample(CECOG_4,frq1,frq2);
ecog(1).contact_pair(5).raw_ecog_signal=resample(CECOG_5,frq1,frq2);
if ~isempty(strfind(aomatconv_filename,'lfp')) || ~isempty(strfind(aomatconv_filename,'LFP'))% evt == 1
ecog(1).contact_pair(6).raw_ecog_signal=resample(CLFP,frq1,frq2);
end
Fs = 1000;
%% store processed Aux chan data into aux structure array
aux = struct('chan',{});
aux(1).chan(1).raw=resample(CAn_In__1,frq1,frq2);
aux(1).chan(1).lbl = auxchan_lbl.prelead
aux(1).chan(2).raw=resample(CAn_In__2,frq1,frq2);
aux(1).chan(3).raw=resample(CAn_In__3,frq1,frq2);
%% store processed EMG chan data into aux structure array
emg = struct('chan',{});
emg(1).chan(1).raw=resample(CEMG_1,frq1,frq2);
emg(1).chan(2).raw=resample(CEMG_2,frq1,frq2);
emg(1).chan(3).raw=resample(CEMG_3,frq1,frq2);
emg(1).chan(4).raw=resample(CEMG_4,frq1,frq2);
%% remove zeros add when map data are converted in mat file by 'mapconverter'
%% Do the remontage
for i = 1: length(ecog.contact_pair)
if i<5
ecog.contact_pair(i).remontaged_ecog_signal = ecog.contact_pair(i).raw_ecog_signal - ecog.contact_pair(i+1).raw_ecog_signal;
else
ecog.contact_pair(i).remontaged_ecog_signal = ecog.contact_pair(i).raw_ecog_signal;
end
end
%% grab rest/active timestamps from task button
% ask user for aux channel with task button voltage
% evt = menu('Select one','rest/active data','rest only');
if ~isempty(strfind(aomatconv_filename,'ipad')) %evt == 1
% find active and rest periods using the raw signal
MARGIN = 0.5; % is a very wide range that handles large fluctuations in task voltage. ParseIntraOpEvents uses MARGIN = 0.05
thresh = max(CAn_In__3);
ACTIVE = [thresh-MARGIN thresh+MARGIN]; %Determine the threshold
in1 = nr_inrange(CAn_In__3,ACTIVE);
inds = find(in1);
[pos,n] = nr_evFindGroups(inds,1,1000*d); %find active period of 1s minimum
active_time = inds(pos(1,:))/d;
rest_time = inds(pos(2,:))/d;
%assignin('base','thresh',thresh)
%%assignin('base','active',active)
%assignin('base','in1',in1)
%%assignin('base','inds',inds)
%assignin('base','active_time',active_time)
%assignin('base','rest_time',rest_time)
% save rest/active timestamps in ecog structure array
ecog(1).rest_time=int32(rest_time);
ecog(1).active_time=int32(active_time);
% find the begining of each trial using the raw signal
CAn_In__1=CAn_In__1-mean(CAn_In__1);
% thresh = max(CAn_In__1);
% MARGIN = thresh/5; % 0.3 is a very wide range that handles large fluctuations in task voltage. ParseIntraOpEvents uses MARGIN = 0.05
% ACTIVE = [thresh-MARGIN thresh+MARGIN];
% in1 = inrange(CAn_In__1,ACTIVE);
% inds = find(in1);
% thresh = max(CAn_In__1)-3*std(CAn_In__1);
close ;plot(CAn_In__1)
hold on
thresh = input('thresh = ');
%thresh = 0.02 %input('thresh');
%assignin('base','CAn_In__1',CAn_In__1)
%assignin('base','thresh',thresh)
inds = find(abs(CAn_In__1)>thresh);
%assignin('base','inds',inds)
[pos,n] = nr_evFindGroups(inds,5*d,2);
ipad_ON_time = inds(pos(1,1:end-1))/d;
ipad_OFF_time = inds(pos(1,2:end))/d;
% delete false detection
diff_ipad_ON= ipad_ON_time(2:end)-ipad_ON_time(1:end-1);
ok_ON = find(diff_ipad_ON>5000);
%%assignin('base','evFindGroups',evFindGroups)
%assignin('base','d',d)
%assignin('base','thresh',thresh)
%%assignin('base','active',active)
%assignin('base','in1',in1)
%%assignin('base','inds',inds)
%assignin('base','active_time',active_time)
%assignin('base','rest_time',rest_time)
%assignin('base','ipad_ON_time',ipad_ON_time)
%assignin('base','diff_ipad_ON',diff_ipad_ON)
%assignin('base','ok_ON',ok_ON)
diff_ipad_OFF= ipad_OFF_time(2:end)-ipad_OFF_time(1:end-1)
assignin('base','diff_ipad_OFF',diff_ipad_OFF)
%ok_OFF = find(diff_ipad_OFF>5000);
% check the timing
plot(ipad_ON_time*d, thresh,'*r')
plot(ipad_OFF_time*d, thresh,'*k')
ecog(1).ipad_ON_time=int32(ipad_ON_time);
ecog(1).ipad_OFF_time= int32(ipad_OFF_time);
%assignin('base','ok_ON',ok_ON)
%assignin('base','ipad_ON_time',ipad_ON_time)
else
ecog(1).rest_time=[];
ecog(1).active_time=[];
ecog(1).ipad_ON_time=[];
ecog(1).ipad_OFF_time=[];
end
%% save data
% use aomatconv_filename to create output name
name=[aomatconv_filename(1:11),'str_',aomatconv_filename(12:end)];
%assignin('base','name',name)
%cd(['~/Dropbox/cluster_files/data/',aomatconv_filename(1:10),'/anl'])
cd(anl_dir)
load(aux_lbl)
cd(aomatstruct_file_dir)
save(name,'name','ecog','M1_ch','aux','emg','Fs','thresh','auxchan_lbl');
%% Add IPAD file info
%[ipad_data_aomatconv_filename, ipad_data_dir] = uigetfile('*.mat','Choose ipad data file'); % choose ipad data file
load(ipad_filename)
%assignin('base','ipad_data_dir',ipad_data_dir)
%
%
% name_ipad=[aomatconv_filename '_ecog'];
%
% load(name_ipad);
%cd([ipad_data_dir(1:end-5),'anl'])
cd(aomatstruct_file_dir)
save(name,'description','timestamp','-append');