Professional Documents
Culture Documents
A Simple Framework For Behavioral Experiments PDF
A Simple Framework For Behavioral Experiments PDF
Jens Schwarzbach
Trento University
2
What is ASF?
ASF stands for A Simple Framework for presenting visual and auditory stimuli in
behavioral experiments. It is based on the Psychophysics Toolbox for Matlab
www.psychtoolbox.org, which needs to be installed for ASF to run. The idea is to write a
program that reduces the experimenter’s task of programming behavioral experiments to
generating tables of stimulus- and trial descriptions while accurate timing of stimuli,
collecting responses from different devices such as voice-keys, button boxes or a
computer mouse, generating triggers for EEG/MEG equipment and synchronization with
MR-scanners and response-logging is dealt with in the background. While this little list
of tasks makes already clear that the name ASF becomes more and more of a misnomer I
hope that it makes experimenting simpler and that it is easy to use.
This document has two parts, one for users who want to run experiments with minimal
programming using ASF as a kind of accurate slide show player, and one part for users
who want to program customized experiments taking advantage of tasks already
operational in ASF. For example, if you have a set of pictures that you want to show with
high temporal accuracy and collect participant’s responses, part one shows you how to do
that. If you want to program an experiment with some complex rendering, e.g. moving
stimuli, but you do not want to reinvent the wheel in terms of how to link with an MR
scanner and different response devices, have a look at part two.
The ASF-Practitioner's guide
1.2 Demo
This section describes a simple framework (ASF) for running behavioral experiments
running using the Psychtoolbox for Matlab.
ASF needs four things from you:
1. a textfile that contains the names of all image files that will be displayed during
the experiment (stimulus-file), the file extension is .std
2. a textfile that contains trial by trial information about what image should be
shown and for how long (trial-file), the file extension is .trd
3. a name for a file that will hold the results (result-file), and
4. a structure Cfg that contains settings that you wish to apply to the program (e.g.
about your display).
An experiment using the Matlab Psychtoolbox is invoked by typing:
[expinfo] = ASF(stimulusfile, trialfile, expname, Cfg)
img_3.bmp
You can also write absolute path names into the stimulus-file:
c:\experiments\experiment1\images\img_1.bmp
c:\experiments\experiment1\images\img_2.bmp
c:\experiments\experiment1\images\img_3.bmp
The recommended option is to use relative path-descriptions. If the images reside in a
subdirectory of the current working directory with the name 'images', then you can put a
relative path-description to each file into the stimulus-file, for example:
.\images\img_1.bmp
.\images\img_2.bmp
.\images\img_3.bmp
In our simple experiment a trial consists of showing each of the three pictures above for
one second and measure reaction time starting from the second picture. Note that picture-
duration is referred to in frames whose duration depends on the refresh rate of your
monitor. Let us assume you have a 100Hz monitor. This means that each picture in your
experiment has a duration of 100 frames. On different trials you want to show the
pictures in different order.
2
The ASF-Practitioner's guide
measurement starts. Note: Consider using the encode() function described in section 2.2.8
for assigning codes to trials.
code Time P1 PD1 P2 PD2 P3 PD3 P4 PD4 P5 PD5 P6 PD6 P7 PD7 StartRT Corr.
Resp
1 2 1 30 2 30 3 1 2 4 3 1 2 90 1 1 5 1
2 4 1 30 2 30 3 1 2 4 4 1 2 90 1 1 5 3
3 6 1 30 2 30 4 1 2 4 4 1 2 90 1 1 5 3
4 8 1 30 2 30 4 1 2 4 3 1 2 90 1 1 5 3
3
The ASF-Practitioner's guide
4
The ASF-Practitioner's guide
1.5.4 Timing
- Cfg.useTrialOnsetTimes [ {0} | 1 ]
By default, trials follow each other as fast as possible. Setting this parameter to 1
means that the second column of a trial definition determines the time with respect to
the start of the experiment at which a given trial is started. This feature can be used
for temporal jittering of trials in event-related fMRI studies.
1.5.5 Feedback
- Cfg.feedbackTrialCorrect [ {0} | 1 ]
Beep after a correct response.
- Cfg.feedbackTrialError [ {0} | 1 ]
Beep after an incorrect response.
- Cfg.onlineFeedback [ {0} | 1 ]
Provides trial by trial and cumulative statistics. Particularly useful when using
dualscreen displays. Requires statistics toolbox.
5
The ASF-Practitioner's guide
ASFShowTrialSample.m for your purposes. In order to ASF calling your code and
not its internal one you need to provide a pointer to this function in the Cfg-structure,
in this case by typing ‘Cfg.userSuppliedTrialFunction = @ASFShowTrialSample.m’.
The ASF-Programmer’s Guide below gives more information about that.
- Cfg.userDefinedTRDcolumns [ {0} | nColumns ]
Users can supply additional information in a trial-definition file that they can use in
their own trial function. For example you may want to add two columns per trial
which tell your trialfunction where to show a bitmap (default is at the center of the
screen). These columns are inserted after column 2.
The following example shows a possible trial definition file in which stimuli are
shown at four different positions on the screen ([-100, 0; 0 100; 100, 0; 0, -100]).
Note that if you want to show stimuli at different positions you also have to write
your own stimulus presentation routine (see Cfg.userSuppliedTrialFunction above).
6
1 2 -100 0 1 100 2 100 3 100 2
2 4 0 100 1 100 3 100 2 100 2
3 6 100 0 2 100 1 100 3 100 2
4 8 0 -100 2 100 3 100 1 100 2
5 10 -100 0 3 100 1 100 2 100 2
6 12 0 100 3 100 2 100 1 100 2
1.5.7 Miscellaneous
- Cfg.randomizeTrials [ {0} | 1 ]
Trialwise randomization of the TRD-file. Not really a recommended feature since it
can interfere with other features such as trial-onset times (see below) or
counterbalancing of conditions. It is recommended that the order of trial presentations
is managed by the program or person which or who creates the TRD-files.
6
The ASF-Practitioner's guide
o 'synchToScannerPort'
o 'useTrialOnsetTimes'
o 'feedbackTrialCorrect'
o 'feedbackTrialError'
o 'Task'
o 'Fs'
o 'userSuppliedTrialFunction'
o 'userDefinedSTMcolumns'
o 'stimNames'
o 'trialFileName'
o 'pathstr'
o 'name'
o 'ext'
o 'version'
o 'frameRate'
o 'monitorFlipInterval'
o 'getFlipInterval'
o 'Environment'
o 'nTrials'
o 'experimentStart'
- trialinfo: A structure array that contains trial by trial information.
o trialinfo.trial: the description for a given trial as read in from the trial-file
o trialinfo.datestr: date and time when the trial has been shown
o trialinfo.response: which key has been pressed and respective reaction
time of the trial
o trialinfo.timing: not yet documented
o trialinfo.startRTMeasurement: not yet documented
o trialinfo.endRTMeasurement: not yet documented
Find below a piece of code that puts the results of your experiment into an easy-to-handle
matrix format.
function res = read_expinfo(expinfo)
%PACK DATA FROM EXPINFO INTO A MATRIX
%ROW -> trial
%COL 1: CODE
%COL 2: RT
%COL 3: KEY
nTrials = length(expinfo.TrialInfo);
res(nTrials, 3) = 0;
for i=1:nTrials
res(i, 1) = expinfo.trialinfo(i).trial.code;
res(i, 2) = expinfo.trialinfo(i).response.RT;
res(i, 3) = expinfo.trialinfo(i).response.key;
end
7
The ASF-Practitioner's guide
Use the function decode() described in section 2.2.8 to retrieve the factorial combination
of any given trial.
Figure 1: Typical masked priming experiments show primes and masks at different SOAs and
response congruencies. Researchers investigate whether presenting a masked stimulus can
nevertheless influence reaction times to a target stimulus, which is in this case the mask itself (see e.g.
Vorberg et al., 2003). Timing is provided in milliseconds and corresponding number of frames on a
60Hz display.
Material and Procedures: Stimuli are presented on a 60 Hz display, i.e. stimulus
duration is quantized in steps of one frame duration (1/60th of a second or 16.6667 ms).
Each trial begins with showing a fixation cross at the center of the screen for 30 frames
(WarningPeriod). Primes are shown above fixation for one frame (PrimeDuration).
Primes are either neutral, pointing to the left or to the right. Stimulus onset asynchrony
(SOA) varies between zero and five frames. From that follows that for SOAs shorter than
PrimeDuration there is a period of temporal overlap of prime and mask (Overlap = SOA-
PrimeDuration). If SOA equals PrimeDuration there is a direct succession of the prime by
a mask. If SOA is longer than PrimeDurationthere is variable interstimulus interval (ISI)
between prime an mask in which only the fixation cross is shown (ISI = SOA-
PrimeDuration). The mask is shown altogether for 6 frames (MaskDuration). Masks
appear centered around the same location as the prime and they either point to the left or
to the right. After extinguishing the mask there is a fixation cross for 90 frames
(ResponsePeriod) which is terminated either by a participant's response or when
Response period has expired. Between trials there is a fixed inter trial interval (ITI) of
500ms. Reaction time measurement starts when presenting the mask.
8
The ASF-Practitioner's guide
The following figure depicts a trial with prime and mask pointing to the left (i.e.
congruent trial) with an SOA of 83 ms (i.e. 5 frames).
A B
Picture 1: Picture 2:
Empty.bmp Fix.bmp
Picture 3: Picture 4:
PrimeL.bmp MaskL.bmp
Figure 2: A) Detailed sequence of frames in a congruent trial with an SOA of 83ms between prime
and mask. B) Pictures used in this trial.
Sequence of events (pages) in this trial:
• Empty screen for 30 frames
• Fixation cross for 30 frames
• Prime (e.g. arrow left) and fixation cross for 1 frame
• fixation cross for 4 frames
• Mask (e.g. arrow left) and fixation cross for 5 frames
• Empty screen for 90frames
• Reaction time measurement starts with the onset of the mask
the functionality of ASF by writing your own equivalent of ShowTrial that has the same
interface, .ie. same input and output arguments, but other functionality. The following
section describes how you can enhance stimulus presentation from pure visual
presentations to combined audiovisual presentations.
10
The ASF-Programmer's guide
2.1 Introduction
2.1.1 Online help
"help Psychtoolbox" provides a general overview. Detailed help is provided by typing
"help <function_name>" e.g. "help Screen". More focused help on how to use a specific
parameter, e.g. the OpenWindow parameter of the function Screen is provided by
"Screen('OpenWindow?')". The website www.psychtoolbox.org gives you the latest
information.
Figure 3: Screens in the PTB. A) Single screen setup. Screen 1 is the primary diplay. B) Dual-screen
setup. If you have multiple displays attached to your computer, screen 2 is the secondary display and
so forth. Screen 0 is all your displays next to each other. For example, in a multi-display setup you
can use Screen 1 for programming and Screen 2 for displaying your stimulus. If you only have one
screen, the stimulus will at least partially occlude your progrm window, whjich can become difficult
for debugging.
Typically you will create a window on each screen that you are using in your
experiment. An onscreen window contains a backbuffer and a frontbuffer. The
backbuffer is where all drawing operations are performed i.e. where you build/prepare
your stimulus for presentation. The frontbuffer is displayed at the display refresh interval.
After you have finished drawing and your stimulus image is ready in the backbuffer, you
issue the Screen('Flip',...) command. This command asks PTB to switch the role of the
front- and back- buffer at the next vertical retrace of your display device. This way, the
former backbuffer becomes the new frontbuffer and your new stimulus gets shown to the
subject. The former frontbuffer becomes the new backbuffer and is ready for drawing the
next stimulus on.
11
The ASF-Programmer's guide
%-----------------------------------------------------------------
%NOW YOU CAN START DRAWING ONTO THE BACK-BUFFER, WHICH WILL NOT
% BECOME VISIBLE UNTIL YOU FLIP IT TO THE FOREGROUND
%-----------------------------------------------------------------
12
The ASF-Programmer's guide
%all the graphics operations only become visible once you "flip"
% the backbuffer to the foreground
%get help by typing on the command line: Screen('Flip?')
Screen('Flip', windowPtr, [], 1);
%-----------------------------------------------------------------
%SHUTDOWN OF ALL WINDOWS CREATED BY THE PSYCHOPHYSICS TOOLBOX
%-----------------------------------------------------------------
%Close all open onscreen and offscreen windows and textures
Screen('CloseAll');
catch
% This "catch" section executes in case of an error in the "try"
% section above.
% Importantly, it closes the onscreen window if it's open.
Screen('CloseAll');
%display the last error message
psychrethrow(psychlasterror);
end
2.1.2.1.1 Tasks
Change the program that it creates its window in the upper right of the screen using one
quarter of your display.
Hint: You can determine the size of your display by typing
ScreenDim = get(0, 'ScreenSize') on the Matlab command line. You will get a vector that
contains [upperLeftRow, upperLeftColumn, width, height]
13
The ASF-Programmer's guide
%-----------------------------------------------------------------
%NOW YOU CAN START DRAWING ONTO THE BACK-BUFFER, WHICH WILL NOT
% BECOME
%VISIBLE UNTIL YOU FLIP IT TO THE FOREGROUND
%-----------------------------------------------------------------
14
The ASF-Programmer's guide
%read image
image_matrix = imread('.\images\pic_001.jpg');
%all the graphics operations only become visible once you "flip"
% the %backbuffer to the foreground
%get help by typing on the command line: Screen('Flip?')
Screen('Flip', windowPtr, [], 1);
%-----------------------------------------------------------------
%SHUTDOWN OF ALL WINDOWS CREATED BY THE PSYCHOPHYSICS TOOLBOX
%-----------------------------------------------------------------
%Close all open onscreen and offscreen windows and textures
Screen('CloseAll');
catch
% This "catch" section executes in case of an error in the "try"
% section above.
%Importantly, it closes the onscreen window if it's open.
Screen('CloseAll');
%display the last error message
psychrethrow(psychlasterror);
end
15
The ASF-Programmer's guide
2.1.2.2.1 Showing all images in a directory for one second and record mouse
buttons as responses
This program is the Psychophysics Toolbox version of the homework assignment from
session 4
• Suppose you have n images on your harddisk (image_1.bmp...image_n.bmp).
• Write a program that displays each image for 1s and that collects a mouse
response
16
The ASF-Programmer's guide
end
%fill the entire window with the default background color
Screen('FillRect', windowPtr);
17
The ASF-Programmer's guide
%-----------------------------------------------------------------
%SHUTDOWN OF ALL WINDOWS CREATED BY THE PSYCHOPHYSICS TOOLBOX
%-----------------------------------------------------------------
%Close all open onscreen and offscreen windows and textures
Screen('CloseAll');
%-----------------------------------------------------------------
%PLOT REACTION TIMES
%-----------------------------------------------------------------
figure
plot(vertcat(response.time), 'o-')
xlabel('TRIAL')
ylabel('RT [ms]')
catch
% This "catch" section executes in case of an error in the "try"
%section above.
% Importantly, it closes the onscreen window if it's open.
Screen('CloseAll');
%display the last error message
psychrethrow(psychlasterror);
end
18
The ASF-Programmer's guide
Figure 4: Typical masked priming experiments show primes and masks at different SOAs and
response congruencies. Researches investigate whether presenting a masked stimulus can
nevertheless influence reaction times to an target stimulus, which is in this case the mask itself (see
e.g. Vorberg et al., 2003). Timing is provided in milliseconds and corresponding number of frames on
a 60Hz display.
Material and Procedures: Stimuli are presented on a 60 Hz display, i.e. stimulus
duration is quantized in steps of one frame duration (1/60th of a second or 16.6667 ms).
Each trial begins with showing a fixation cross at the center of the screen for 30 frames
(WarningPeriod). Primes are shown above fixation for one frame (PrimeDuration).
Primes are either neutral, pointing to the left or to the right. Stimulus onset asynchrony
(SOA) varies between zero and five frames. From that follows that for SOAs shorter than
PrimeDuration there is a period of temporal overlap of prime and mask (Overlap = SOA-
PrimeDuration). If SOA equals PrimeDuration there is a direct succession of the prime by
a mask. If SOA is longer than PrimeDurationthere is variable interstimulus interval (ISI)
between prime an mask in which only the fixation cross is shown (ISI = SOA-
PrimeDuration). The mask is shown altogether for 6 frames (MaskDuration). Masks
appear centered around the same location as the prime and they either point to the left or
to the right. After enxtinguishing the mask there is a fixation cross for 90 frames
(ResponsePeriod) which is terminated either by a participant's response or when
Response period has expired. Between trials there is a fixed inter trial interval (ITI) of
500ms. Reaction time measurement starts when presenting the mask.
The following figure depicts a trial with prime and mask pointing to the left (i.e.
congruent trial) with an SOA of 83 ms (i.e. 5 frames).
19
The ASF-Programmer's guide
A B
Picture 1: Picture 2:
Empty.bmp Fix.bmp
Picture 3: Picture 4:
PrimeL.bmp MaskL.bmp
Figure 5: A) Detailed sequence of frames in a congruent trial with an SOA of 83ms between prime
and mask. B) Pictures used in this trial.
Sequence of events (pages) in this trial:
• Empty screen for 30 frames
• Fixation cross for 30 frames
• Prime (e.g. arrow left) and fixation cross for 1 frame
• fixation cross for 4 frames
• Mask (e.g. arrow left) and fixation cross for 5 frames
• Empty screen for 90frames
• Reaction time measurement starts with the onset of the mask
20
The ASF-Programmer's guide
The following code implements the playback lop from above and measures reaction time.
Note that the functions ShowImage() and WaitMs() and CheckWhenButtonWasPressed()
have yet to be defined.
nPages = length(trial.pagenumber);
for p = 1:nPages
ShowImage(img(trial.pagenumber, :, :))
if (p == trial. StartRTonPage)
t0 = GetSecs; %GetSecs is a PTB function
end
WaitFrames(trial.pageduration(p))
end
t1 = CheckWhenButtonWasPressed; %returns time of button press
Trial.RT = t1-t0;
The following code reades the images and puts them onto textures (using PTB-functions):
function [stimnames, tex] = read_stimuli(windowPtr, fname_stim)
%function [stimnames, tex] = read_stimuli(windowPtr, fname_stim)
21
The ASF-Programmer's guide
%READ STIMULUS-NAMES
stimnames = importdata(fname_stim);
nStimuli = size(stimnames, 1);
end
close(h)
22
The ASF-Programmer's guide
The description for an entire experiment can thus be stored in a textfile that contains n
lines for n trials.
counter = 0;
while 1
counter = counter + 1;
aline = fgetl(fid);
if ~ischar(aline), break, end
fprintf(1, '%s\n', aline)
aline = str2num(aline);
trialdefs(counter).code = aline(1);
trialdefs(counter).pagenumber = aline(2:2:(end-1));
trialdefs(counter).pageduration = aline(3:2:(end-1));
trialdefs(counter).StartRTonPage = aline(end);
end
fclose(fid);
else
%FILE DOES NOT EXIST
errorflag = 1;
error(sprintf('FILE %s NOT FOUND', fname))
end
23
The ASF-Programmer's guide
2.2.8.2 Decoding
Decode is the function that returns the factorial combinations from a code that has been
generated by encode(). It takes the code or a vector of codes and the factorial design as
arguments. The program returns the factorial combinations (in most of the cases this will
be a matrix) for each code.
24
The ASF-Programmer's guide
acode = codes(i);
for factor = size(fac, 2):-1:1
v(i, factor) = fix(acode/prod(fac(1:factor-1)));
acode = acode - v(i, factor)*prod(fac(1:factor-1));
end
end
faccombination = fliplr(v);
25
Release Notes
3 Release Notes
%20070620 ADDED VERSION ENTRY TO Cfg
%20070620 REMOVED "WaitSecs(0.001);" IN WaitForMousePress FUNCTION
%20070622 ADDED VoiceKey functionality
%20070622 ADDED TRIGGER OUTPUT FOR MEG/EEG (requires data
acquisition toolbox)
%20070624 ADDED NONDESTRUCTIVE FLIPPING
%20070710 ADDED SYNCHRONIZATION TO EXTERNAL TRIGGERS
%20070906 ADDED GRACEFUL EXIT WHEN STM or STIMDEF FILES ARE NOT
FOUND
%20070906 ADDED TRIAL ONSET TIMES ARE NOW CODED IN THE SECOND
COLUMN OF STM FILES
% USER CAN DECIDE WHETHER TO USE OR IGNORE THEM BY
% SETTING Cfg.useTrialOnsetTimes FLAG
%20070906 ADDED TRIAL BY TRIAL FEEDBACK: Cfg.feedbackTrialCorrect,
Cfg.feedbackTrialError
%20070906 ADDED GRACEFUL EXIT WHEN PRESSING THE q BUTTON REPEATEDLY
%20070930 ADDED READ TEXT AND SOUND STIMULI
%20071010 FIXED REMOVED ERROR THAT OCCURRED WHEN NO DATA
ACQUISITION TOOLBOX WAS INSTALLED
%20071103 FIXED INCREASED MEMORY EFFICIENCY (ALTHOUGH STILL ROOM
FOR
% IMPROVEMENTS)
%20071128 CHANGED PTB_Init does not return rect directly any more,
the screen rect is now Cfg.Screen.rect
%20071202 CHANGED starting to adjust all variable names according to
mixedCase convention.
% This means, that variables start lower case and
each new word with upper case.
% BUT: structures start with upper case, e.g. Cfg,
Cfg.Screen etc.
% Example: cfg.UseBackBuffer became Cfg.useBackBuffer
% THIS WILL AFFECT PLUGINS WRITTEN BY USERS BASED ON
% PREVIOUS TEMPLATES
%20071203 ADDED KEYBOARD as possible response device
%20071203 ADDED FEEDBACK: COMMAND WINDOW SHOWS RT AND KEY FOR EACH
TRIAL
26
Release Notes
27
Release Notes
Index
Cfg.feedbackTrialCorrect ..................... 5 Cfg.userDefinedTRDcolumns .............. 5
Cfg.feedbackTrialError ......................... 5 Cfg.userSuppliedTrialFunction ............. 5
Cfg.issueTriggers .................................. 4 Cfg.useTrialOnsetTimes ....................... 4
Cfg.onlineFeedback .............................. 5 Cfg.waitUntilResponseAfterTrial ......... 4
Cfg.plotVOT ......................................... 4 dual-screen ............................................ 3
Cfg.randomizeTrials ............................. 5 event-related fMRI ................................ 4
Cfg.responseTerminatesTrial ................ 4 Matlab data acquisition toolbox ............ 4
Cfg.Screen.rect ...................................... 3 primary display ..................................... 3
Cfg.synchToScanner ............................. 4 secondary display .................................. 3
Cfg.SynchToScannerPort...................... 4 ShowTrial() ........................................... 5
Cfg.useBackBuffer................................ 3 temporal jittering of trials ..................... 4
cfg.userDefinedTRDcolumns ............... 5
28