-
Notifications
You must be signed in to change notification settings - Fork 2
/
trig_fun_ME125_2.m
185 lines (145 loc) · 6.68 KB
/
trig_fun_ME125_2.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
function [trl, event] = trig_fun_ME125(cfg)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function for Fieldtrip trial selection was created for ARC Discovery Project (DP170103148) -
% MEG rovling MMN study (MEG_ID: 125). It can be easily adapted for use for other MEG data acquaried
% at KIT-Macquarie Brain Reserach Liboratory.
% Sub-function that creates an .evt file was written by Steven Saunders.
%
% Wei He & Paul Sowman
% 2018-08-09
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
file = cfg.dataset;
path = cfg.path;
First_Channel = 146; % Frist trigger channel
Last_Channel = 158; % Last trigger channel
PD_Channel = 135; % -1 for none
PD_Threshold = 0.25; % Photo detector or Audio sensor threshould
hdr = ft_read_header(cfg.dataset,'dataformat','yokogawa_con');
triggers = ft_read_data(cfg.dataset,'dataformat','yokogawa_con','chanindx',First_Channel:Last_Channel);
for i=1:size(triggers,1)
trig_height(i)=max(triggers(i,:));
end
trig_thresh=0.25*max(trig_height);
% Threshold = 2;
Threshold = 0.5*max(trig_height);
Sampling_Rate = hdr.Fs;
FindTriggers(path, file, First_Channel,Last_Channel,Threshold,PD_Channel,PD_Threshold,Sampling_Rate)
event_ori = ft_read_event(cfg.dataset,'dataformat','yokogawa_con','trigindx',First_Channel:Last_Channel,'threshold',trig_thresh,'detectflank','up');
event5 = importdata([cfg.dataset(1:end-4),'.evt']);
event6 = repmat(struct('type','trigger','value',1,'sample',1,'time',1,'duration','duration'), 1, length(event_ori));
triggers3 = load(['oddball_short_',cfg.dataset(1:4),'_run1.txt']);%load textfile with trigger coding
for k = 1:length(event_ori)
event6(k).type = event_ori(k).type;
event6(k).value = triggers3(k,4);
event6(k).sample = event5.data(k,1) * hdr.Fs;
event6(k).time = event5.data(k,1);
end
% add a pre-deviant standard condition with value 8
k=1;
for i = 1:length(event6)-1
if ~strcmp(event6(i).type, event6(i+1).type)
event7(k)=event6(i);
k=k+1;
else
end
end
for i = 1: length(event7)
event7(i).value=8;
end
k=1;
for i = 2: length(event6)
if event6(i).value == 1
event8(k)=event6(i);
k=k+1;
else
end
end
event9 = [event7, event8];
[event index] = sortStruct(event9,'sample');
% determine the number of samples before and after the trigger
pretrig = -round(cfg.trialdef.prestim * hdr.Fs);
posttrig = round(cfg.trialdef.poststim * hdr.Fs);
sample = round([event(find(strcmp('duration', {event.duration}))).sample]');
% value = round([event(find(strcmp('duration', {event.duration}))).value]');
trl = [];
for j = 1:length(event)
%trg1 = value(n);
trlbegin = sample(j) + pretrig;
trlend = sample(j) + posttrig;
offset = pretrig;
newtrl = [trlbegin trlend offset];
trl = [trl; newtrl];
end
% trl = [trl value]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Subfunctions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function FindTriggers(path, file, First_Channel,Last_Channel,Threshold,PD_Channel,PD_Threshold, Sampling_Rate)
% Export the triggers from Yokogawa ".con" files into tab-delimited .evt files
% Change log:
%
% 2015-05-27: If the photo detector is above the threshold when a trigger
% occurrs then wait until it drops below the threshold and then change the
% time of the trigger to the time when the photo detector goes back up
% above the threshold
% 2014-05-05: Make the sampling rate user-adjustable
ffile = fullfile(path, file);
fprintf(1,'FindTriggers: Processing %s\n', ffile);
data = getYkgwData(ffile);
samples=data(First_Channel:Last_Channel,:)';
[numsamples,numchannels] = size(samples);
if (PD_Channel ~= 0)
detector=data(PD_Channel,:)';
end
events = [];
er = 1; % er holds the current row in the events matrix
for s = 2:numsamples,
for c = 1:numchannels,
if samples(s,c) > Threshold && samples(s-1,c) <= Threshold
% A trigger occurred
time = s;
% Correct for photo detector if a PD channel was given,
% but don't correct the PD channel itself!
if (PD_Channel ~= 0 && c ~= (PD_Channel - First_Channel + 1))
% If the photo detector is high when the trigger
% occurred wait until it goes low before starting
% the wait for it to go high
while time <= numel(detector) && detector(time) > PD_Threshold
time = time + 1;
end
% Wait for the photo detector to go above the
% threshold
while time <= numel(detector) && detector(time) <= PD_Threshold
time = time + 1;
end
if time > numel(detector),
fprintf('FindTriggers: Warning - A trigger occurs at Tsec %6.3f but the PD channel does not exceed the threshold (%g) at or after this point\n', (s/Sampling_Rate), PD_Threshold);
elseif (time - s) > 55,
fprintf('FindTriggers: Warning - Photo detector event for trigger at Tsec %6.3f is more than 55ms later at %6.3f.\n', (s/Sampling_Rate), (time/Sampling_Rate));
end
end
events(er,:) = [(time/Sampling_Rate) c 1];
er = er + 1;
end
end
end
events = sortrows(events);
% Construct output filename
[~,name,~] = fileparts(file);
outfname = fullfile(path,[name '.evt']);
if exist(outfname) ~= 0,
button = questdlg(['"' outfname '" already exists. Do you want to replace it?'], 'FindTriggers', 'Cancel', 'Replace', 'Cancel');
if strcmp(button, 'Cancel')
fprintf('FindTriggers: Aborted - file %s already exists\n', outfname);
return;
else
delete(outfname);
end
end
fprintf(1,'FindTriggers: Creating %s\n', outfname);
fid=fopen(outfname, 'w');
fprintf(fid, 'Tsec \t TriNo \t Code \n');
fclose(fid);
dlmwrite(outfname, events, 'delimiter', '\t', 'precision', '%6.3f', '-append');
end
end