使用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函数学习笔记
Oct 07 Python
简单讲解Python中的闭包
Aug 11 Python
Pythont特殊语法filter,map,reduce,apply使用方法
Feb 27 Python
python爬虫的工作原理
Mar 05 Python
python 批量修改/替换数据的实例
Jul 25 Python
python实现flappy bird游戏
Dec 24 Python
Python+OpenCV图片局部区域像素值处理改进版详解
Jan 23 Python
用Python解决x的n次方问题
Feb 08 Python
基于Numpy.convolve使用Python实现滑动平均滤波的思路详解
May 16 Python
Python实现直方图均衡基本原理解析
Aug 08 Python
django教程如何自学
Jul 31 Python
python利用xlsxwriter模块 操作 Excel
Oct 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
PHP利用APC模块实现文件上传进度条的方法
2015/01/26 PHP
redis+php实现微博(二)发布与关注功能详解
2019/09/23 PHP
JavaScript中setAttribute用法介绍
2013/07/20 Javascript
javascript框架设计读书笔记之种子模块
2014/12/02 Javascript
如何解决easyui自定义标签 datagrid edit combobox 手动输入保存不上
2015/12/26 Javascript
扩展Bootstrap Tooltip插件使其可交互的方法
2016/11/07 Javascript
Bootstrap Modal对话框如何在关闭时触发事件
2016/12/02 Javascript
JS正则表达式学习之贪婪和非贪婪模式实例总结
2016/12/26 Javascript
vue.js中引入vuex储存接口数据及调用的详细流程
2017/12/14 Javascript
Angular4学习教程之DOM属性绑定详解
2018/01/04 Javascript
Vue不能观察到数组length的变化
2018/06/08 Javascript
使用vue2.0创建的项目的步骤方法
2018/09/25 Javascript
微信小程序如何修改本地缓存key中单个数据的详解
2019/04/26 Javascript
解决vue中使用proxy配置不同端口和ip接口问题
2019/08/14 Javascript
es6 for循环中let和var区别详解
2020/01/12 Javascript
[01:12:27]EG vs Secret 2018国际邀请赛淘汰赛BO3 第二场 8.22
2018/08/23 DOTA
[34:10]Secret vs VG 2019国际邀请赛淘汰赛 败者组 BO3 第二场 8.24
2019/09/10 DOTA
[42:32]完美世界DOTA2联赛循环赛 Magma vs PXG BO2第二场 10.28
2020/10/28 DOTA
python使用在线API查询IP对应的地理位置信息实例
2014/06/01 Python
使用Protocol Buffers的C语言拓展提速Python程序的示例
2015/04/16 Python
简单解决Python文件中文编码问题
2015/11/22 Python
Swift 3.0在集合类数据结构上的一些新变化总结
2016/07/11 Python
Python实现购物车功能的方法分析
2017/11/10 Python
Python openpyxl 遍历所有sheet 查找特定字符串的方法
2018/12/10 Python
python制作填词游戏步骤详解
2019/05/05 Python
YSL Beauty加拿大官方商城:圣罗兰美妆加拿大
2017/05/15 全球购物
香港卓悦化妆品官网:BONJOUR
2017/09/21 全球购物
ToysRus日本官网:玩具反斗城
2018/09/08 全球购物
打架检讨书100字
2014/01/19 职场文书
质量保证书
2015/01/17 职场文书
悬空寺导游词
2015/02/05 职场文书
交通事故赔偿起诉书
2015/05/20 职场文书
2019大学生预备党员转正思想汇报
2019/06/21 职场文书
Python 内置函数速查表一览
2021/06/02 Python
Oracle表空间与权限的深入讲解
2021/11/17 Oracle
JavaScript实现音乐播放器
2022/08/14 Javascript