Friday, September 11, 2015

Logbook 9/11

This week I worked on attempting to graph the incoming data from the TGAM brain chip over Bluetooth. The data comes in as a string in the specified format below with values being from 0 to 131072 (I think)

freqs = [single strength, attn, relax, delta, theta, alphaL, alphaH, betaL, betaH, gammaL, gammaH]

The signal strength should be low to indicate a good connection, 200 means there is brain waves getting picked up. I wrote a python script to capture the data (Bluetooth serial connected on COM8 on my computer). The script breaks down the data into an array and the plots the values.






Attention and relaxation values I believe are the most important. These are computed within the chip itself from the values of the following frequency bands. For a more accurate or fine-tuned approach, we could monitor the frequency bins as well. The values of attn. and relaxation have a smaller range, 256 being the max, therefore the other values are plotted off the visible area.

For some reason, at times when a certain frequency value goes to high, python prints “ERROR: packet too long” rather than the actual data. To get rid of this messing things up, the script looks to see if the letter ‘E’ is in the incoming data string first. If it is, it just ignores it. No good data should ever have the letter E in it since the TGAM chip only sends a string of numbers and commas.





#Python Analyzing Program
import serial
import numpy as np
import time
from matplotlib import pyplot as plt
ser = serial.Serial('COM8', 9600)

plt.ion() # set plot to animated

line = [None] * 10
ydata = [0] * 50
attn = [0] * 50
relax = [0] * 50
delta = [0] * 50
theta = [0] * 50
alphaL = [0] * 50
alphaH = [0] * 50
betaL = [0] * 50
betaH = [0] * 50
gammaL = [0] * 50
gammaH = [0] * 50

freqs = [attn, relax, delta, theta, alphaL, alphaH, betaL, betaH, gammaL, gammaH]
shades = ["#800000", "#000066","#66FFFF", "#66CCFF", "#99CCFF", "#9999FF", "#CC66FF" , "#FF66FF", "#FF3399", "#FF0066"]
ax1=plt.axes()

# make plot
i=0;
for x in freqs:
    line[i], = plt.plot(x)
    plt.ylim([10,40])
    i+=1;

# start data collection
while True:
    data = ser.readline(); # read data from serial
                                   # port and strip line endings

    if(data.find('E')==-1):
        print data
        data = data.split(",");
        if data[0] != 200:
            ymin = 10;
            ymax = 200
            plt.ylim([ymin,ymax])
            j = 0;
            for f in freqs:
                if(j==0 or j==1):
                    line[j].linestyle='-';
                f.append(data[j+1])
                del f[0]
                line[j].c = shades[j]
                line[j].set_xdata(np.arange(len(f)))
                line[j].set_ydata(f)  # update the data
                j+=1;
            plt.draw() # update the plot

            print "receieved"
            time.sleep(.5) 





No comments:

Post a Comment