22
ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices 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 Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

Embed Size (px)

Citation preview

Page 1: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices

Matlab: Cogent & External DevicesFriederike Schüür

18th March 2010 Course Material: http://www.icn.ucl.ac.uk/

Cogent Software: http://www.vislab.ucl.ac.uk/cogent.php

Page 2: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

ICN Matlab for Cognitive Neuroscience 2009 /10 – Presenting Stimuli with Cogent

Initialising expt. variablesInitialising expt. variables

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

Configuration of devices

Basic script overview: recapIn 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!

Page 3: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices

Today’s lecture: Ext. devices• Using a button box

• Synchronization with external devices

• MRI scanner

• Check sound or display synchronization

• Sending triggers to:

• TMS

• EEG

serial port ...

parallel port ...

Configuration of devices

Configuration of devices

+ some cogent commands

+ some cogent commands

General Usage:

Page 4: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

4

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).

Page 5: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

5

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.

Page 6: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

6

ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices

“Jargon” of the serial portconfig_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.

Page 7: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent

readserialbytes(port)-----------------------------------------------------------------------

reads all bytes arriving at serial port 1 & empties the buffer (clearserialbytes(port))

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

Page 8: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent

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.

Serial port: button box

[k t n] = getserialbyte(port)-----------------------------------------------------------------------

returns values and times of serial bytes read by readserialbytes

Page 9: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

9

ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent

%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 boxLoop round 10 x 15sec trials, get button presses

Depends on specific button box, usually:0, 1, 2, 4, 6.

Page 10: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

10

ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent

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.

Example: Synchronization

Ensure that stimulus computer & external device are in synchrony.

Page 11: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

11

ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent

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.

Example: Synchronizationwith the FIL MRI scanner.

Page 12: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

12

ICN Matlab for Cognitive Neuroscience 09 /10: Recording, logging & saving with Cogent

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.

Example: Sound or DisplayCheck sound or display synchronization

Page 13: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

13

ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices

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.

Page 14: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

14

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

Page 15: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

15

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.

Page 16: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

16

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!

Page 17: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

17

ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices

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.

Sending triggers to TMS / EEG

Page 18: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

18

ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices

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

Example: paired pulse TMS

Page 19: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

19

ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices

% 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)

Example: paired pulse TMS

Page 20: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

20

ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices

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.

Example: paired pulse TMS

Page 21: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

21

ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices

Not with the commands introduced …

Use the Matlab Data Acquisition Toolbox.

Very flexible use of parallel & serial ports and other interfaces.

More advanced communication

Page 22: ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices Matlab: Cogent & External Devices Friederike Schüür 18th March 2010

22

ICN Matlab for Cognitive Neuroscience 2009 /10 – Matlab: Cogent & External devices

Next session:

Sam Schwartzkopf about alternatives to Cogent for stimulus presentation:

Psychophysics toolbox.

That’s it for this time ...