-
Notifications
You must be signed in to change notification settings - Fork 0
/
fMRS_experiment_Nov9edit.m~
259 lines (197 loc) · 6.54 KB
/
fMRS_experiment_Nov9edit.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
% fMRS Experiment
% Clear the workspace
close all;
clear all;
sca;
nBlocks = 6; % number of stim + rest blocks
stimTime = 10; % time in seconds
restTime = 10; % time of rest block in seconds
finalRestTime = 3; % length of last rest block in seconds
% ** ***** I ADDED THIS WHICH MAKES THE TIMING VERY INACCURATE ****
Screen('Preference', 'SkipSyncTests', 1);
% Here we call some default settings for setting up Psychtoolbox
PsychDefaultSetup(2);
% Get the screen numbers
screens = Screen('Screens');
% Draw to the external screen if avaliable
screenNumber = max(screens);
% Define black and white
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
grey = white / 2;
crosshairTimer = 12;
% Open an on screen window
[window, windowRect] = PsychImaging('OpenWindow', screenNumber,...
grey, [], 32, 2, [], [], kPsychNeed32BPCFloat);
% Get the size of the on screen window
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
% Query the frame duration
ifi = Screen('GetFlipInterval', window);
% this is the crosshair part
% Set up alpha-blending for smooth (anti-aliased) lines
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
% Setup the text type for the window
Screen('TextFont', window, 'Ariel');
Screen('TextSize', window, 36);
% Get the centre coordinate of the window
[xCenter, yCenter] = RectCenter(windowRect);
% Here we set the size of the arms of our fixation cross
fixCrossDimPix = 40;
% Keyboard info
fiveKey = KbName('5');
enterKey = KbName('ENTER');
% Now we set the coordinates (these are all relative to zero we will let
% the drawing routine center the cross in the center of our monitor for us)
xCoords = [-fixCrossDimPix fixCrossDimPix 0 0];
yCoords = [0 0 -fixCrossDimPix fixCrossDimPix];
allCoords1 = [xCoords; yCoords];
xCoords = [-fixCrossDimPix fixCrossDimPix -fixCrossDimPix fixCrossDimPix ];
yCoords = [ fixCrossDimPix -fixCrossDimPix -fixCrossDimPix fixCrossDimPix];
allCoords2 = [xCoords; yCoords];
% Set the line width for our fixation cross
lineWidthPix = 8;
% Screen resolution in Y
screenYpix = windowRect(4);
% Number of white/black circle pairs
rcycles = 8;
% Number of white/black angular segment pairs (integer)
tcycles = 12;
% Now we make our checkerboard pattern
xylim = 2 * pi * rcycles;
[x, y] = meshgrid(-xylim: 2 * xylim / (screenXpixels - 1): xylim,...
-xylim: 2 * xylim / (screenXpixels - 1): xylim);
at = atan2(y, x);
checks = ((1 + sign(sin(at * tcycles) + eps)...
.* sign(sin(sqrt(x.^2 + y.^2)))) / 2) * (white - black) + black;
circle = x.^2 + y.^2 <= xylim^2;
%checks = circle .* checks + grey * ~circle; % this line makes the image
% circular
% Now we make this into a PTB texture
radialCheckerboardTexture(1) = Screen('MakeTexture', window, checks);
radialCheckerboardTexture(2) = Screen('MakeTexture', window, 1 - checks);
% Time we want to wait before reversing the contrast of the checkerboard
checkFlipTimeSecs = 1/7.85;
checkFlipTimeFrames = round(checkFlipTimeSecs / ifi);
frameCounter = 0;
% Time to wait in frames for a flip
waitframes = 1;
% Texture cue that determines which texture we will show
textureCue = [1 2];
% Display an intro screen
% Draw text in the middle of the screen in Courier in white
Screen('TextSize', window, 80);
Screen('TextFont', window, 'Courier');
DrawFormattedText(window, 'The Experiment will begin now, press any key to start', 'center', 'center', white);
% Flip to the screen
Screen('Flip', window);
KbStrokeWait;
HideCursor;
stimON = true;
while stimON
tic
% begin with rest block
% Now fill the screen green
Screen('FillRect', window, grey);
% Flip to the screen
Screen('Flip', window);
% Wait for rest time
[keyIsDown,secs, keyCode] = KbCheck;
while keyCode(fiveKey)
[keyIsDown,secs, keyCode] = KbCheck;
if keyCode(enterKey)
stimON = false;
ShowCursor;
sca;
return;
end
if keyIsDown
break;
end
end
toc
% next do a stim block
tic
Screen('DrawLines', window, allCoords1,...
lineWidthPix, [1,0,0], [xCenter yCenter], 2);
% Sync us to the vertical retrace
vbl = Screen('Flip', window);
toc
tic
count =1;
orient =1;
allCoords = allCoords1;
t_flip(count) = toc;
[keyIsDown,secs, keyCode] = KbCheck;
while keyCode(fiveKey)
% check for key being pressed to exit.
[keyIsDown,secs, keyCode] = KbCheck;
if keyCode(enterKey)
stimON = false;
ShowCursor;
sca;
return;
end
if keyIsDown
break;
end
% Increment the counter
frameCounter = frameCounter + 1;
% Draw our texture to the screen
Screen('DrawTexture', window, radialCheckerboardTexture(textureCue(1)));
Screen('DrawLines', window, allCoords,...
lineWidthPix, [1,0,0], [xCenter yCenter], 2);
% Flip to the screen
vbl = Screen('Flip', window, vbl + (waitframes - 0.5) * ifi);
% Reverse the texture cue to show the other polarity if the time is up
if frameCounter == checkFlipTimeFrames
if randi(2,1) ==2 && ((toc-t_flip(count))>crosshairTimer)
if orient == 2
allCoords = allCoords1;
count = count+1;
t_flip(count)= toc;
orient =1;
elseif orient ==1
allCoords = allCoords2;
count = count+1;
t_flip(count)= toc;
orient =2;
end
end
textureCue = fliplr(textureCue);
frameCounter = 0;
end
end
repetition =
if repetition == 10
stimON = false;
end
toc
end
% final rest
% begin with rest block
% Now fill the screen green
Screen('FillRect', window, grey);
% Flip to the screen
Screen('Flip', window);
% Wait for two seconds
for j = 1:finalRestTime
WaitSecs(1);
if KbCheck
ShowCursor;
sca;
return;
end
end
% display an end screen
% Draw text in the middle of the screen in Courier in white
Screen('TextSize', window, 80);
Screen('TextFont', window, 'Courier');
DrawFormattedText(window, 'Game Over!', 'center', 'center', white);
% Flip to the screen
Screen('Flip', window);
KbStrokeWait;
ShowCursor;
% Clear up and leave the building
sca;
close all;
clear all;