-
Notifications
You must be signed in to change notification settings - Fork 7
/
SVMapproach.py
209 lines (144 loc) · 4.88 KB
/
SVMapproach.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import sys
import numpy as np
import sys
from scipy.fftpack import rfft,fftfreq
import numpy as np
import matplotlib.pyplot as plt
def fileRead(fileName,lineToRemove,leftColToRemove,rightColToRemove):
file = open(fileName, 'r')
fullText=file.read()
lines=fullText.split('\n')
print('Read '+str(len(lines))+' lines')
for x in range(lineToRemove):
lines.pop(0)
ar=[]
for x in range(len(lines)):
temp=[]
fields=lines[x].split(",")
for xx in range(len(fields)):
fields[xx]=fields[xx].strip()
for y in range(leftColToRemove,len(fields)-rightColToRemove):
#print('converting to float >>>'+fields[y])
temp.append(float(fields[y]))
if len(temp)==0:
continue
ar.append(temp)
print('Reading line '+str(x)+' \r')
sys.stdout.flush()
npar=np.ndarray((len(ar), len(ar[0])))
for i in range(len(ar)):
for j in range(len(ar[0])):
npar[i][j] = ar[i][j]
return npar.transpose()
def nextpow2(i):
"""
Find the next power of 2 for number i
"""
n = 1
while n < i:
n *= 2
return n
def readFiles(fileNameList):
NO_OF_CHANNELS=8
PASS_BAND_LOW=3.0
PASS_BAND_HIGH=50.0
NO_OF_BANDS=5
singleBandWidth=(PASS_BAND_HIGH-PASS_BAND_LOW)/NO_OF_BANDS
interestingBands=[x for x in range(NO_OF_BANDS)]
allChannelBandResults=np.ndarray((NO_OF_CHANNELS,len(interestingBands)))
fileNames=fileNameList#["openBCI_2013-12-24_meditation.txt"]
Y=[1]
for file in range(len(fileNames)):
ar=fileRead(fileNames[file],4,1,3)#complete
for chan in range(len(ar)):
bandResults = np.ndarray(len(interestingBands))
bandCount = np.ndarray(len(interestingBands))
freqSpectrum=rfft(ar[chan,:])
timeStep=1.0/250
n=len(ar[chan])
freq=fftfreq(n,d=timeStep)
endIndex=0
startIndex=0
while(freq[startIndex]<PASS_BAND_LOW):
startIndex+=1
while(freq[endIndex]<PASS_BAND_HIGH):
endIndex+=1
endIndex-=1
freqSpectrum=np.abs((freqSpectrum[startIndex:endIndex]))
freq=(freq[startIndex:endIndex])
'''plt.figure()
plt.plot(freq, freqSpectrum)'''
#plt.plot(range(len(freq)),freq)
for f in range(len(freq)):
if(freq[f]>0):
bandResults[int((freq[f]-PASS_BAND_LOW)/singleBandWidth)]+=freqSpectrum[f]
'''band=0
for bb in range(len(interestingBands)):
if interestingBands[bb]>freq[f]:
band=bb-1
break
bandResults[band]+=np.abs(freqSpectrum[f])
bandCount[band]+=1'''
'''
for x in range(len(bandResults)):
if bandCount[x]<1:
bandResults[x]=0
else:
bandResults[x]=bandResults[x]/(1.0*bandCount[x])'''
allChannelBandResults[chan]=bandResults
print('channel ',chan+1,'of ',len(allChannelBandResults),' channels completed')
#print(allChannelBandResults)
#put the plot code here
'''for i in range(NO_OF_CHANNELS):
plt.figure()
plt.plot(interestingBands, allChannelBandResults[i,:])
plt.show()'''
plt.show()
return allChannelBandResults.flatten()
def readFileAndMakeFeatureVector(fileName):
return readFiles([fileName])
#readFiles(sys.argv[1:])
#fileRead(sys.argv[1])
DIMENSIONS=int(sys.argv[1])
TRAINING_DATA_POINTS=int(sys.argv[2])
TEST_DATA_POINTS=int(sys.argv[3])
X=np.ndarray((TRAINING_DATA_POINTS,DIMENSIONS))
Y=np.ndarray(TRAINING_DATA_POINTS)
for f in range(TRAINING_DATA_POINTS):
fileName=input('Enter file name\n')
X[f]=readFileAndMakeFeatureVector(fileName)
Y[f]=int(input('Enter class\n'))
print('Finished importing data')
from sklearn import svm
clf = svm.SVC(kernel='linear')
print(X)
print(Y)
clf.fit(X[:], Y[:])
correct=0
wrong=0
pre=clf.predict(X)
for x in range(len(X)):
print('x='+str(X[x])+' y='+str(Y[x])+' prediction='+str(pre[x]))
if pre[x] == Y[x]:
correct += 1
else:
wrong += 1
accuracy=(correct*100.0)/(correct+wrong)
print('accuracy='+str(accuracy))
XX=np.ndarray((TEST_DATA_POINTS,DIMENSIONS))
YY=np.ndarray(TEST_DATA_POINTS)
for ff in range(TEST_DATA_POINTS):
fileName=input('Enter file name\n')
XX[ff]=readFileAndMakeFeatureVector(fileName)
YY[ff]=int(input('Enter calss\n'))
correct=0
wrong=0
pred=clf.predict(XX)
for x in range(len(XX)):
print(' y='+str(YY[x])+' prediction='+str(pred[x]))
if pred[x] == YY[x]:
correct += 1
else:
wrong += 1
accuracy=(correct*100.0)/(correct+wrong)
print('accuracy='+str(accuracy))