使用python接受tgam的腦波數(shù)據(jù)實(shí)例
廢話不多說(shuō),來(lái)看看實(shí)例吧!
# -*- 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")
補(bǔ)充知識(shí):Python處理腦電數(shù)據(jù):PCA數(shù)據(jù)降維
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] #逆轉(zhuǎn),即降序 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的腦波數(shù)據(jù)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
分享4個(gè)方便且好用的Python自動(dòng)化腳本
自動(dòng)化測(cè)試是把以人為驅(qū)動(dòng)的測(cè)試行為轉(zhuǎn)化為機(jī)器執(zhí)行的一種過(guò)程,直白的就是為了節(jié)省人力、時(shí)間或硬件資源,提高測(cè)試效率,這篇文章主要給大家分享介紹了3個(gè)方便且好用的Python自動(dòng)化腳本,需要的朋友可以參考下2022-02-02Python 多線程C段掃描、檢測(cè) Ping掃描腳本的實(shí)現(xiàn)
這篇文章主要介紹了Python 多線程C段掃描、檢測(cè) Ping掃描腳本的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09詳解OpenCV中直方圖,掩膜和直方圖均衡化的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了OpenCV中直方圖、掩膜、直方圖均衡化詳細(xì)介紹及代碼的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),需要的可以參考一下2022-11-11pytorch下大型數(shù)據(jù)集(大型圖片)的導(dǎo)入方式
今天小編就為大家分享一篇pytorch下大型數(shù)據(jù)集(大型圖片)的導(dǎo)入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01解決Python中pandas讀取*.csv文件出現(xiàn)編碼問(wèn)題
很多朋友在使用Python中pandas讀取csv文件時(shí),出現(xiàn)編碼格式問(wèn)題,接下來(lái)通過(guò)本文給大家分享解決Python中pandas讀取*.csv文件出現(xiàn)編碼問(wèn)題,需要的朋友可以參考下2019-07-07Python調(diào)用百度AI實(shí)現(xiàn)人像分割詳解
本文主要介紹了如何通過(guò)Python調(diào)用百度AI從而實(shí)現(xiàn)人像的分割與合成,文中的示例代碼對(duì)我們的工作或?qū)W習(xí)有一定的幫助,需要的朋友可以參考一下2021-12-12python 利用panda 實(shí)現(xiàn)列聯(lián)表(交叉表)
這篇文章主要介紹了python 利用panda 實(shí)現(xiàn)列聯(lián)表(交叉表),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02python對(duì) MySQL 數(shù)據(jù)庫(kù)進(jìn)行增刪改查的腳本
這篇文章主要介紹了python對(duì) MySQL 數(shù)據(jù)庫(kù)進(jìn)行增刪改查的腳本,幫助大家更好的利用python處理數(shù)據(jù)庫(kù),感興趣的朋友可以了解下2020-10-10