-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example Experiment
313 lines (245 loc) · 8.54 KB
/
Example Experiment
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
function BasicPsychtoolobox
% Clear the workspace and the screen
sca;
close all;
clearvars;
% If you are recording with EEG, enter 1
EEG = 0;
% Initializing psychtoolbox
[screens,screenNumber,grey,white,black,window] = initPsychtoolbox();
% Initializing EEG
if EEG
[BIOSEMI,BIO_LPT_ADDRESS] = initBIO_h();
end
% Setting the triggers for EEG
stimOnsetTrigger = 100;
fixationOnsetTrigger = 150;
% Loading a blank image of the same dimension as the stimuli of the
% experiment
theImageLocation = 'C:\Users\alexander.lepauvre\Pictures\diodeTest.png';
theImage = imread(theImageLocation);
% Make the image into a texture
imageTexture = Screen('MakeTexture', window, theImage);
% Displazing the instructions, telling the experimenter what they should do
% to measure the delays
displayInstructions(window)
% If you are recording with the EEG, send a trigger informing of the start
% of the experiment
if EEG
sendTrig(1,BIOSEMI,BIO_LPT_ADDRESS);
end
% This is the presentation loop. 100 iterations. For each iteration a white
% picture will be presented for 1.5 second, then only the fixation will be
% presented for 0.5sec.
for i = 1 : 10
% Draw the image to the screen, unless otherwise specified PTB will draw
% the texture full size in the center of the screen. We first draw the
% image in its correct orientation.
Screen('DrawTexture', window, imageTexture, [], [], 0);
drawFixation(window)
% Flip to the screen
Screen('Flip', window);
if EEG
sendTrig('100',biosemi,LPT_address);
end
% Getting the time of the begining of the trial. Trial begins as soon
% as the picture is presented
beginTrial = GetSecs;
% The time elapsed correspond to the time we are at, minus the time we
% started measuring
elapsedTime = GetSecs - beginTrial;
% As long as the elapsed time is inferior to 2 seconds, the trial
% continues
while elapsedTime < 2
% Once 1.5 msec have passed, diplay the fixation only
if elapsedTime>= 1.5
% Fill the screen with the grey color
Screen('FillRect', window,grey);
% Draw the fixation
drawFixation(window)
% Flip to the screen
Screen('Flip', window);
% If you are using EEG, send the trigger for the end of
% stimulus presentation
if EEG
sendTrig('150',biosemi,LPT_address);
end
end
% Actualize the elapsed Time counter
elapsedTime = GetSecs - beginTrial;
end
end
% Sending the end trigger to the EEG
if EEG
sendTrig(TRG_MINIBLOCK_ENDED,BIOSEMI,BIO_LPT_ADDRESS);
saveTrigToHD();
end
% Now we are done with the trials, we can save teh EEG triggers to the hard
% drive and terminate the EEG recording
if EEG
end
% Clear the screen.
safeExit(EEG,BIOSEMI,BIO_LPT_ADDRESS)
% Ideally here, I would have something loading the EEG data and analyze
% them directly to get the delays.
end
function [screens,screenNumber,grey,white,black,window] = initPsychtoolbox()
% Here we call some default settings for setting up Psychtoolbox
PsychDefaultSetup(2);
% Setting the sync tests: SkipSyncTests set to 0 because we want the sync
% tests to be performed
Screen('Preference', 'SkipSyncTests', 0);
% Setting the level of visual debug. In other word, this lets you decide
% how much of the warnings sent by Psychtoolbox you want to see. Set to 4,
% the most thorough: we want everything psychtoolbox sends to be sent.
Screen('Preference', 'VisualDebugLevel', 4);
% Get the screen numbers. This gives us a number for each of the screens
% attached to our computer.
screens = Screen('Screens');
% To draw we select the maximum of these numbers. So in a situation where we
% have two screens attached to our monitor we will draw to the external
% screen.
screenNumber = max(screens);
% Define black and white (white will be 1 and black 0). This is because
% in general luminace values are defined between 0 and 1 with 255 steps in
% between. All values in Psychtoolbox are defined between 0 and 1
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
% But here, we are setting the gray to be fitting our experiment:
grey = [0.5 0.5 0.5];
% Open an on screen window using PsychImaging and color it grey.
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);
% Getting the dimensions of the window
ScreenWidth = windowRect(3);
ScreenHeight = windowRect(4);
center = [ScreenWidth/2; ScreenHeight/2];
% Getting the frame rate
hz = Screen('NominalFrameRate', window);
disp(hz);
refRate = hz.^(-1);
% Setting the text preferences: text renderer 1 is for HQ, TextEncodingLocale UTF-8
Screen('Preference', 'TextRenderer', 1);
Screen('Preference','TextEncodingLocale','UTF-8');
% this enables us to use the alpha transparency
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA', [grey 128]);
% Setting the priority level to maximal for the screen on which we will
% present
priorityLevel=MaxPriority(window);
Priority(priorityLevel);
%% Text parameters
fontType = 'David';
Screen('TextFont',window, fontType);
Screen('TextStyle', window, 0);
%Screen('TextSize', window, round(fontSize*screenScaler));
%% PRELIMINTY PREPATATION
% check for Opengl compatibility, abort otherwise
AssertOpenGL;
% Do dummy calls to GetSecs, WaitSecs, KbCheck
KbCheck;
WaitSecs(0.1);
GetSecs;
end
% Setting the fixation
function [ ] = drawFixation(w)
Screen('TextSize', w);
FIXATION_COLOR = [0 0 0];
DrawFormattedText(w, double('o'), 'center', 'center', FIXATION_COLOR);
Screen('TextSize', w);
end
function displayInstructions(w)
Screen('TextSize', w);
FIXATION_COLOR = [0 0 0];
DrawFormattedText(w, double('You are about to start. Make sure the diode is sticked to the screen the right way'), 'center', 'center', FIXATION_COLOR);
Screen('TextSize', w);
Screen('Flip', w);
KbWait;
end
%% EEG System
function [BIOSEMI, BIO_LPT_ADDRESS] = initBIO_h()
global trigMatName triggers triggsCounter triggsStart
prf1 = sprintf('%s', date);
trigMatName = sprintf('%s%c%s%c%s_S%d_%s_triggers.mat',pwd,filesep,'data',filesep,'DiodeTest', prf1);
% Doing some preallocation for the triggers
TRIGGER_ARRAY_SIZE = 1000;
% The triggers matrix will contain the different triggers onsets
triggers = nan(TRIGGER_ARRAY_SIZE,2);
% Setting a counter for the triggers
triggsCounter= 0;
% Finding out the moment where we start to record to get the time of the
% other triggers relative to the beining
triggsStart = GetSecs;
% Starting the hardware
try
[BIOSEMI, BIO_LPT_ADDRESS]=init_bio;
catch e
warning('BioSemi connection initiation failed! Triggers will not be sent!');
if ~DEBUG throw(e); end
end
end
%INIT_BIO
% EEG trigger function trigger hardware intilization
function [ biosemi,LPT_address ] = init_bio()
% config_io;
% RB_address = hex2dec('D010');
% LPT_address = hex2dec('0378');
LPT_address = hex2dec('c010');
biosemi=io64;
status=io64(biosemi);
if status
disp ('fail')
biosemi = [];
end
end
% Sending the triggers: this function is sending the triggers
% Input: BIOSEMI and BIO_LPT_ADDRESS are required fpr the program to know
% where to send the triggers, and the trigger code is the code of the
% current trigger
function [] = sendTrig(trigCode,BIOSEMI,BIO_LPT_ADDRESS)
% Setting the global variables: triggsCounter counts how many triggers were
% sent in total, triggsStart gets when the trigger was sent
global triggsCounter triggsStart triggers
try
% This command is sending the trigger through the parallel port
io64(BIOSEMI,BIO_LPT_ADDRESS,trigCode);
% Setting the triggers counter
triggsCounter = triggsCounter + 1;
% Saving the time of the trigger in the triggers matrix
triggers(triggsCounter,2) = GetSecs - triggsStart;
% Waiting a little and then resetting the trigger (setting it back to
% 0) through the parallel port
WaitSecs(0.001);
io64(BIOSEMI,BIO_LPT_ADDRESS,0);
WaitSecs(0.001);
% Adding the trigger code to the triggers matrix.
triggers(triggsCounter,1) = trigCode;
catch %e
warning ('Trigger not sent correctly!');
end
end
% This function saves the triggers to the hard drive
function saveTrigToHD()
global trigMatName triggers
try
save(trigMatName,'triggers');
catch
try
save(trigMatName,'triggers');
catch
end
end
end
%% Safe exit
% This function closes everything that eneds to be closed and so on
function [] = safeExit(EEG,BIOSEMI,BIO_LPT_ADDRESS)
Priority(0);
sca;
ShowCursor;
ListenChar(0);
try
if EEG
saveTrigToHD();
sendTrig(255,BIOSEMI,BIO_LPT_ADDRESS);
end
catch
end
end