-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilters.py
More file actions
42 lines (33 loc) · 1.01 KB
/
filters.py
File metadata and controls
42 lines (33 loc) · 1.01 KB
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
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import butter, lfilter, freqz
class Filter:
def __init__(self, sample_rate=44100, order=5):
self.nyquist_frequency = 0.5*sample_rate
self.order = order
def plot_frequency_response(self, xlim=None):
'''
Only for debugging
'''
if not xlim:
xlim = self.nyquist_frequency
w, h = freqz(filt.b, filt.a, worN=8000)
plt.plot(self.nyquist_frequency*w/np.pi, np.abs(h), 'b')
plt.plot(cutoff, 0.5*np.sqrt(2), 'ko')
plt.axvline(cutoff, color='k')
plt.xlim(0, xlim)
plt.grid()
plt.show()
class LowpassFilter(Filter):
def _update(self, cutoff):
'''
Calculate new coefficients
'''
normalized_cutoff = cutoff / self.nyquist_frequency
self.b, self.a = butter(self.order, normalized_cutoff, btype='low', analog=False)
def __call__(self, data):
if not hasattr(self, 'cutoff') or self.cutoff != data['lpc']:
self.cutoff = data['lpc']
self._update(self.cutoff)
data['data'] = lfilter(self.b, self.a, data['data'])
return data