-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_lpf.py
executable file
·73 lines (63 loc) · 1.76 KB
/
make_lpf.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
#!/usr/bin/env python3
import numpy
from scipy.signal import firwin, remez, kaiser_atten, kaiser_beta
def create_filter(rate, tone, taps, bandwidth, window="hamming"):
# rate = rate * 0.5
# tone = tone / rate
# bandwidth = bandwidth / rate
fl = tone - (bandwidth / 2)
fh = tone + (bandwidth / 2)
return firwin(
numtaps=taps,
cutoff=bandwidth,
fs=rate,
pass_zero='lowpass',
window=window,
scale=True,
)
def bandpass_kaiser(ntaps, lowcut, highcut, fs, width):
nyq = 0.5 * fs
atten = kaiser_atten(ntaps, width / nyq)
beta = kaiser_beta(atten)
taps = firwin(
ntaps,
[lowcut, highcut],
nyq=nyq,
pass_zero="bandpass",
window=("kaiser", beta),
scale=True,
)
return taps
def bandpass_remez(ntaps, lowcut, highcut, fs, width):
delta = 0.5 * width
edges = [
0,
lowcut - delta,
lowcut + delta,
highcut - delta,
highcut + delta,
0.5 * fs,
]
taps = remez(ntaps, edges, [0, 1, 0], Hz=fs)
return taps
taps = 16
rate = 62500
tone = 20840
bandwidth = 100
coefficients = create_filter(rate=rate, tone=tone, bandwidth=bandwidth, taps=taps, window="hamming")
# coefficients = bandpass_remez(taps, (1000 - bandwidth / 2), (1000 + bandwidth / 2), rate, 1.0)
# coefficients = bandpass_kaiser(taps, (1000 - bandwidth / 2), (1000 + bandwidth / 2), rate, 1.0)
print(
"""// Filter defined in `make_coefficients.py` with the following parameters:
// taps: {}
// rate: {}
// tone: {}
// bandwidth: {}
#define LPF_STAGES {}
static float lpf_coefficients[LPF_STAGES] = {{""".format(
taps, rate, tone, bandwidth, taps
)
)
for coef in coefficients:
print(" {}f,".format(coef))
print("};")