-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_mfcc.py
291 lines (179 loc) · 5.91 KB
/
get_mfcc.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'''
takes a wav file as input and outputs its 13 co-efficient MFCC.
Only implemented for single path, single MFCC extraction and does not automatically work on whole directory.
'''
import numpy as np
import os
from numpy.linalg import norm
import librosa.display
from get_spectraldata import *
import matplotlib.pyplot as plt
from normalize_data import normalizeSoundData
from import_words import shuffle_in_unison_scary
from dtw import dtw
pre_emphasis = 0.97
frame_size = 0.025
frame_stride = 0.01
NFFT = 512
nfilt = 40
num_ceps = 12
cep_lifter = 22 # 0 for no filter
nonllpath = '../LL_chunks/chunk1.wav'
llpath = '../nonLL_chunks/chunk1.wav'
def librosaMfcc(path):
list = absoluteFilePaths(path)
data = []
for file in list:
mfcc = average(findMfcc(file))
# deltah = delta(mfcc)
#temp = np.concatenate((mfcc, deltah))
data.append(mfcc)
return np.array(data)
def plotMfcc(mfcc1, mfcc2):
plt.subplot(1, 2, 1)
librosa.display.specshow(mfcc1)
plt.subplot(1, 2, 2)
librosa.display.specshow(mfcc2)
plt.show()
def computeDistace(mfcc1, mfcc2):
print("Finding DTW between the 2 mfccs")
dist, cost, acc_cost, path = dtw(mfcc1.T, mfcc2.T, dist=lambda x, y: norm(x - y, ord=1))
print ('Normalized distance between the two sounds:', dist)
plt.imshow(cost.T, origin='lower', cmap=plt.get_cmap('gray'), interpolation='nearest')
plt.plot(path[0], path[1], 'w')
plt.xlim((-0.5, cost.shape[0] - 0.5))
plt.ylim((-0.5, cost.shape[1] - 0.5))
plt.xlabel('MFCC Column Index (FET)')
plt.ylabel('MFCC Column Index (Non-FET)')
plt.show()
def padMfcc(mfcc, fixedsize = 30):
print ("padding mfcc")
np.transpose(mfcc)
# mfcc too small
diff = fixedsize - mfcc.shape[1]
print (diff)
for _ in range(diff):
print ("running")
np.vstack((mfcc, np.zeros(20)))
print (mfcc.shape)
def getMfccAverage(): # return average of all mfccs for each word in numpy array
# first creating the dir list
pathlist = []
pathlist.extend(absoluteFilePaths('../LL_chunks'))
pathlist.extend(absoluteFilePaths('../nonLL_chunks'))
print ("path list is",pathlist)
data = []
for path in pathlist:
print ("finding mfcc of", path)
data.append(average(findMfcc(path)))
# print len(data[-1])
return np.array(data)
def getndimMfcc(): # return average of all mfccs for each word
# first creating the dir list
pathlist = []
pathlist.extend(absoluteFilePaths('../LL_chunks'))
pathlist.extend(absoluteFilePaths('../nonLL_chunks'))
print ("path list is",pathlist)
data = []
for path in pathlist:
print ("finding mfcc of", path)
data.append(findMfcc(path))
print(len(data[-1]))
return np.array(data)
def getMfccDelta():
# first creating the dir list
print ("hello")
pathlist = []
pathlist.extend(absoluteFilePaths('../LL_chunks'))
pathlist.extend(absoluteFilePaths('../nonLL_chunks'))
print ("path list is", pathlist)
data = []
for path in pathlist:
print ("finding mfcc of", path)
# mfcc = average((findMfcc(path)))
deltah = average(delta(findMfcc(path)))
# temp = np.concatenate((deltah, mfcc))
print (deltah)
data.append(deltah)
print (len(data[-1]))
return np.array(data)
def absoluteFilePaths(directory):
for dirpath,_,filenames in os.walk(directory):
if ('.DS_Store' in filenames):
filenames.remove('.DS_Store')
for f in filenames:
yield os.path.abspath(os.path.join(dirpath, f))
def findMfcc(path):
y1, sr1 = librosa.load(path)
mfcc = librosa.feature.mfcc(y1, sr1)
return mfcc
def average(mfcc):
# take an n coefficient mfcc for multiple samples and finds its nx1 size average array
# print ("averaging mfcc")
ave = []
for i in range(mfcc.shape[0]):
ave.append(np.mean(mfcc[i,:]))
ave_numpy = np.array(ave)
# print (ave_numpy)
return ave_numpy
def getFinalNormalizedMfcc(): #shuffling occurs here
data = getMfccAverage()
# data = deltaplusmfcc()
# data = getMfccSum()
# data = getMfccDelta()
normalizeSoundData(data)
_, labels = getTrainingData()
print (labels)
shuffle_in_unison_scary(data, labels)
return data, labels
def flatten():
print("flattening")
def delta(mfcc):
'''
:param mfcc: input mfcc 2d array
:return: first dervative of mfcc 2d array
'''
print ("finding the delta")
return librosa.feature.delta(mfcc)
def sum(mfcc): #over time axis or coloum wise
data = np.array(mfcc) # if mfcc data is not already in numpy
return data.sum(axis=1)
def getlabels():
'''
:return: gets non shuffled labels as numpy array of size [training samples, 1], 1 for LL 0 for Non LL
'''
def getMfccSum():
pathlist = []
pathlist.extend(absoluteFilePaths('../LL_chunks'))
pathlist.extend(absoluteFilePaths('../nonLL_chunks'))
print ("path list is", pathlist)
data = []
for path in pathlist:
print ("finding mfcc of", path)
data.append(sum(findMfcc(path)))
print (len(data[-1]))
return np.array(data)
def getmfccDoubleDelta():
print
if __name__ == '__main__':
# nonllmfcc, llmfcc = librosaMFCC(nonllpath, llpath)
# average(llmfcc)
# a = []
#
# for i in range(5):
# temp = findMfcc('../nonLL_chunks/chunk{}.wav'.format(i+1))
# np.savetxt('nonll{}.csv'.format(i+1), temp)
mfcc1 = findMfcc('../tempsentences/fox.wav')
mfcc2 = findMfcc('../tempsentences/foxn.wav')
print(mfcc1.shape)
print(mfcc2.shape)
computeDistace(mfcc1, mfcc2)
# plotMfcc(nonllmfcc, llmfcc)
# computeDistace(nonllmfcc, llmfcc)
# data, labels = getFinalNormalizedMfcc()
# print (data[1:10, :])
# print (data.size)
# print (type(data))
# # print data[500:,:]
#
# # deltaplusmfcc()