使用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简单的函数定义和用法实例
May 07 Python
使用Python脚本将文字转换为图片的实例分享
Aug 29 Python
Python排序搜索基本算法之选择排序实例分析
Dec 09 Python
Python3对称加密算法AES、DES3实例详解
Dec 06 Python
解决python给列表里添加字典时被最后一个覆盖的问题
Jan 21 Python
利用python-pypcap抓取带VLAN标签的数据包方法
Jul 23 Python
python利用re,bs4,requests模块获取股票数据
Jul 29 Python
python实现从ftp服务器下载文件
Mar 03 Python
Pandas读取csv时如何设置列名
Jun 02 Python
Python常用GUI框架原理解析汇总
Dec 07 Python
python实现b站直播自动发送弹幕功能
Feb 20 Python
python自动化操作之动态验证码、滑动验证码的降噪和识别
Aug 30 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加密解密函数(动态加密)
2013/06/19 PHP
php下载文件超时时间的设置方法
2016/10/06 PHP
Laravel 6 将新增为指定队列任务设置中间件的功能
2019/08/06 PHP
深入理解JavaScript系列(4) 立即调用的函数表达式
2012/01/15 Javascript
javascript中的onkeyup和onkeydown区别介绍
2013/04/28 Javascript
JavaScript:new 一个函数和直接调用函数的区别分析
2013/07/10 Javascript
js调用图片隐藏&amp;显示实现代码
2013/09/13 Javascript
jQuery CSS()方法改变现有的CSS样式
2014/08/20 Javascript
javascript实现控制文字大中小显示
2015/04/28 Javascript
javascript实现点击单选按钮链接转向对应网址的方法
2015/08/12 Javascript
利用jQuery及AJAX技术定时更新GridView的某一列数据
2015/12/04 Javascript
详解vue 模版组件的三种用法
2017/07/21 Javascript
Angular 4中如何显示内容的CSS样式示例代码
2017/11/06 Javascript
bootstrap table.js动态填充单元格数据的多种方法
2019/07/18 Javascript
微信小程序官方动态自定义底部tabBar的例子
2019/09/04 Javascript
Layui数据表格之单元格编辑方式
2019/10/26 Javascript
详解Vue中的自定义指令
2020/12/07 Vue.js
[01:14]英雄,所敬略同——2018完美盛典宣传视频4K
2018/12/05 DOTA
python如何为被装饰的函数保留元数据
2018/03/21 Python
python requests 测试代理ip是否生效
2018/07/25 Python
pandas去除重复列的实现方法
2019/01/29 Python
Python数据分析模块pandas用法详解
2019/09/04 Python
numpy创建单位矩阵和对角矩阵的实例
2019/11/29 Python
Python如何使用27行代码绘制星星图
2020/07/20 Python
零基础学Python之前需要学c语言吗
2020/07/21 Python
Python基础进阶之海量表情包多线程爬虫功能的实现
2020/12/17 Python
html5生成柱状图(条形图)效果的实例代码
2016/03/25 HTML / CSS
Myprotein葡萄牙官方网站:英国优质运动营养品牌
2016/09/12 全球购物
为有想象力的人提供的生活方式商店:Firebox
2018/06/04 全球购物
学习党的群众路线教育实践活动心得体会
2014/03/01 职场文书
个人贷款担保书
2014/04/01 职场文书
单位未婚证明范本
2014/11/25 职场文书
物业接待员岗位职责
2015/04/15 职场文书
新闻通讯稿范文
2015/07/22 职场文书
css position fixed 左右双定位的实现代码
2021/04/29 HTML / CSS
教你怎么用python爬取爱奇艺热门电影
2021/05/20 Python