/
smooth.py
248 lines (228 loc) · 6.73 KB
/
smooth.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
"""
smooth.py
--------------
A group of functions for smoothing, filtering, averaging and otherwise treating
incomming audio data
--------------
Author : Nathan Villicana-Shaw
Email : nathanshaw@alum.calarts.edu
Date : Oct 13, 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])/2
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 data filtered from x + sens to the last sample
--------------------
"""
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]/2 + x[(i+1)/2]
return X
#not tested yet
def underSample(x, ratio=2):
under = np.zeros(len(x)//ratio)
for i in range(len(under)-1):
under[i] = (x[i*ratio])
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
"""
----------
These Functions need more work : :
---------
def median(x, kernal = None):
if (kernal == None):
kernal = 5
return (medfilt(x, kernal))
def weightedAverage(x, windowSize = 21):
if (windowSize % 2 == 0):
print("please enter in an odd number for window size next time")
halfHop = (windowSize - 1)//2
weight = 0
for i in windowSize:
weight = weight + i
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
"""