/
fir_comparison.py
63 lines (47 loc) · 1.31 KB
/
fir_comparison.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
"""
Compare the simple filter with other FIR filters that have been designed using the windwo method.
"""
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
def freq2rad(freq, fs):
return freq * np.pi / (fs/2)
def rad2freq(rad, fs):
return rad * (fs/2) / np.pi
fs = 16000
filter_order = [40, 110, 180, 250, 320]
cutoff = 100. # in Hz
# for plotting
ALPHA = 0.8
f_max = 1000
# normalized cutoff
nyq = 0.5 * fs
fc_norm = cutoff / nyq
# prepare figure
plt.figure()
# simple filter
b = np.array([1, -1])
w, h = signal.freqz(b)
plt.semilogx([rad2freq(rad, fs) for rad in w],
20 * np.log10(abs(h)),
label="simple (2-tap)",
alpha=ALPHA)
# compare FIR filters of different lengths
for order in filter_order:
# design filter
b = signal.firwin(numtaps=order + 1, cutoff=fc_norm, window="hanning", pass_zero=False)
# visualize response
w, h = signal.freqz(b)
plt.semilogx([rad2freq(rad, fs) for rad in w],
20 * np.log10(abs(h)),
label="%d-tap FIR" % (order + 1),
alpha=ALPHA)
plt.margins(0, 0.1)
plt.title("Frequency response (log scale)")
plt.xlabel("Frequency [Hz]")
plt.ylabel("Magnitude [dB]")
plt.ylim([-50, 10])
plt.grid()
plt.legend(loc="lower right")
plt.tight_layout()
plt.show()