how to read EEG data from Python?

wEEtoZwEEtoZ Norway
edited October 2016 in Software
Hi!

I am working with a simple python program to acquire channel data and print it out to console live. But it seems that it does not work. Is it true that all of the 3 bytes per channel has to do with the voltage? Because when I print out channel data and try to blink etc, there is almost no reaction (numbers does not change).

Python Code:
import serial
import binascii
sampNumb = ""
channel_data = [0,0,0,0,0,0,0,0,0]
accel_data = [0,0,0]
gain = 24
Vref = 4.5
# scale_fac_uVolts_per_count = Vref/float((pow(2,23)-1))/gain*1000000.
scale_factor = Vref / (gain * ((2 ** 23) - 1))
i = 0
with serial.Serial('COM4', 115200, timeout = 1,parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE) as ser:
ser.write('b')
while(1):
s = ser.read(1)
if binascii.hexlify(s) == "a0":
sample = ser.read(31)
sampleID = sample[0]
channel_data[1] = sample[1:4] # Data
channel_data[2] = sample[4:7]
channel_data[3] = sample[7:10]
channel_data[4] = sample[10:13]
channel_data[5] = sample[13:16]
channel_data[6] = sample[16:19]
channel_data[7] = sample[19:22]
channel_data[8] = sample[22:25]

accel_data[0] = sample[25:27] # Accel X
accel_data[1] = sample[27:29] # Accel Y
accel_data[2] = sample[29:31] # Accel Z

print(str(int(binascii.hexlify(channel_data[1]), 16) * scale_factor) + "\t" + str(
int(binascii.hexlify(channel_data[2]), 16) * scale_factor))
Any suggestions?

Comments

  • cfausncfausn Rochester NY
    Do you know C? Check out the c driver logic if so. If not, we have a python program that does handle this somewhere on the github.

    What you need to do is create a state machine for the data. The naive approach you're taking (which is also what I did during the C driver development) does not necessarily work, because while starting bytes are guaranteed to be a0, not every byte is guaranteed not to be a0. For example, an a0 byte may be sent in a packet as any other data is, so relying on a0 doesn't work well. Instead you're going to want to break the stream into different aspects of a packet and process data after you see the end byte.

    Check the github for specific examples, and the processing GUI as well. There's a great state machine in the GUI that may help you with the logic. I would also suggest sending a 'v' character when you start just to reset the board and make sure everything is ready to be streamed. Hope that clarified things a bit!
  • wjcroftwjcroft Mount Shasta, CA
    Wee, hi.

    Colin, great points that the framing bytes are allowed in the data.

    Here's some links that show how the existing Python driver can be used. In second case with LSL as well,




    Also the search box in the right column will turn up other posts using Python,


    William

  • Thank you very much for the feedback both of you.

    cfausn: Yes, I know C. _I will try to look at your c driver and see what I can learn from that. But is all of the bytes between A0 and C0 representing the voltage? The signal is pretty much random when it is printed out, and there is no response in the frontal electrodes when blinking (as it is in the original OPENBCI GUI). Is any filtering needed you think? Or do you think that this has to do with the fact that the bytes are read wrong due to the A0 bits in the data?

    wjcroft: Thank you for that. I will look into the LSL tutorial you linked there, seems pretty useful.
  • wjcroftwjcroft Mount Shasta, CA
    @wEEtoZ, you need to remove the millivolt level DC offset to see the waveforms in the microvolt range.


Sign In or Register to comment.