Testing Stimulus Presentation Program using Emulated Scanner Pulses

If you fancy Psychophysics toolbox to present stimuli during an fMRI experiment, you will need to know exactly at which time point the volume acquisition occurs with respect to important events of the experiment, such as for example stimulus onsets. 

Scanners usually send pulses and these are needed to be recorded by the stimulus computer in order to solve this problem. In the specific setup I am using, these pulses are collected in a circuit, called cogent-box, and accumulated. This cumulated count is sent to the serial port of the stimulus computer in the form of 2 bytes. The stimulus program can now correctly time events from different sources e.g. stimulus onsets and the start of volume acquisitions.

The downside is generally that one would need to go to the scanner's console room and run few dummy experiments with the scanner until the presentation code is devoid of any bugs.

However, it would be much nicer to test the stimulus presentation program in the office without the need of having a scanner sending pulses. Therefore some labs have a mobile version of the cogent box with the aim of testing your presentation program without the need of having a scanner to send pulses. But even then, today many modern computers do not have serial ports, making impossible the use such mobile boxes. 


A software solution can circumvent many of the problems associate to a mobile box. In a modern computer, one may create any arbitrary number of virtual ports (Serial or Parallel), read and write data to and from these ports. There are softwares that let you specify any number of virtual ports easily, for example "Virtual Serial Port Deriver". 

Once you have this software you can create a pair of virtual serial port, say COM1 and COM2. You can fire the Matlab, use COM2  and write information in the form of two bytes (to emulate what the cogent-box would be doing in response to pulses received from the scanner at each TR). These two bytes imitates the situation where the cogent-box is counting the number of volumes that have been so far acquired. In another instance of Matlab, running in parallel to the first one, you can set COM1 and read data from it. This will be analogous to the situation you are in the scanner room, without the need of a mobile cogent box or a scanner.

The following psychophysics toolbox function can be used to send pulses.


function SendPulses(N,TR)
%function SendPulses(N)
%
%Sends N pulses to COM2 serial port. TR is the interpulse TR (that is
%the "TR").
%
%Selim Onat, 24-Jan-2013 10:55:53

IOPort('CloseAll')
box.port = IOPort('OpenSerialPort', 'COM2');
counter  = 0;
%
t        = GetSecs;%now
t        = t:TR:t+TR*N;%time of the future pulses
for n = 1:N
    tobesent = [floor(n./255) n];%two byte to be sent
    WaitSecs('untiltime',t(n));%wait and fire.
    [~,t_]=IOPort( 'Write' , box.port , [char(tobesent(1))+1,char(tobesent(2))]);
    counter = counter +1;
    fprintf('Pulse No: %3d sent at %0.10g was due to at %0.10g\n',counter,t_,t(n));
    drawnow%give some time for Control-C
end