-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIIR_Benchmark.py
130 lines (114 loc) · 3.44 KB
/
IIR_Benchmark.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
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams.update({'font.size': 18})
wc = 1000
order = 4
# Butterworth filter
b, a = signal.butter(order, wc, 'low', analog=True)
w, h = signal.freqs(b, a)
plt.figure(1)
plt.subplot(321)
plt.semilogx(w, 20 * np.log10(np.abs(h)))
plt.title('Butterworth')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Amplitude [dB]')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
plt.axvline(wc, color='green') # cutoff frequency
plt.figure(2)
plt.subplot(321)
plt.semilogx(w, np.unwrap(np.angle(h)))
plt.axvline(wc, color='green') # cutoff frequency
plt.title('Butterworth')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Phase [radians]')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
# Bessel filter
b, a = signal.bessel(order, wc, 'low', analog=True, norm='phase')
w, h = signal.freqs(b, a)
plt.figure(1)
plt.subplot(322)
plt.semilogx(w, 20 * np.log10(np.abs(h)))
plt.title('Bessel')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Amplitude [dB]')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
plt.axvline(wc, color='green') # cutoff frequency
plt.figure(2)
plt.subplot(322)
plt.semilogx(w, np.unwrap(np.angle(h)))
plt.axvline(wc, color='green') # cutoff frequency
plt.title('Bessel')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Phase [radians]')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
# Chebyshev 1
# rp : maximum ripple allowed below the unity gain
b, a = signal.cheby1(order, 10, wc, 'low', analog=True)
w, h = signal.freqs(b, a)
plt.figure(1)
plt.subplot(323)
plt.semilogx(w, 20 * np.log10(abs(h)))
plt.title('Chebyshev Type I')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Amplitude [dB]')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
plt.axvline(wc, color='green') # cutoff frequency
plt.figure(2)
plt.subplot(323)
plt.semilogx(w, np.unwrap(np.angle(h)))
plt.axvline(wc, color='green') # cutoff frequency
plt.title('Chebyshev Type I')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Phase [radians]')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
# Chebyshev 2 or inverse of chebyshev 1
b, a = signal.cheby2(order, 10, wc, 'low', analog=True)
w, h = signal.freqs(b, a)
plt.figure(1)
plt.subplot(324)
plt.semilogx(w, 20 * np.log10(abs(h)))
plt.title('Chebyshev Type II')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Amplitude [dB]')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
plt.axvline(wc, color='green') # cutoff frequency
plt.figure(2)
plt.subplot(324)
plt.semilogx(w, np.unwrap(np.angle(h)))
plt.axvline(wc, color='green') # cutoff frequency
plt.title('Chebyshev Type II')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Phase [radians]')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
# Elliptic or Cauer
b, a = signal.ellip(order, 10, 10, wc, 'low', analog=True)
w, h = signal.freqs(b, a)
plt.figure(1)
plt.subplot(325)
plt.semilogx(w, 20 * np.log10(abs(h)))
plt.title('Elliptic')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Amplitude [dB]')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
plt.axvline(wc, color='green') # cutoff frequency
plt.figure(2)
plt.subplot(325)
plt.semilogx(w, np.unwrap(np.angle(h)))
plt.axvline(wc, color='green') # cutoff frequency
plt.title('Elliptic')
plt.xlabel('Frequency [radians / second]')
plt.ylabel('Phase [radians]')
plt.margins(0, 0.1)
plt.grid(which='both', axis='both')
plt.tight_layout()
plt.show()