220 likes | 518 Views
Matlab: Cogent & External Devices Friederike Schüür 18th March 2010 Course Material: http://www.icn.ucl.ac.uk/ Cogent Software: http://www.vislab.ucl.ac. uk/cogent.php. ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices. Initialising expt. variables.
E N D
Matlab: Cogent & External Devices Friederike Schüür 18th March 2010 Course Material: http://www.icn.ucl.ac.uk/ Cogent Software: http://www.vislab.ucl.ac.uk/cogent.php ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
Initialising expt. variables Configuration of devices start_cogent • Run experiment • load premade stimulus files • create new stimuli • trial loop: • - present stimuli • - get user responses • - save data as you go • save final data & close files stop_cogent Basic script overview: recap In the previous sessions we covered: • General structure of cogent scrips • Presentation of stimuli • Visual • Sound • Registration of keyboard & mouse events • Logging & saving of data • Communication with external devices! ICN Matlab for Cognitive Neuroscience 2009 /10 – Presenting Stimuli with Cogent
Today’s lecture: Ext. devices General Usage: • Using a button box • Synchronization with external devices • MRI scanner • Check sound or display synchronization • Sending triggers to: • TMS • EEG serial port ... Configuration of devices + some cogent commands parallel port ... Configuration of devices + some cogent commands ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
What’s a serial port? • A serial port is a serial communication interface. • Information transfers in or out one bit at a time. • Largely obsolete in modern computers (too bad for us ...). Diagrammatic oscilloscope trace of voltage levels for an uppercase ASCII “K” character with 1 start bit, 8 data bits, and one stop bit (RS-232). ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
Configuration of the serial port Configuration: config_serial(port, baudrate, parity, stopbits, bytesize) port: - COM port number (1,2,3,4, etc.) baudrate: - (110, 300, 600, ..., 128000, 256000) parity: - (0 = none, 1 = odd, 2 = even, 3 = mark, 4 = space) stopbits: - (0 = 1, 1 = 1.5, 2 = 2) bytesize: - (4 bits, 8 bits) Default setting: config_serial(1, 9600, 0, 0, 8) which is used if config_serial is used without input arguments. ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
“Jargon” of the serial port config_serial(port, baudrate, parity, stopbits, bytesize) port: Number of the serial port you’re going to use. baudrate: Number of symbols transmitted per second (similar to bit rate). parity: Simple form of error detection in data transmission. E.g. if you choose even parity, then parity bit set to 1 is nr. of 1s is odd in data transmission making number of transmitted 1s even. If nr. of transmitted 1s then odd: error! (mark parity = bit always 1, space parity = bit always 0). stopbits: Allow receiving signal hardware to detect the end of a character. bytesize: Number of bits per byte. Default setting: config_serial(1, 9600, 0, 0, 8) which is used if config_serial is used without input arguments. ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
logserialbytes(port) ----------------------------------------------------------------------- logs & time stamps all serial bytes that have been read by last readserialbytes. Saved in a logfile if configuered. clearserialbytes(port) ----------------------------------------------------------------------- empties the buffer for serial port number Serial port: button box readserialbytes(port) ----------------------------------------------------------------------- reads all bytes arriving at serial port 1 & empties the buffer (clearserialbytes(port)) ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent
Serial port: button box waitserialbyte(port, time, b) ----------------------------------------------------------------------- waits a time (‘time’ in ms) for a serial byte with value b to arrive at serial port number ‘port’. Executes readserialbytes and logserialbytes. All serial bytes are logged, and the buffer is emptied. If time ‘time’ elapsed before b has arrived, then the script continues. [k t n] = getserialbyte(port) ----------------------------------------------------------------------- returns values and times of serial bytes read by readserialbytes ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent
Loop round 10 x 15sec trials, get button presses Depends on specific button box, usually: 0, 1, 2, 4, 6. %Correct %defining variables trial_no = 10; trial_dur = 15000; clearserialbytes(1); %empty buffer for p = 1:trial_no; wait(trial_dur) % allow some time to pass readserialbytes(1); [k t n] = getserialbytes(1); % update & extract bytes keys(p).k = k; keys(p).t = t; end; Example: button box ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent
Ensure that stimulus computer & external device are in synchrony. Example: Synchronization • Incoming pulses generated by the external device can be used for synchronization. • The stimulus computer waits until the signal is registered. • Commands used for this operation are: • clearserialbytes(1) • waitserialbytes(1,inf, byte_to_wait_for). • Once the byte_to_wait_for has been received, the script continues. ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent
with the FIL MRI scanner. Example: Synchronization Depends on serial port (getslice reads serial port) use code available at: http://www.fil.ion.ucl.ac.uk/local/mri/home/ericf/cogent2000.html [slices, times] = getslice( port ) % returns most recent slice number with time stamps. logslice %transfers scanner slices read by getslice to the log file [s, t] = waitslice( port, n ) %waits forever for MRI slice n or greater, returns actual slice number and time stamp. ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent
Check sound or display synchronization Example: Sound or Display • Serial port can also send signals to other devices, e.g. oscilloscopes. • For this, you need a special cable (see Cogent user manuals for instructions or ask Martin! ). • You can generate a signal using: • cgsignal (port, signal) • Signal = signal code (0/1/2/3/4 etc.) • or • sendserialbytes (port, byte) • byte needs to be between 0 – 255. ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent
What’s a parallel port? • Interface for connecting peripherals. • Transmits particular amounts of bits (e.g. 8) in parallel at the same time. Problems with accessing the parallel port: For security reasons, accessing is parallel port is blocked on most “newer computers”. However, you can open up the ports by e.g. using Userport which is included in the cogent download. UserPort.sys is a driver that gives access to the I/O Ports. Needs to be installed once on each computer you use. ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
Essential links For .dll files for parallel port communication: http://www.fil.ion.ucl.ac.uk/local/mri/home/ericf/cogent2000.html For problems with I/O access: http://www.beyondlogic.org/porttalk/porttalk.htm ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
“Configuration” of the parallel port Parallel ports usually have decimal addresses: e.g. 888, 632, or 956. Configuration: startportb(port) e.g. starportb(888) Note: startportb might not be necessary, depending on your parallel port access (the one exception for configuration ... !) Controlling parallel port input: k = inportb(port) Returns decimal values of bytes arriving at port ‘port. Controlling parallel port output: outportb(port, num). Send the number ‘num’ to specified port. ‘Num’ between 0 - 255. ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
Sending triggers to TMS / EEG Use outportb(port,num); wait(t_ms); outportb(port,0); to send a t_ms long pulse. If parallel port of stimulus computer (sending) is connected to an EEG setup (receiving) then num determines the pulse ID in an EEG trace. If particular bits sent by the parallel port send pulses to different external devices, e.g. to trigger data recording or a TMS machine, then num will determine which device receives the trigger. This allows triggering multiple devices from within a Matlab Cogent Script. Check carefully for delays! Problems in the past! ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
Sending triggers to TMS / EEG Use outportb(port,num); wait(t_ms); outportb(port,0); num is a number between 0 and 255. It’s meaning is clear once it’s transformed into binary code. • e.g. 4 corresponds to ‘100’ send bit 2 with this command, you would send a pulse to the devices connected to bit 2 (note, count from right, the first zero corresponds to bit 0). This way you can send pulses to one or multiple devices at the same time. ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
Example: paired pulse TMS Use outportb(port,num); wait(t_ms); outportb(port,0); • Bit 0 to TMS machine 1 • Bit 1 TMS machine 2 • Bit 2 trigger DAQ computer ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
Example: paired pulse TMS % Trigger TMS 1000ms after stimulus presentation. Start data recording after % 100ms before TMS trigger TMS.dly = 1000; TMS.intl = 2; TMS.pre_rec = 100; % Define times cgdrawsprite(1,0,0,0); % draw stimulus in buffer cgflip(0,0,0); % present stimulus t_present = time; % get time of stimulus presentation waituntil(t_present + TMS.dly - TMS.pre_rec); %wait until start recording outportb(888,4);wait(1);outportn(888,0); %start recording (4 = 100b) ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
Example: paired pulse TMS waituntil(t_present + TMS.dly); %wait until first TMS pulse outportb(888,2);wait(1);outportn(888,0); %1st TMS pulse (2 = 10b) waituntil(t_present + TMS.dly + TMS.intvl); %wait until 2nd TMS pulse outportb(888,1);wait(1);outportn(888,0); %2nd TMS pulse ( 1 = 1b) Important: Use waituntil (not wait) otherwise timing inaccuracies. ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
More advanced communication Not with the commands introduced … Use the Matlab Data Acquisition Toolbox. Very flexible use of parallel & serial ports and other interfaces. ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices
That’s it for this time ... Next session: Sam Schwartzkopf about alternatives to Cogent for stimulus presentation: Psychophysics toolbox. ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices