使用python接受tgam的脑波数据实例


Posted in Python onApril 09, 2020

废话不多说,来看看实例吧!

# -*- coding: utf-8 -*-
import serial 
 
filename='yjy.txt' 
t = serial.Serial('COM5',57600)
b=t.read(3)
vaul=[]
i=0
y=0
p=0
while b[0]!=170 or b[1]!=170 or b[2]!=4:
 b=t.read(3)
 print(b)
if b[0]==b[1]==170 and b[2]==4:
 a=b+t.read(5)
 print(a)
 if a[0] == 170 and a[1]==170 and a[2]==4 and a[3]==128 and a[4]==2: 
 while 1:
  i=i+1
#  print(i)
  a=t.read(8)
#  print(a)
  sum=((0x80+0x02+a[5]+a[6])^0xffffffff)&0xff
  if a[0]==a[1]==170 and a[2]==32:
  y=1
  else:
  y=0
  if a[0] == 170 and a[1]==170 and a[2]==4 and a[3]==128 and a[4]==2:
  p=1
  else:
  p=0
  if sum!=a[7] and y!=1 and p!=1:
   print("wrroy1")
   b=t.read(3)
   c=b[0]
   d=b[1]
   e=b[2]
   print(b)
   while c!=170 or d!=170 or e!=4:
   c=d
   d=e
   e=t.read()
   print("c:")
   print(c)
   print("d:")
   print(d)
   print("e:")
   print(e)
   if c==(b'\xaa'or 170) and d==(b'\xaa'or 170) and e==b'\x04':
    g=t.read(5)
    print(g)
    if c == b'\xaa' and d==b'\xaa' and e==b'\x04' and g[0]==128 and g[1]==2: 
    a=t.read(8)
    print(a)
    break
   
#  if a[0]==a[1]==170 and a[2]==4:
  # print(type(a))
  
  if a[0] == 170 and a[1]==170 and a[2]==4 and a[3]==128 and a[4]==2:
  high=a[5]
  low=a[6]
#  print(a)
  rawdata=(high<<8)|low 
  if rawdata>32768:
   rawdata=rawdata-65536
#  vaul.append(rawdata)
  sum=((0x80+0x02+high+low)^0xffffffff)&0xff
  if sum==a[7]:
   vaul.append(rawdata)
  if sum!=a[7]:
   print("wrroy2")
   b=t.read(3)
   c=b[0]
   d=b[1]
   e=b[2]
#   print(b)
   while c!=170 or d!=170 or e!=4:
   c=d
   d=e
   e=t.read()
   if c==b'\xaa' and d==b'\xaa' and e==b'\x04':
    g=t.read(5)
    print(g)
    if c == b'\xaa' and d==b'\xaa' and e==b'\x04' and g[0]==128 and g[1]==2: 
    a=t.read(8)
    print(a)
    break
  if a[0]==a[1]==170 and a[2]==32:
  c=a+t.read(28)
  print(vaul)
  print(len(vaul))
  for v in vaul:
   w=0
   if v<=102:
   w+=v
   q=w/len(vaul)
   q=str(q)
   with open(filename,'a') as file_object:
    file_object.write(q)
    file_object.write("\n")
   if 102<v<=204:
   w+=v
   q=w/len(vaul)
   q=str(q)
   with open(filename,'a') as file_object:
    file_object.write(q)
    file_object.write("\n")
   if 204<v<=306:
   w+=v
   q=w/len(vaul)
   q=str(q)
   with open(filename,'a') as file_object:
    file_object.write(q)
    file_object.write("\n")
   if 306<v<=408:
   w+=v
   q=w/len(vaul)
   q=str(q)
   with open(filename,'a') as file_object:
    file_object.write(q)
    file_object.write("\n")
   if 408<v<=510:
   w+=v
   q=w/len(vaul)
   q=str(q)
   with open(filename,'a') as file_object:
    file_object.write(q)
    file_object.write("\n")
#  print(c)
  vaul=[]
#  if i==250:
#  break
#  with open(filename,'a') as file_object:
#   file_object.write(q)
#   file_object.write("\n")

补充知识:Python处理脑电数据:PCA数据降维

pca.py

#!-coding:UTF-8-
from numpy import *
import numpy as np

def loadDataSet(fileName, delim='\t'):
 fr = open(fileName)
 stringArr = [line.strip().split(delim) for line in fr.readlines()]
 datArr = [map(float,line) for line in stringArr]
 return mat(datArr)

def percentage2n(eigVals,percentage):
 sortArray=np.sort(eigVals) #升序
 sortArray=sortArray[-1::-1] #逆转,即降序
 arraySum=sum(sortArray)
 tmpSum=0
 num=0
 for i in sortArray:
 tmpSum+=i
 num+=1
 if tmpSum>=arraySum*percentage:
  return num

def pca(dataMat, topNfeat=9999999):
 meanVals = mean(dataMat, axis=0)
 meanRemoved = dataMat - meanVals #remove mean
 covMat = cov(meanRemoved, rowvar=0)
 eigVals,eigVects = linalg.eig(mat(covMat))
 eigValInd = argsort(eigVals)  #sort, sort goes smallest to largest
 eigValInd = eigValInd[:-(topNfeat+1):-1] #cut off unwanted dimensions
 redEigVects = eigVects[:,eigValInd] #reorganize eig vects largest to smallest
 lowData_N = meanRemoved * redEigVects#transform data into new dimensions
 reconMat_N = (lowData_N * redEigVects.T) + meanVals
 return lowData_N,reconMat_N

def pcaPerc(dataMat, percentage=1):
 meanVals = mean(dataMat, axis=0)
 meanRemoved = dataMat - meanVals #remove mean
 covMat = cov(meanRemoved, rowvar=0)
 eigVals,eigVects = linalg.eig(mat(covMat))
 eigValInd = argsort(eigVals)  #sort, sort goes smallest to largest
 n=percentage2n(eigVals,percentage)
 n_eigValIndice=eigValInd[-1:-(n+1):-1]
 n_eigVect=eigVects[:,n_eigValIndice]
 lowData_P=meanRemoved*n_eigVect
 reconMat_P = (lowData_P * n_eigVect.T) + meanVals
 return lowData_P,reconMat_P

readData.py

import matplotlib.pyplot as plt
from pylab import *
import numpy as np
import scipy.io as sio
def loadData(filename,mName):
 load_fn = filename
 load_data = sio.loadmat(load_fn)
 load_matrix = load_data[mName]
 #load_matrix_row = load_matrix[0]

 #figure(mName)
 #plot(load_matrix,'r-')
 #show()

 #print type(load_data)
 #print type(load_matrix)
 #print load_matrix_row
 return load_matrix

main.py

#!-coding:UTF-8
import matplotlib.pyplot as plt
from pylab import *
import numpy as np
import scipy.io as sio
import pca
from numpy import mat,matrix
import scipy as sp
import readData
import pca

if __name__ == '__main__':
 A1=readData.loadData('6electrodes.mat','A1')
 lowData_N, reconMat_N= pca.pca(A1,30)
 lowData_P, reconMat_P = pca.pcaPerc(A1,0.95)
 #print lowDMat
 #print reconMat
 print shape(lowData_N)
 print shape(reconMat_N)
 print shape(lowData_P)
 print shape(reconMat_P)

以上这篇使用python接受tgam的脑波数据实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python简单猜数游戏实例
Jul 09 Python
Python爬豆瓣电影实例
Feb 23 Python
python梯度下降法的简单示例
Aug 31 Python
python调用虹软2.0第三版的具体使用
Feb 22 Python
Pycharm+Python+PyQt5使用详解
Sep 25 Python
使用 Python ssh 远程登陆服务器的最佳方案
Mar 06 Python
django 外键创建注意事项说明
May 20 Python
tensorflow模型转ncnn的操作方式
May 25 Python
python Matplotlib模块的使用
Sep 16 Python
Flask处理Web表单的实现方法
Jan 31 Python
使用Selenium实现微博爬虫(预登录、展开全文、翻页)
Apr 13 Python
pandas中DataFrame重置索引的几种方法
May 24 Python
解决使用python print打印函数返回值多一个None的问题
Apr 09 #Python
Python 实现自动完成A4标签排版打印功能
Apr 09 #Python
python网络编程:socketserver的基本使用方法实例分析
Apr 09 #Python
Python使用扩展库pywin32实现批量文档打印实例
Apr 09 #Python
python3 自动打印出最新版本执行的mysql2redis实例
Apr 09 #Python
python实现处理mysql结果输出方式
Apr 09 #Python
python读取配置文件方式(ini、yaml、xml)
Apr 09 #Python
You might like
URL Rewrite的设置方法
2007/01/02 PHP
PHP自定session保存路径及删除、注销与写入的方法
2014/11/18 PHP
smarty模板引擎中自定义函数的方法
2015/01/22 PHP
PHP微信公众号自动发送红包API
2016/06/01 PHP
php版微信自定义回复功能示例
2016/12/05 PHP
php实现的二分查找算法示例
2017/06/20 PHP
php设计模式之策略模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
用于判断用户注册时,密码强度的JS代码
2009/01/01 Javascript
jquery 获取表单元素里面的值示例代码
2013/07/28 Javascript
jQuery 插件开发指南
2014/11/14 Javascript
JS实现表单中checkbox对勾选中增加边框显示效果
2015/08/21 Javascript
JavaScript File API文件上传预览
2016/02/02 Javascript
JQUERY的AJAX请求缓存里的数据问题处理
2016/02/23 Javascript
使用JQuery实现智能表单验证功能
2016/03/08 Javascript
详述JavaScript实现继承的几种方式(推荐)
2016/03/22 Javascript
JavaScript判断页面加载完之后再执行预定函数的技巧
2016/05/17 Javascript
当jquery ajax遇上401请求的解决方法
2016/05/19 Javascript
JS实现的幻灯片切换显示效果
2016/09/07 Javascript
JS实现课堂随机点名和顺序点名
2017/03/09 Javascript
Vue自定义图片懒加载指令v-lazyload详解
2020/12/31 Javascript
vue文件运行的方法教学
2019/02/12 Javascript
微信小程序批量监听输入框对按钮样式进行控制的实现代码
2019/10/12 Javascript
vue自定义指令限制输入框输入值的步骤与完整代码
2020/08/30 Javascript
使用Python发送邮件附件以定时备份MySQL的教程
2015/04/25 Python
Python中使用haystack实现django全文检索搜索引擎功能
2017/08/26 Python
Python实现将HTML转换成doc格式文件的方法示例
2017/11/20 Python
Python中的单继承与多继承实例分析
2018/05/10 Python
Python八皇后问题解答过程详解
2019/07/29 Python
python实现字典嵌套列表取值
2019/12/16 Python
PyTorch 对应点相乘、矩阵相乘实例
2019/12/27 Python
Python列表倒序输出及其效率详解
2020/03/04 Python
金鑫耀Java笔试题
2014/09/06 面试题
大学生自我鉴定范文
2013/12/28 职场文书
纪检干部现实表现材料
2014/08/21 职场文书
经贸日语专业自荐信
2014/09/02 职场文书
新年祝酒词大全
2015/08/11 职场文书