-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
157 lines (129 loc) · 4.77 KB
/
demo.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
import numpy as np
import scipy.signal as signal
def remezBands(L, w0):
"""Band edges and gains of an interpolation filter for use with Remez.
Parameters
----------
L : int
Interpolation factor, e.g., 2, 3, etc.
w0 : float
Signal bandwidth in radians, i.e., 0 < `w0` < π.
Returns
-------
bands : ndarray
A rank-1 array (vector) of band edges in normalized frequency. All
entries are between 0 and 0.5.
gains : ndarray
Vector half-as-long as `bands` containing gains in these bands. These
two can be passed into, e.g., `scipy.signal.remez`.
References
----------
.. [1] Phil Schniter, “ECE-700 Multirate Notes”, March 27, 2006, page 6.
http://www2.ece.ohio-state.edu/~schniter/ee700/handouts/multirate.pdf
"""
if L % 2 == 0:
k = np.arange(0, L / 2.0)
transitionBands = np.sort(np.hstack(
(0, np.pi, (2 * k * np.pi + w0) / L, (2 * (k + 1) * np.pi - w0) / L
))) / (2 * np.pi)
else:
k = np.arange(0, (L + 1) / 2.0)
transitionBands = np.sort(np.hstack(
(0, (2 * k * np.pi + w0) / L, (2 * (k + 1) * np.pi - w0) / L
))) / (2 * np.pi)
transitionBands = transitionBands[:-1]
desired = np.zeros(transitionBands.size // 2)
desired[0] = 1.0
return transitionBands, desired
def remezDesign(L, w0, rippleDb=-30):
"""Design an interpolation filter with Remez exchange.
Parameters
----------
L : int
Interpolation factor, e.g., 2, 3, etc.
w0 : float
Bandwidth of the signal in radians, i.e., 0 < `w0` < π.
rippleDb : float
Tolerable ripple (in 10*log10 dB). This is used with Kaiser’s formula
to estimate the filter length. E.g., ripple (max in passband, min in
stopband) of 0.001 means `rippledB` of -30.
Returns
-------
out : ndarray
A rank-1 array (vector) containing the filter weights.
"""
ntaps, _ = signal.kaiserord(rippleDb,
(2 * np.pi - 2 * w0) / (L * 2 * np.pi))
bandsDesired = remezBands(L, w0)
return signal.remez(ntaps, *bandsDesired)
def firwin2Design(L, w0, rippleDb=-30):
"""Like `remezDesign` but uses `scipy.signal.firwin2` instead of Remez.
Since `firwin2` doesn’t handle don’t-care regions, the returned filter is
just an ordinary low-pass filter, and is likely to have slower
transition-band behavior than that returned by `remezDesign` or
`firlsDesign`.
"""
ntaps, _ = signal.kaiserord(rippleDb,
(2 * np.pi - 2 * w0) / (L * 2 * np.pi))
bands, gains = remezBands(L, w0)
return signal.firwin2(ntaps, [0, bands[1], bands[2], 0.5], [1, 1, 0, 0.0],
nyq=0.5)
def firlsDesign(L, w0, rippleDb=-30):
"Like `remezDesign` but uses `scipy.signal.firls` instead of Remez."
ntaps, _ = signal.kaiserord(rippleDb,
(2 * np.pi - 2 * w0) / (L * 2 * np.pi))
# firls requires odd numtaps
if ntaps % 2 == 0:
ntaps += 1
b, g = remezBands(L, w0)
# the `vstack` stuff below: repeat each element of `g` twice, so `[1 0 0]`
# becomes `[1 1, 0 0, 0 0]`.
return signal.firls(ntaps, b, np.vstack([g, g]).T.ravel(), nyq=0.5)
if __name__ == '__main__':
import matplotlib.pyplot as plt
db20 = lambda x: 20 * np.log10(np.abs(x))
def viz(b):
w, h = signal.freqz(b)
plt.plot(w / (2 * np.pi), db20(h))
w0 = 0.9 * np.pi
Ntaps = 96
plt.figure()
viz(signal.remez(Ntaps, *remezBands(5, w0)))
viz(signal.remez(Ntaps, *remezBands(4, w0)))
viz(signal.remez(Ntaps, *remezBands(3, w0)))
viz(signal.remez(Ntaps, *remezBands(2, w0)))
plt.grid()
plt.figure()
viz(remezDesign(5, w0))
viz(remezDesign(4, w0))
viz(remezDesign(3, w0))
viz(remezDesign(2, w0))
plt.grid()
plt.figure()
viz(remezDesign(4, w0))
viz(firwin2Design(4, w0))
viz(firlsDesign(4, w0))
plt.grid()
b, a = signal.butter(9, 0.8)
w, h = signal.freqz(b, a)
plt.figure()
plt.plot(w, db20(h))
x = np.random.randn(100)
y = signal.lfilter(b, a, x)
L = 4
z = np.vstack([y] + [np.zeros_like(y)] * (L - 1))
z = z.T.ravel()
b = remezDesign(L, 0.9 * np.pi)
v = signal.lfilter(b, 1, z)
plt.figure()
plt.plot(np.fft.rfftfreq(1024), db20(np.fft.rfft(x, 1024)))
plt.plot(np.fft.rfftfreq(1024), db20(np.fft.rfft(y, 1024)))
plt.plot(np.fft.rfftfreq(1024) * L, db20(np.fft.rfft(z, 1024)))
plt.plot(np.fft.rfftfreq(1024) * L, db20(np.fft.rfft(v, 1024)))
plt.grid()
delay = (b.size - 1) / (L * 2.0)
tx = np.arange(x.size + 0.0)
tv = np.arange(v.size + 0.0) / L - delay
plt.figure()
plt.plot(tx, y, 'o--')
plt.plot(tv, v * L, '.-')