使用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中吸引人的一些特性
Apr 09 Python
Python连接DB2数据库
Aug 27 Python
python+django快速实现文件上传
Oct 24 Python
Python抓取手机号归属地信息示例代码
Nov 28 Python
今天 平安夜 Python 送你一顶圣诞帽 @微信官方
Dec 25 Python
Python列表对象实现原理详解
Jul 01 Python
Flask框架重定向,错误显示,Responses响应及Sessions会话操作示例
Aug 01 Python
Python 网络编程之UDP发送接收数据功能示例【基于socket套接字】
Oct 11 Python
python中p-value的实现方式
Dec 16 Python
Pytorch中.new()的作用详解
Feb 18 Python
对python中return与yield的区别详解
Mar 12 Python
Python结合Window计划任务监测邮件的示例代码
Aug 05 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
基于PHP实现堆排序原理及实例详解
2020/06/19 PHP
ext 同步和异步示例代码
2009/09/18 Javascript
用jQuery技术实现Tab页界面之二
2009/09/21 Javascript
Javascript 面向对象编程(一) 封装
2011/08/28 Javascript
JS完成代码前最好对其做5件事
2013/04/07 Javascript
jQuery事件之键盘事件(ctrl+Enter回车键提交表单等)
2014/05/11 Javascript
jquery中ajax使用error调试错误的方法
2015/02/08 Javascript
Jquery实现鼠标移动放大图片功能实例
2015/03/25 Javascript
使用AOP改善javascript代码
2015/05/01 Javascript
jQuery的基本概念与高级编程
2015/05/14 Javascript
Jquery幻灯片特效代码分享--打开页面随机选择切换方式(3)
2015/08/15 Javascript
JS实现的表格行鼠标点击高亮效果代码
2015/11/27 Javascript
jQuery向父辈遍历的简单方法
2016/09/18 Javascript
jquery实现的简单轮播图功能【适合新手】
2018/08/17 jQuery
JavaScript一元正号运算符示例代码
2019/06/30 Javascript
vue数据响应式原理知识点总结
2020/02/16 Javascript
微信小程序间使用navigator跳转传值问题实例分析
2020/03/27 Javascript
iview实现动态表单和自定义验证时间段重叠
2021/01/10 Javascript
Tornado服务器中绑定域名、虚拟主机的方法
2014/08/22 Python
django利用request id便于定位及给日志加上request_id
2018/08/26 Python
python 字符串只保留汉字的方法
2018/11/16 Python
python虚拟环境迁移方法
2019/01/03 Python
10行Python代码计算汽车数量的实现方法
2019/10/23 Python
python实现简单学生信息管理系统
2020/04/09 Python
解决pycharm安装第三方库失败的问题
2020/05/09 Python
torchxrayvision包安装过程(附pytorch1.6cpu版安装)
2020/08/26 Python
css3 实现圆形旋转倒计时
2018/02/24 HTML / CSS
Html5百叶窗效果的示例代码
2017/12/11 HTML / CSS
3种方式实现瀑布流布局小结
2019/09/05 HTML / CSS
迪梵英国官方网站:Darphin英国
2017/12/06 全球购物
阿玛尼美妆英国官网:Giorgio Armani Beauty英国
2019/03/28 全球购物
Mountain Hardwear官网:攀岩服装和户外装备
2019/09/26 全球购物
保安公司服务承诺书
2014/05/28 职场文书
运动会观后感
2015/06/09 职场文书
公司趣味运动会开幕词
2016/03/04 职场文书
浅谈redis五大数据结构和使用场景
2021/04/12 Redis