-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiir1.py
71 lines (52 loc) · 2.11 KB
/
iir1.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
#coding:utf-8
# A class of iir butterworth filter
# Check version
# Python 3.6.4 on win32 (Windows 10)
# numpy 1.16.3
# scipy 1.4.1
# matplotlib 2.1.1
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal
class Class_IIR1(object):
def __init__(self, fc=2205.0, btype='low', n_order=2, sampling_rate=44100):
# design iir butterworth filter
# initalize
self.fc= fc # cut off frequency of High Pass Filter by unit is [Hz]
self.sr= sampling_rate
self.n_order= n_order
self.btype= btype
self.b, self.a= signal.butter(self.n_order, (self.fc / (self.sr/2.0)) , btype=self.btype, analog=False, output='ba') # b=Numerator(bunsi), a=denominator(bunbo)
def filtering(self, xin):
return signal.lfilter(self.b, self.a, xin)
def f_show(self, worN=1024):
# show frequency response
wlist, fres = signal.freqz(self.b, self.a, worN=worN)
fig = plt.figure()
ax1 = fig.add_subplot(111)
flist = wlist / ((2.0 * np.pi) / self.sr)
plt.title('frequency response')
ax1 = fig.add_subplot(111)
plt.semilogx(flist, 20 * np.log10(abs(fres)), 'b') # plt.plot(flist, 20 * np.log10(abs(fres)), 'b')
plt.ylabel('Amplitude [dB]', color='b')
plt.xlabel('Frequency [Hz]')
ax2 = ax1.twinx()
angles = np.unwrap(np.angle(fres))
angles = angles / ((2.0 * np.pi) / 360.0)
plt.semilogx(flist, angles, 'g') # plt.plot(flist, angles, 'g')
plt.ylabel('Angle(deg)', color='g')
plt.grid()
plt.axis('tight')
plt.show()
def N(self,):
# if moving average is approximated as a LPF
# n is span of moving average
n=np.sqrt(np.power( 0.443 * self.sr / self.fc, 2.0) +1.0)
print (n)
if __name__ == '__main__':
# instance
sampling_rate=44100
fc=sampling_rate / 20 # re-sample sampling_rate / 10 ?
lpf=Class_IIR1(fc=fc, sampling_rate=sampling_rate)
lpf.f_show()
lpf.N()