使用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处理json数据中的中文
Mar 06 Python
python网络编程示例(客户端与服务端)
Apr 24 Python
Python函数中定义参数的四种方式
Nov 30 Python
为python设置socket代理的方法
Jan 14 Python
python实现根据用户输入从电影网站获取影片信息的方法
Apr 07 Python
Python编程中time模块的一些关键用法解析
Jan 19 Python
python使用socket创建tcp服务器和客户端
Apr 12 Python
Django压缩静态文件的实现方法详析
Aug 26 Python
Python Pexpect库的简单使用方法
Jan 29 Python
python通过txt文件批量安装依赖包的实现步骤
Aug 13 Python
python随机生成大小写字母数字混合密码(仅20行代码)
Feb 01 Python
使用numpy nonzero 找出非0元素
May 14 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
Amazon Prime Video平台《无限住人 -IMMORTAL-》2020年开始TV放送!
2020/03/06 日漫
配置支持SSI
2006/11/25 PHP
PHP取整数函数常用的四种方法小结
2012/07/05 PHP
Linux下实现PHP多进程的方法分享
2012/08/16 PHP
php daddslashes()和 saddslashes()有哪些区别分析
2012/10/26 PHP
PHP获取短链接跳转后的真实地址和响应头信息的方法
2014/07/25 PHP
PHP实现远程下载文件到本地
2015/05/17 PHP
JavaScript中判断页面关闭、页面刷新的实现代码
2014/08/27 Javascript
jQuery实现ajax调用WCF服务的方法(附带demo下载)
2015/12/04 Javascript
谈谈我对JavaScript原型和闭包系列理解(随手笔记6)
2015/12/20 Javascript
jQuery Mobile弹出窗、弹出层知识汇总
2016/01/05 Javascript
JS公共小方法之判断对象是否为domElement的实例
2016/11/25 Javascript
JScript实现表格的简单操作
2017/08/15 Javascript
基于Vue开发数字输入框组件
2017/12/19 Javascript
详解vue-cli 本地开发mock数据使用方法
2018/05/29 Javascript
webpack dll打包重复问题优化的解决
2018/10/10 Javascript
微信小程序使用二次贝塞尔曲线画波浪
2018/12/25 Javascript
微信小程序页面上下滚动效果
2020/11/18 Javascript
vue+elementUi图片上传组件使用详解
2019/08/20 Javascript
Python使用multiprocessing创建进程的方法
2015/06/04 Python
举例讲解Python的lambda语句声明匿名函数的用法
2016/07/01 Python
Python基于百度AI的文字识别的示例
2018/04/21 Python
Python 16进制与中文相互转换的实现方法
2018/07/09 Python
python程序封装为win32服务的方法
2021/03/07 Python
对python中UDP,socket的使用详解
2019/08/22 Python
Python 转换RGB颜色值的示例代码
2019/10/13 Python
利用python对mysql表做全局模糊搜索并分页实例
2020/07/12 Python
无需压缩软件,用python帮你操作压缩包
2020/08/17 Python
法国最大的在线眼镜店:EasyLunettes
2019/08/26 全球购物
英国领先的餐饮折扣俱乐部:Gourmet Society
2020/07/26 全球购物
成人毕业生自我鉴定
2013/10/18 职场文书
人力资源总监工作说明
2014/03/03 职场文书
学校安全责任书
2014/04/14 职场文书
新党章心得体会
2014/09/04 职场文书
实习介绍信模板
2015/01/30 职场文书
公诉意见书范文
2015/06/05 职场文书