Skip to content

Commit bdce04c

Browse files
committed
add old main script to be refactored
1 parent a19fb11 commit bdce04c

File tree

1 file changed

+331
-0
lines changed

1 file changed

+331
-0
lines changed

ALocalizer.m

Lines changed: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
1+
%function mirror_runMTLocalizer
2+
clear all;
3+
clc
4+
5+
%8 trials = 370 sec (without trigger) =6.17 minutes
6+
% if TR =2 Sec. 370 sec / 2 (TR) = 185 TRs (without trigger) + 4 = 189 TRs
7+
% if TR =2.5 Sec. 370 sec / 2 (TR) = 148 TRs (without trigger) + 4 = 152 TRs
8+
9+
%% To correct for the y-axis problem inside the scanner
10+
% where the lower 1/3 of the screen is not appearing because of coil indicate which device the script is running on, on PC, the middle of the
11+
% y axis will be the middle of the screen, on the Scanner, the middle of y-axis will be the middle of the upper 2/3 of the screen, because the
12+
% lower 1/3 is not visible due to the coil in the scanner.
13+
device = 'Scanner';
14+
%device = 'PC';
15+
16+
fprintf('Connected Device is %s \n\n',device);
17+
18+
% Original Script Written by Sam Weiller to localize MT+/V5
19+
% Adapted by M.Rezk to localize MT/MST (Huk,2002)
20+
%% Start me up
21+
% Get the subject Name
22+
SubjName = input('Subject Name: ','s');
23+
if isempty(SubjName)
24+
SubjName = 'trial';
25+
end
26+
27+
fprintf('Auditory MT Localizer \n\n')
28+
29+
%% Experiment Parametes
30+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31+
initial_wait = 5; % seconds to have a blank screen at the beginning, the scans will be discarded until % the magnetic field is homogenous
32+
finalWait = 5;
33+
%blockDur = 16; % Block duration [should be a multiple of osc (below)]
34+
ISI = 0.1; % Interstimulus Interval between events in the block.
35+
ibi = 6; % Inter-block duration in seconds (time between blocks)
36+
nrBlocks = 14; % Number of trials , where 1 block = 1 block of all conditions (static and motion)
37+
numEventsPerBlock = 12;
38+
range_targets = [0 2]; % range of number of targets in each block (from 2 to 5 targets in each block)
39+
40+
mirror_width= 11.5; % Width (x-axis) of the mirror (in cm)
41+
v_dist = 14; % viewing distance from the mirror (cm) "in this script we use mirror"
42+
fix_r = 0.15; % radius of fixation point (deg)
43+
44+
%Audiofile_duration = 16 ; % Length of the Audio file (in seconds)
45+
%Stop_audiofile = blockDur ; % Let the audio file play for x Seconds
46+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 1 Cycle = one inward and outward motion together
47+
%% Experimental Design
48+
% function "experimental_design" while assign the blocks, conditions, and
49+
% the number of targets that will be used in the motion localizer
50+
%[names,targets,condition] = experimental_design(nrBlocks,range_targets);
51+
[names,targets,condition,directions,isTarget] = experimental_design(nrBlocks,numEventsPerBlock,range_targets) ;
52+
53+
numBlocks = length(names); % Create a variable with the number of blocks in the whole experiment
54+
55+
56+
%% InitializePsychAudio;
57+
InitializePsychSound(1);
58+
59+
[soundData, freq] = loadAudioFiles(SubjName);
60+
phandle = PsychPortAudio('Open',[],[],1,freq,2);
61+
%PsychPortAudio('FillBuffer',phandle,soundData_static);
62+
%fprintf('\nstatic wav file loaded. \n')
63+
64+
%% PTB Setup
65+
screenNumber = max(Screen('Screens'));
66+
%screenNumber = 0;
67+
%Screen('Preference', 'SkipSyncTests', 2);
68+
[w, winRect, xMid, yMid] = startPTB(screenNumber, 1, [128 128 128]);
69+
HideCursor;
70+
71+
%% Color indeces, and Screen parameters and inter-flip interval.
72+
% Color indices
73+
white = WhiteIndex(screenNumber);
74+
black = BlackIndex(screenNumber);
75+
grey = ceil((white+black)/2);
76+
77+
% Flip interval and screen size
78+
ifi = Screen('GetFlipInterval', w); % Get the flip interval
79+
[tw, th] = Screen('WindowSize', w);
80+
81+
%nframes = floor(blockDur/ifi);
82+
% while mod(nframes,2)~=0 % make sure the nframes are even number
83+
% nframes = nframes-1; % to be able to re-assign dots in the static condition (to perform divison calculation)
84+
% end
85+
86+
%% Welcome screen
87+
Screen('TextFont',w, 'Courier New');
88+
Screen('TextSize',w, 20);
89+
Screen('TextStyle', w, 1);
90+
DrawFormattedText(w,'Press for FASTER sound \n\n\n(static or motion)',...
91+
'center', 'center', black);
92+
Screen('Flip', w);
93+
[~, ~, ~]=KbCheck;
94+
KbWait;
95+
Screen('Flip', w);
96+
97+
WaitSecs(0.25);
98+
99+
% DrawFormattedText(w,'The experiment is about to begin','center', 'center', black);
100+
% Screen('Flip', w);
101+
% [KeyIsDown, pend, KeyCode]=KbCheck;
102+
%
103+
% KbWait;
104+
% Screen('Flip', w);
105+
106+
%% FUNCTION
107+
if strcmp(device,'PC')
108+
DrawFormattedText(w,'Waiting For Trigger',...
109+
'center', 'center', black);
110+
Screen('Flip', w);
111+
112+
% press key
113+
KbWait();
114+
KeyIsDown=1;
115+
while KeyIsDown>0
116+
[KeyIsDown, ~, ~]=KbCheck;
117+
end
118+
119+
% open Serial Port "SerPor" - COM1 (BAUD RATE: 11520)
120+
elseif strcmp(device,'Scanner')
121+
DrawFormattedText(w,'Waiting For Trigger','center', 'center', black);
122+
Screen('Flip', w);
123+
SerPor = MT_portAndTrigger;
124+
Screen('Flip', w);
125+
end
126+
127+
%% Experiment Start (Main Loop)
128+
experimentStartTime = GetSecs;
129+
130+
%% To correct for the y-axis problem inside the scanner
131+
if strcmp(device,'Scanner')
132+
adjusted_yAxis = 2/3*th; % where the lower 1/3 of the screen is not appearing because of coil
133+
elseif strcmp(device,'PC')
134+
adjusted_yAxis = th; % y-axis is the same, no changes
135+
end
136+
137+
%% Pixels per degree
138+
[mirrorPixelPerDegree] = mirror2Pixels (winRect,v_dist,mirror_width) ; % Calculate pixel per degree on the mirror surface
139+
140+
%% fixation coordiates
141+
fix_cord = [[tw/2 adjusted_yAxis/2]-fix_r*mirrorPixelPerDegree [tw/2 adjusted_yAxis/2]+fix_r*mirrorPixelPerDegree];
142+
143+
%% Experiment start
144+
% The experment will wait (initial_wait) Secs before running the stimuli
145+
Screen('FillOval', w, uint8(white), fix_cord); % draw fixation dot (flip erases it)
146+
blank_onset=Screen('Flip', w);
147+
WaitSecs('UntilTime', blank_onset + initial_wait);
148+
149+
targetTime = [];
150+
responseKey = [];
151+
responseTime = [];
152+
153+
eventOnsets=zeros(numBlocks,numEventsPerBlock);
154+
eventEnds=zeros(numBlocks,numEventsPerBlock);
155+
eventDurations=zeros(numBlocks,numEventsPerBlock);
156+
157+
responsesPerBlock=zeros(numBlocks,1);
158+
159+
playTime = zeros(numBlocks,1);
160+
161+
for blocks = 1:numBlocks
162+
163+
timeLogger.block(blocks).startTime = GetSecs - experimentStartTime; % Get the start time of the block
164+
timeLogger.block(blocks).condition = condition(blocks); % Get the condition of the block (motion or static)
165+
timeLogger.block(blocks).names = names(blocks); % Get the name of the block
166+
167+
responseCount=0;
168+
169+
for iEvent = 1: numEventsPerBlock
170+
171+
Sound=[];
172+
173+
if isTarget(blocks,iEvent)==0
174+
175+
if strcmp(directions(blocks,iEvent),'S')
176+
Sound= soundData.S;
177+
elseif strcmp(directions(blocks,iEvent),'U')
178+
Sound= soundData.U;
179+
elseif strcmp(directions(blocks,iEvent),'D')
180+
Sound= soundData.D;
181+
elseif strcmp(directions(blocks,iEvent),'R')
182+
Sound= soundData.R;
183+
elseif strcmp(directions(blocks,iEvent),'L')
184+
Sound= soundData.L;
185+
end
186+
187+
elseif isTarget(blocks,iEvent)==1
188+
189+
if strcmp(directions(blocks,iEvent),'S')
190+
Sound= soundData.S_T;
191+
elseif strcmp(directions(blocks,iEvent),'U')
192+
Sound= soundData.U_T;
193+
elseif strcmp(directions(blocks,iEvent),'D')
194+
Sound= soundData.D_T;
195+
elseif strcmp(directions(blocks,iEvent),'R')
196+
Sound= soundData.R_T;
197+
elseif strcmp(directions(blocks,iEvent),'L')
198+
Sound= soundData.L_T;
199+
end
200+
201+
end
202+
203+
eventOnsets(blocks,iEvent)=GetSecs-experimentStartTime;
204+
205+
PsychPortAudio('FillBuffer',phandle,Sound);
206+
playTime(blocks,1) = PsychPortAudio('Start',phandle);
207+
208+
%length(playedAudio)/freq
209+
210+
while GetSecs() <= eventOnsets(blocks,iEvent)+ experimentStartTime + (length(Sound)/freq)
211+
212+
if strcmp(device,'Scanner')
213+
[sbutton,secs] = TakeSerialButton(SerPor);
214+
%[sbutton,secs] = MT_TakeSerialButtonPerFrame(SerPor);
215+
%responseKey(end+1)= sbutton;
216+
if sbutton~= 0
217+
responseTime(end+1)= secs - experimentStartTime;
218+
219+
%%%%%%%%%%%%%%%%%%%%%
220+
% while you are pressing, wait till it is
221+
% released
222+
while sbutton ~= 0
223+
[sbutton,secs]= TakeSerialButton(Cfg.SerPor);
224+
end
225+
%%%%%%%%%%%%%%%%%%%%%
226+
227+
responseCount = responseCount + 1;
228+
end
229+
230+
231+
232+
elseif strcmp(device,'PC')
233+
234+
[keyIsDown, secs, ~ ] = KbCheck();
235+
236+
if keyIsDown
237+
responseTime(end+1)= secs - experimentStartTime;
238+
while keyIsDown ==1
239+
[keyIsDown , ~] = KbCheck();
240+
end
241+
242+
responseCount = responseCount + 1;
243+
end
244+
end
245+
246+
end
247+
248+
eventEnds(blocks,iEvent)=GetSecs-experimentStartTime;
249+
eventDurations(blocks,iEvent)=eventEnds(blocks,iEvent)-eventOnsets(blocks,iEvent);
250+
251+
WaitSecs(ISI);
252+
253+
end
254+
255+
responsesPerBlock (blocks,1) = responseCount ;
256+
257+
%% Get Block end and duration
258+
timeLogger.block(blocks).endTime = GetSecs - experimentStartTime; % Get the time for the block end
259+
timeLogger.block(blocks).length = timeLogger.block(blocks).endTime - timeLogger.block(blocks).startTime; %Get the block duration
260+
261+
%% Fixation cross and inter-block interval
262+
Screen('FillOval', w, uint8(white), fix_cord); % draw fixation dot (flip erases it)
263+
blank_onset=Screen('Flip', w);
264+
WaitSecs('UntilTime', blank_onset + ibi); % wait for the inter-block interval
265+
266+
end;
267+
268+
% At the end of the blocks wait ... secs before ending the experiment.
269+
Screen('FillOval', w, uint8(white), fix_cord); % draw fixation dot (flip erases it)
270+
blank_onset=Screen('Flip', w);
271+
WaitSecs('UntilTime', blank_onset + finalWait);
272+
273+
274+
%% Save the results ('names','onsets','ends','duration') of each block
275+
names = cell(length(timeLogger.block),1);
276+
onsets = zeros(length(timeLogger.block),1);
277+
ends = zeros(length(timeLogger.block),1);
278+
durations = zeros(length(timeLogger.block),1);
279+
280+
for i=1:length(timeLogger.block)
281+
names(i,1) = timeLogger.block(i).names;
282+
onsets(i,1) = timeLogger.block(i).startTime;
283+
ends(i,1) = timeLogger.block(i).endTime;
284+
durations(i,1) = timeLogger.block(i).length;
285+
end
286+
287+
%% KeyPresses and Times
288+
% for i=length(responseKey):-1:2 % responseKey gives a '1' in all frames where button was pressed, so one motor response = gives multiple consequitive '1' frames
289+
% if responseKey(i-1)~=0 % therefore, we need to cancel consequitive '1' frames after the first button press
290+
% responseKey(i)=0; % we loop through the responses and remove '1's that are not preceeded by a zero
291+
% responseTime(i)=0; % this way, we remove the additional 1s for the same button response
292+
% end % - The same concept for the responseTime
293+
% end
294+
%
295+
% for i=length(targetTime):-1:2 % The same concept as responseKey adn responseTime.
296+
% if targetTime(i-1)~=0 % Our Targets lasts 3 frames, to remove the TargetTime for the 2nd and 3rd frame
297+
% targetTime(i)=0; % we remove targets that are preceeded by a non-zero value
298+
% end % that way, we have the time of the first frame only of the target
299+
% end
300+
%
301+
% responseKey = responseKey(responseKey > 0); % Remove zero elements from responseKey, responseTime, & targetTime
302+
% responseTime = responseTime(responseTime > 0);
303+
% targetTime = targetTime(targetTime > 0);
304+
305+
%% Shutdown Procedures
306+
ShowCursor;
307+
clear screen;
308+
myTotalSecs=GetSecs;
309+
Experiment_duration = myTotalSecs - experimentStartTime;
310+
311+
%% Save a mat Log file
312+
% Onsets & durations are saved in seconds.
313+
save(['logFileFull_',SubjName,'.mat']);
314+
save(['logFile_',SubjName,'.mat'], 'names','onsets','durations','ends','targets','responseTime','responseKey','targetTime','Experiment_duration','playTime');
315+
316+
317+
%% FUNCTION
318+
% close Serial Port ---- VERY IMPORTANT NOT FORGET
319+
if strcmp(device,'Scanner')
320+
CloseSerialPort(SerPor);
321+
end
322+
323+
catch
324+
clear Screen;
325+
fprintf('Code was catched!')
326+
%% Close serial port of the scanner IF CRASH OF THE CODE
327+
if strcmp(Cfg.device,'Scanner')
328+
CloseSerialPort(Cfg.SerPor);
329+
end
330+
331+
end

0 commit comments

Comments
 (0)