/
preProcessing.py
377 lines (350 loc) · 10.4 KB
/
preProcessing.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""
preProcessing.py
-------------------
A group of preProcessing functions for DSP
--------------------------
Author : Nathan Villicana-Shaw
Email : nathanshaw@alum.calarts.edu
Date : December 9th,, 2014
--------------------------
CalArts : MTEC-480
Fall 2014
--------------------------
"""
import numpy as np
from scipy.signal import lfilter, firwin, medfilt
from pylab import figure, plot, grid, show
def lpf(x, sens):
"""
--------------------
Low Pass Filter Function
--------------------
Variables :
--------------------
x : Incomming array of audio data
sens : the number of samples the function looks back to in order to filter the data
--------------------
Returns :
--------------------
x : The data filtered from x + sens to the last sample
--------------------
"""
for i in range(sens, len(x)):
for s in range(1, sens):
x[i] = (x[i-s] + x[i])
x[i] = x[i]/sens
return x
def normalized(x):
"""
-------------------
Normalizing Filter for scaling data in incomming array so the max value is equal to 1
--------------------
Variables :
--------------------
x : Incomming array of audio data
--------------------
Returns :
--------------------
X : The data filtered from x + sens to the last sample
--------------------
"""
maxi = 0
X = np.zeros(len(x))
for i in range(0, len(x)):
if (x[i] > maxi):
maxi = float(x[i])
for i in range(0,len(x)):
X[i] = x[i]/maxi
return X
def normalAverage(x, windowSize = 21):
"""
-------------------
another type of low pass filter, this one looks ahead and behing to calculate value
--------------------
Variables :
--------------------
x : Incomming array of audio dataA
windowSize : the number of samples to use in order to calculate the average
--------------------
Returns :
--------------------
X : The data filtered from x + sens to the last sample
--------------------
"""
if (windowSize % 2 == 0):
print("please enter in an odd number for window size next time")
halfHop = (windowSize - 1)//2
for i in range(halfHop, len(x) - halfHop):
total = 0
average = 0
for i in range(-halfHop, halfHop):
total = total + x[i]
average = total/windowSize
x[i] = average
return x
def overSample(x):
"""
-------------------
an over sampler used for increading the number of samples available for calculation
function interpolates between each sample to calculate a values between the two given
--------------------
Variables :
--------------------
x : Incomming array of audio dataA
--------------------
Returns :
--------------------
X : the incomming array x but twice as long
--------------------
"""
X = np.zeros[len(x)*2]
for i in range (0, len(X)):
if (i % 2 == 0):
X[i] = x[i/2]
else :
X[i] = (x[(i-1)/2] + x[(i+1)/2])/2
return X
def underSample(x):
"""
-------------------
A undersampling function that takes all the even samples from the incomming array and returns them
-------------------
Variables :
-------------------
x : incomming array or list of data
-------------------
Returns :
-------------------
X : under sampled version of x
-------------------
"""
under = np.zeros(len(x)//2)
for i in range(len(under)-1):
under[i] = (x[i*2])
return under
#this function needs some work
def highPass(x, cuttoff=4000):
"""
-------------------
a high pass filter that uses fft and ifft to remove frequencies above a threshold
--------------------
Variables :
--------------------
x : Incomming array of audio dataA
cuttoff : the frequency where the filter will be in full effect
: default value : 4000
--------------------
Returns :
--------------------
filtered : x returned without any frequencies above the given cuttoff
--------------------
"""
f = np.zeros(len(x))#f is the filter
f[cuttoff*3//4:cuttoff] = np.linspace(0,1,(cuttoff//4))#create a window that acts as filter
f[cuttoff:] = 1#i dont think this works
fftAnal = np.fft.rfft(x)
print("Length of fftAnal is :",len(fftAnal))
print("Length of x is : ", len(x))
for i in range(0, len(fftAnal)):
fftAnal[i] = fftAnal[i] * f[i]
filtered = np.fft.ifft(fftAnal)
return filtered
def fir(x, srate=None, cutoffFreq=None, numTaps=None):
"""
-------------------
--------------------
Variables :
--------------------
x : Incomming array of audio dataA
srate : the sample rate of x
cutoffFreq : the frequency where the filter takes full effect
numTaps : the length of the filter
--------------------
Returns :
--------------------
X : The data filtered
--------------------
"""
if (srate == None):
srate = 44100
nyquist = srate/2
if (cutoffFreq == None):
cutoffFreq = 4168#the frequency of c8
if (numTaps == None):
numTaps = 29 #filter length
fir_coeff = firwin(numTaps, cutoffFreq/nyquist)#creates lowpass filter using FIR
X = lfilter(fir_coeff, 1.0, x)
return X
def removeDC(x):
"""
------------------------
Function for calculating an array of datas DC bias and
then removing it from the signal
------------------------
Variables
------------------------
x : An array of data passed into finction
------------------------
Returns :
------------------------
X : x returned after DC bias is removed from each sample
------------------------
"""
total = 0
X = np.zeros(len(x))
bias = mean(x)
print("removing signal bias of : ", bias)
for i in range(0, len(x)):
X[i] = x[i] - bias
return X
def mean(x):
"""
-------------------
takes the arithmatic mean of the input array of data
--------------------
Variables :
--------------------
x : Incomming array of audio data
--------------------
Returns :
--------------------
X : The average of all the values with the array x
--------------------
"""
total = 0
X = np.zeros(len(x))
for i in range(0,len(x)):
total = total+x[i]
mean = total/len(x)
return mean
def halfWave(x):
"""
---------------------------
The function throws away all negative values in the input
array
---------------------------
Variables :
---------------------------
x : array of data
---------------------------
Returns :
---------------------------
x : rectified array
---------------------------
"""
for i in range(0, len(x)):
if (x[i] <= 0):
x[i] = 0
return x
def fullWave(x):
"""
-----------------------------
The function ensures all values are positive
-----------------------------
Variables:
-----------------------------
x : array of data
-----------------------------
Returns :
-----------------------------
x : rectified array
-----------------------------
"""
for i in range(0,len(x)):
if (x[i] <= 0):
x[i] = x[i]*-1
return x
def energy(x):
"""
------------------------------
The function takes the 'power' of the input signal by squaring the values
------------------------------
Variables:
------------------------------
x : array of data
------------------------------
Returns :
------------------------------
x : rectified array given as power
------------------------------
"""
for i in range(0, len(x)):
x[i] = x[i]**(1/2)
return x
def internalClip(x, threshold = None):
"""
-----------------------
An internal clipper function that only keeps the peaks of an incomming signal
-----------------------
Variables :
-----------------------
x : incomming array of audio data
threshold : the minumum threshold for a signal to pass through the function
-----------------------
Returns :
-----------------------
x : the clipped data is returned
-----------------------
"""
if threshold=None:
threshold = 0.16
x = smooth.normalize(x)
for i in range(0, len(x)):
if (x[i] < threshold):
if(x[i] > -threshold):
x[i] = 0
return x
def binaryClip(x, threshold = None):
"""
-----------------------
An internal clipper function that only keeps the peaks of an incomming signal
This clipper only returns -1,0 or 1. I call it a binary clipper
-----------------------
Variables :
-----------------------
x : incomming array of audio data
threshold : the minumum threshold for a signal to pass through the function
-----------------------
Returns :
-----------------------
x : the clipped data is returned as 1's, -1's or 0's
-----------------------
"""
if threshold=None:
threshold = 0.16
#normalize the data
x = smooth.normalize(x)
for i in range(0, len(x)):
if (x[i] < threshold):
if(x[i] > -threshold):
x[i] = 0
if (x[i] > 0):
x[i] = 1
if (x[i] < 0) :
x[i] = -1
return x
def envelope(x, binSize=None):
"""
---------------------
function for determining the overall amplitude envelope of a signal
---------------------
Variables :
----------------------
x : incoming array of audio data (or data in general
binSize : the amount of samples processed at a time
----------------------
Returns :
----------------------
x : an array containing the envelope data
----------------------
"""
if (binSize == None):
binSize = 2200
for i in range(0,len(x)-binSize,binSize):
maxi = 0.0
for j in range (i,i+binSize):
if (x[j] > maxi):
maxi = x[j]
for i in range(i,i+binSize):
x[i] = maxi
return x