-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
210 lines (191 loc) · 7.84 KB
/
utils.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
import wave, struct, time
import numpy as np
import scipy.io.wavfile as wav
import scipy.fftpack as fft
def openWavFile(fileName):
data = wav.read(fileName)
ssize = data[1].shape[0]
nparray = data[1].astype('float32')
return nparray
def stftWindowFunction(xPhi, xMag):
oldShapePhi = xPhi.shape
oldShapeMag = xMag.shape
xPhi = np.reshape(xPhi, (-1, xPhi.shape[-1]))
xMag = np.reshape(xMag, (-1, xMag.shape[-1]))
retValPhi = []
retValMag = []
for xValPhi, xValMag in zip(xPhi, xMag):
w = np.hanning(xValPhi.shape[0])
phiObj = np.zeros(xValPhi.shape[0], dtype=complex)
phiObj.real, phiObj.imag = np.cos(xValPhi), np.sin(xValPhi)
xIfft = np.fft.ifft(xValMag * phiObj)
wFft = np.fft.fft(w*xIfft.real)
retValPhi.append(np.angle(wFft))
retValMag.append(np.abs(wFft))
retValMag = np.reshape(retValMag, oldShapeMag)
retValPhi = np.reshape(retValPhi, oldShapePhi)
return retValPhi, retValMag
def stft(x, framesz, hop):
framesamp = int(framesz)
hopsamp = int(hop)
#w = np.hanning(framesamp)
X = np.asarray([np.fft.fft(x[i:i+framesamp]) for i in range(0, len(x) - framesamp, hopsamp)])
xPhi = np.angle(X)
xMag = np.abs(X)
return xPhi, xMag
def istft(X, fs, hop, origs):
x = np.zeros(origs)
framesamp = X.shape[1]
hopsamp = int(hop*fs)
for n,i in enumerate(range(0, len(x)-framesamp, hopsamp)):
x[i:i+framesamp] += np.real(np.fft.ifft(X[n]))
return x
def waveToSTFT(waveData, sampCount, blkSize, hop):
initLen = len(waveData)
sampSize = int(initLen/sampCount)
phiObj = []
magObj = []
for sInd in xrange(0, sampCount):
tempTmSpls = []
sampBlk = waveData[sInd * sampSize:(sInd + 1) * sampSize]
stftPhi, stftMag = stft(sampBlk, blkSize, hop)
phiObj.append(stftPhi)
magObj.append(stftMag)
return ([], np.asarray(phiObj), np.asarray(magObj))
def waveToMFCC(waveData, sampCount, blkCount=False, blkSize=False):
waveLen = len(waveData)
sampSize = int(waveLen/sampCount)
retTmSpl = []
if blkSize:
blkCount = sampSize/blkSize
elif blkCount:
blkSize = sampSize/blkCount
else:
return False
for sInd in xrange(0, sampCount):
tempTmSpls = []
sampBlk = waveData[sInd * sampSize:(sInd + 1) * sampSize]
for bInd in xrange(0, blkCount):
tempBlk = sampBlk[bInd * blkSize:(bInd + 1) * blkSize]
complexSpectrum = np.fft.fft(tempBlk)
powerSpectrum = np.abs(complexSpectrum) ** 2
filteredSpectrum = powerSpectrum
logSpectrum = np.log(filteredSpectrum)
dctSpectrum = fft.dct(logSpectrum, type=2)
tempTmSpls.append(dctSpectrum)
retTmSpl.append(tempTmSpls)
retTmSpl = np.asarray(retTmSpl)
return retTmSpl
def waveToBlock(waveData, sampCount, blkCount=False, blkSize=False, olapf=1, shift=False):
if shift:
waveData = np.concatenate((waveData[shift:], waveData[:shift]))
waveLen = len(waveData)
sampSize = int(waveLen/sampCount)
retPhase = []
retMag = []
retTmSpl = []
if blkSize and blkCount:
tlen = sampCount * blkCount * blkSize
sampSize = blkCount * blkSize
diff = tlen - waveLen
if diff > 0:
waveData = np.pad(waveData, (0,diff), 'constant', constant_values=0)
elif blkSize:
blkCount = sampSize/blkSize
elif blkCount:
blkSize = sampSize/blkCount
else:
return False
for sInd in xrange(0, sampCount):
tempPhases = []
tempMags = []
tempTmSpls = []
sampBlk = waveData[sInd * sampSize:(sInd + 1) * sampSize]
for bInd in xrange(0, blkCount - (olapf - 1)):
tempBlk = sampBlk[bInd * blkSize:(bInd + olapf) * blkSize]
tempFFT = np.fft.fft(tempBlk)
tempPhase = np.angle(tempFFT)
tempMagn = np.abs(tempFFT)
tempPhases.append(tempPhase)
tempMags.append(tempMagn)
tempTmSpls.append(tempBlk)
retPhase.append(tempPhases)
retMag.append(tempMags)
retTmSpl.append(tempTmSpls)
retPhase = np.asarray(retPhase)
retTmSpl = np.asarray(retTmSpl)
retMag = np.asarray(retMag)
return (retTmSpl, retPhase, retMag)
def sectionFeatureScaling(data):
dataShape = data.shape
flatData = np.copy(data).flatten()
flatMax = np.max(flatData)
flatMin = np.min(flatData)
scaledData = (flatData - flatMin)/(flatMax- flatMin)
scaledData = np.reshape(scaledData, dataShape)
return scaledData, flatMax, flatMin
def blockFeatureScaling(kData):
data = np.copy(kData)
maxVal = np.max(np.max(data, axis=0), axis=0)
minVal = np.min(np.min(data, axis=0), axis=0)
scaledData = (data - minVal)/(maxVal- minVal)
return scaledData, maxVal, minVal
def sectionNormalize(data):
dataShape = data.shape
flatData = np.copy(data).flatten()
flatMean = np.mean(flatData)
flatStd = np.std(flatData)
scaledData = (flatData - flatMean)/flatStd
scaledData = np.reshape(scaledData, dataShape)
return scaledData, flatMean, flatStd
def blockNormalize(data):
dataStartShape = data.shape
if len(data.shape) == 2:
data = np.reshape(data, (1, data.shape[0], data.shape[1]))
if len(data.shape) == 1:
data = np.reshape(data, (1, 1, data.shape[0]))
npNorm = np.zeros_like(data)
xCount = data.shape[0]
yCount = data.shape[1]
for sectInd in xrange(xCount):
for blockInd in xrange(yCount):
npNorm[sectInd][blockInd] = data[sectInd][blockInd]
mean = np.mean(np.mean(npNorm, axis=0), axis=0)
std = np.sqrt(np.mean(np.mean(np.abs(npNorm-mean)**2, axis=0), axis=0))
std = np.maximum(1.0e-8, std)
norm = npNorm.copy()
norm[:] -= mean
norm[:] /= std
return norm, mean, std
def extractSTFTWaveData(wavData, sampCount, blkSize=False, returnObj="all", olapf=100):#(waveData, sampCount, blkCount, blkSize, hop):
#wavData = openWavFile(fileName)
#wavObj, wavPhi, wavMag = waveToBlock(wavData, sampCount, blkCount=blkCount, blkSize=blkSize, olapf=olapf, shift=False)
wavObj, wavPhi, wavMag = waveToSTFT(wavData, sampCount, blkSize=blkSize, hop=olapf)
#mfccObj = waveToMFCC(wavData, sampCount, blkCount, blkSize)
phiWav, meanPhiWav, stdPhiWav = blockNormalize(wavPhi)
magWav, meanMagWav, stdMagWav = blockNormalize(wavMag)
#MfccWav, meanMfcc, stdMfcc = blockNormalize(mfccObj)
if returnObj == "phase":
return phiWav, meanPhiWav, stdPhiWav
elif returnObj == "magnitude":
return magWav, meanMagWav, stdMagWav
else:
return phiWav, meanPhiWav, stdPhiWav, magWav, maxMagWav, minMagWav
def extractWaveData(wavData, sampCount, blkCount=False, blkSize=False, returnObj="all", olapf=1, shift=False):
#wavData = openWavFile(fileName)
wavObj, wavPhi, wavMag = waveToBlock(wavData, sampCount, blkCount=blkCount, blkSize=blkSize, olapf=olapf, shift=False)
#mfccObj = waveToMFCC(wavData, sampCount, blkCount, blkSize)
phiWav, meanPhiWav, stdPhiWav = blockNormalize(wavPhi)
magWav, meanMagWav, stdMagWav = blockNormalize(wavMag)
#MfccWav, meanMfcc, stdMfcc = blockNormalize(mfccObj)
if returnObj == "phase":
return phiWav, meanPhiWav, stdPhiWav
elif returnObj == "magnitude":
return magWav, meanMagWav, stdMagWav
else:
return phiWav, meanPhiWav, stdPhiWav, magWav, maxMagWav, minMagWav
def blockShift(data, shift=1):
retObj = []
for sectInd in xrange(data.shape[0]):
retObj.append( np.concatenate((data[sectInd][shift:], data[sectInd][0:shift])) )
return np.reshape(retObj, data.shape)