-
Notifications
You must be signed in to change notification settings - Fork 0
/
fir_taps
executable file
·48 lines (36 loc) · 1.23 KB
/
fir_taps
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
#!/usr/bin/python3
import sys
import argparse
import numpy as np
def lowpass(w_c, N, start=None):
a = np.arange(N) - (float(N - 1) / 2)
taps = np.sin(a * w_c) / a / np.pi
if N % 2 == 1:
taps[N/2] = w_c / np.pi
return taps
WINDOW_FUNC = {
'rectangular': lambda a: np.zeros(a) + 1,
'hamming': np.hamming,
'hanning': lambda a: np.hanning(a + 2)[1:-1]
}
def window_by_name(name):
try:
return WINDOW_FUNC[name]
except KeyError:
msg = "%r is not a known windowing function" % name
raise argparse.ArgumentTypeError(msg)
def main():
parser = argparse.ArgumentParser(description='Produce taps for a low-pass FIR filter.')
parser.add_argument('-n', metavar='NUMBER', type=int, required=True,
help='number of taps')
parser.add_argument('-r', metavar='RATE', type=float, required=True,
help='sample rate')
parser.add_argument('-c', metavar='FREQ', type=float, required=True,
help='cutoff frequency')
parser.add_argument('-w', metavar='WINDOW', type=window_by_name, required=True,
help='windowing function, one of: ' + ', '.join(WINDOW_FUNC.keys()))
args = parser.parse_args()
taps = lowpass((args.c / args.r) * 2 * np.pi, args.n) * args.w(args.n)
np.savetxt(sys.stdout, taps)
if __name__ == "__main__":
main()