-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAudMotion_sEEG.m
More file actions
executable file
·345 lines (248 loc) · 10.1 KB
/
AudMotion_sEEG.m
File metadata and controls
executable file
·345 lines (248 loc) · 10.1 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
%function AudMotion_sEEG
% Original Script Written by Sam Weiller to localize MT+/V5
% Adapted by M.Rezk to localize MT/MST (Huk,2002)
% Simplify the ALocaliser fmri experiment for ERPs
% simply run the script and press enter instead of specifying the
% SubjectName & provide run# to be savedin the logfile, and provide the
% length trials/duration of exp (options: 1-2-3)
% NOTES:
% when the response is given while the sound is played, it can be
% understood in the logfile, because of the time stamp.
clear
clc
PsychPortAudio('Close');
tic
%% set trial or real experiment
device = 'eeg'; % any sound card, triggers through parallel port
% device = 'RME_RCAtrig'; % works with RME sound card and sends one trigger value through RCA cable (trigger box)
% device = 'trial'; % any sound card, no triggers (parallel port not open)
fprintf('Connected Device is %s \n\n',device);
% adjust the amp according to the participant
% multiply the audio by this value to decrease the volume
new_amp = 0.2;
%after everything is ready, wait a bit to initiate sound playing loop
Init_pause = 3;
%% Start me up
SubjName = 'test';
Run = input('\nrun n.: ','s');
if isempty(Run)
Run=99;
end
task_id = 'AudERPs';
% here is prompt a multiverse scenario in witch you can choos n. of trials
% and therefore the length of the experiment
fprintf('\n\n case 1 - 54 trials per condition (Motion & Static) + ~10%% targets (n.12) \n for ~5 min, to repeat at least 2 times\n')
fprintf('\n case 2 - 40 trials per condition (Motion & Static) + ~9%% targets (n.8) \n for ~4 min, to repeat at least 3 times\n')
fprintf('\n case 3 - 28 trials per condition (Motion & Static) + ~12%% targets (n.8) \n for ~3 min, to repeat at least 4 times\n\n')
expLength = input('length of th exp. [1 - 2 - 3]: ','s');
fprintf('\n')
%% Create log output(s)
%logfile name for .tsv (for this script only)
DateFormat = 'yyyy_mm_dd_HH_MM';
Filename = fullfile(pwd, 'output', ...
['sub-' SubjName, ...
'_run-' Run, ...
'_case-n-' expLength, ...
'_' datestr(now, DateFormat) '.tsv']);
% prepare for the output
% ans 7 means that a directory exist
if exist('output', 'dir') ~= 7
mkdir('output');
end
% open a tsv file to write the output (for this script only)
fid = fopen(Filename, 'a');
fprintf(fid, 'SubjID\tExp_trial\tCondition\tSoundfile\tTarget\tTrigger\tISI\tEvent_start\tEvent_end\tEvent_duration\tResponse\tRT\n');
fprintf('Auditory ERPs \n\n')
%% Experiment Parametes
freq = 44100;
switch str2num(expLength)
case 1
% number of trials
numEvents = 120;
% n of target trials
numTargets = 12;
case 2
numEvents = 88;
numTargets = 8;
case 3
numEvents = 64;
numTargets = 8;
end
% creating jitter with uniform distribution around 1 average is 1.5 (after
%1s sound, 1s min gap and max 2s)
jitter = rand(1,numEvents);
% CONSIDER MAKING JITTER BALANCED ACROSS CONDITIONS
% a vector of interstimulus intervals for each event
ISI = 1 + jitter;
%% Experimental Design
% pseudorandomized events order: 2 MOTION + 1 static + 10% of targers
[Event_names, Event_order]= getTrialSeq(numEvents,numTargets,expLength);
% reassign it in case pseudorandomization provided less trial number
numEvents = length(Event_order);
soundfiles = {...
'rms_static_1s',...
'rms_mot_LR_1s',...
'rms_mot_RL_1s',...
'rms_static_2s',...
'rms_mot_LR_2s',...
'rms_mot_RL_2s'};
numcondition = length(soundfiles);
condition = {...
'static',...
'motion',...
'motion',...
'static',...
'motion',...
'motion'};
isTarget = [0 0 0 1 1 1];
%% InitializePsychAudio;
% load all the sounds & lower the amplitude of the sounds
% lower the amp is crucial for the in-ear headphone set!
for icon = 1:numcondition
chosen_file{icon} = [soundfiles{icon},'.wav'];
filename = fullfile('stimuli',SubjName,chosen_file{icon});
[SoundData{icon},~]=audioread(filename);
SoundData{icon} = SoundData{icon} .* new_amp;
SoundData{icon} = SoundData{icon}';
end
InitializePsychSound(1);
% open audio port
audio_config.freq = freq;
audio_config.sound = SoundData{1}; % test sound
% Open parallel port
audio_config = triggerSend('open', device, audio_config);
%% Experiment Start
KbName('UnifyKeyNames')
% Wait for the "SPACE" key with KbCheck in the subfuction.
pressSpace4me();
% get time point at the beginning of the experiment (machine time)
experimentStartTime = GetSecs();
%initial pause
WaitSecs(Init_pause);
%% Loop starts
eventOnsets = zeros(1,numEvents);
eventEnds = zeros(1,numEvents);
eventDurations = zeros(1,numEvents);
responses = {};
playTime = zeros(1,numEvents);
conditions = {};
isTargets = zeros(1,numEvents);
for iEvent = 1:numEvents
startEvent = GetSecs();
responseKey = 'NA';
responseTime = 0;
conditions{end+1,1} = condition(Event_order(iEvent));
% get the onset time
eventOnsets(iEvent)=GetSecs-experimentStartTime;
% get the condition of the event (motion or static)
timeLogger(iEvent).condition = condition(Event_order(iEvent));
% get the name of the event
timeLogger(iEvent).names = soundfiles(Event_order(iEvent));
% get the ISI of the event
timeLogger(iEvent).ISI = ISI(iEvent);
% Load the chosen sound
Sound = SoundData{Event_order(iEvent)};
% fill the buffer
audio_config.sound = Sound;
audio_config = triggerSend('fillBuffer', device, audio_config);
% start sound and send the trigger
trigger = Event_order(iEvent);
audio_config.this_trigger = trigger;
audio_config = triggerSend('start', device, audio_config);
playTime(1,iEvent) = audio_config.playTime ;
% log the start time of the sound
timeLogger(iEvent).startTime = playTime(1,iEvent) - experimentStartTime; %#ok<*SAGROW>
% wait for the ISI and register the responseKey
while (GetSecs-(playTime(1,iEvent)+(length(Sound)/freq))) <= (ISI(iEvent))
status = PsychPortAudio('GetStatus', audio_config.pahandle);
if ~status.Active
PsychPortAudio('Stop', audio_config.pahandle);
end
[keyIsDown, secs, keyCode] = KbCheck(-1);
if keyIsDown
responseKey = KbName(find(keyCode));
responseTime = secs - experimentStartTime;
audio_config = triggerSend('resp', device, audio_config);
% ecs key press - stop playing the sounds//script
if strcmp(responseKey,'DELETE')==1
% If the script is stopped while a sequence is being
% played, it close psychport audio and sends trigger a
% trigger if connected to EEG trigger
audio_config = triggerSend('abort', device, audio_config);
return
end
end
end
%calculate timings
eventEnds(iEvent)=GetSecs-experimentStartTime;
eventDurations(iEvent)=eventEnds(iEvent)-eventOnsets(iEvent);
% log response and time
responses{end+1} = responseKey;
responsesTime(iEvent) = responseTime;
isTargets(iEvent) = isTarget(Event_order(iEvent));
% get the total trial duration and all for structure
timeLogger(iEvent).length = eventDurations(iEvent);
% get the time for the block end
timeLogger(iEvent).endTime = eventEnds(iEvent);
timeLogger(iEvent).responseTime = responsesTime(iEvent);
timeLogger(iEvent).response = responses(iEvent);
timeLogger(iEvent).isTarget = isTarget(Event_order(iEvent));
timeLogger(iEvent).soundcode = trigger;
%logfile incase the script stopped
fprintf(fid,'%s\t %d\t %s\t %s\t %d\t %d\t %f\t %f\t %f\t %f\t %s\t %f\n',...
SubjName, iEvent, condition{Event_order(iEvent)}, soundfiles{Event_order(iEvent)}, ...
isTarget(Event_order(iEvent)), trigger, ISI(iEvent), ...
timeLogger(iEvent).startTime, eventEnds(iEvent), eventDurations(iEvent), ...
responseKey, responseTime);
fprintf(1,'%s\t %d\t %s\t %s\t %d\t %d\t %f\t %f\t %f\t %f\t %s\t %f\n',...
SubjName, iEvent, condition{Event_order(iEvent)}, soundfiles{Event_order(iEvent)}, ...
isTarget(Event_order(iEvent)), trigger, ISI(iEvent), ...
timeLogger(iEvent).startTime, eventEnds(iEvent), eventDurations(iEvent), ...
responseKey, responseTime);
end
%% Save the results ('names','onsets','ends','duration') of each block
names = cell(length(timeLogger),1);
onsets = zeros(length(timeLogger),1);
ends = zeros(length(timeLogger),1);
durations = zeros(length(timeLogger),1);
for i=1:length(timeLogger)
names(i,1) = timeLogger(i).names;
onsets(i,1) = timeLogger(i).startTime;
ends(i,1) = timeLogger(i).endTime;
durations(i,1) = timeLogger(i).length;
end
condition = conditions';
Events_order = Event_order';
target = isTargets';
isi = ISI';
eventEnd = eventEnds';
response = responses';
responseTime = responsesTime';
[ t, table_header ] = make_events(SubjName, task_id, Run, onsets, durations, conditions, ...
names, ...
target, ...
Events_order, ...
isi, ...
eventEnd, ...
response, ...
responseTime);
%% Close the port
audio_config = triggerSend('close', device, audio_config);
% Take the total exp time to printout
Experiment_duration = GetSecs - experimentStartTime;
%% Save a mat Log file
% Onsets & durations are saved in seconds.
save(fullfile(pwd, 'output', ['logFileFull_', SubjName, '_run-' Run,'_case-n-' expLength,'.mat']));
save(fullfile(pwd, 'output', ['logFile_', SubjName, '_run-' Run,'_case-n-' expLength,'.mat']), ...
'names', 'onsets', 'durations', 'ends', 'responseTime', ...
'responseKey', 'Experiment_duration', 'playTime','timeLogger');
%close the logfile
fclose(fid);
%take the last time
expTime = toc;
%% print the duration of the exp
fprintf('\nSequence IS OVER!!\n');
fprintf('\n==================\n\n');
fprintf('\nyou have tested %d trials for STATIC and %d trials for MOTION conditions\n\n', ...
(numEvents-numTargets)/2, (numEvents-numTargets)/2);
fprintf('\nexp. duration was %f minutes\n\n', expTime/60);