-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlpf_fir_design.py
More file actions
executable file
·82 lines (65 loc) · 2.7 KB
/
lpf_fir_design.py
File metadata and controls
executable file
·82 lines (65 loc) · 2.7 KB
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
#!/usr/bin/python2
import os
from numpy import cos, sin, pi, absolute, arange
from scipy.signal import kaiserord, lfilter, firwin, freqz
from pylab import figure, clf, plot, xlabel, ylabel, xlim, ylim, title, grid, axes, show
import argparse
import shutil
from jinja2 import Environment, Template, FileSystemLoader, select_autoescape
cwd=os.path.dirname(os.path.abspath(__file__))
parser = argparse.ArgumentParser(description="FIR LPF coefficient generator")
parser.add_argument('-name',dest='name',default='lpf0', type=str, help='header file name')
parser.add_argument('-sr',dest='sr',type=int,help='sample rate [Hz]')
parser.add_argument('-tbw',dest='tbw',type=float,help='transition bandwidth (normalized to sample_rate/2) 0.0 -> 1.0')
parser.add_argument('-sba',dest='sba',type=float,help='attenuation in stop band (dB)')
parser.add_argument('-cut',dest='cut',type=float,help='cutoff frequency (normalized to sample_rate/2) 0.0 -> 1.0')
# parsing arguments
component = parser.parse_args()
nyq_rate = component.sr/2
component.show = True
env = Environment(
loader = FileSystemLoader(cwd),
autoescape = select_autoescape(['html', 'xml'])
)
coeff_hpp=env.get_template("templates/fir_coeff.tpl")
# PRINT FILTER PARAMETERS
print ('sample rate: '+str(component.sr))
print ('transition bw: '+str(component.tbw))
print ('stop-band attenuation: '+str(component.sba))
print ('cutoff freq: '+str(component.cut))
print ('-------------------------------')
#------------------------------------------------
# Create a FIR filter and apply it to x.
#------------------------------------------------
# Compute the order and Kaiser parameter for the FIR filter.
N, beta = kaiserord(component.sba, component.tbw)
print('resulting filter order: '+str(N))
# Use firwin with a Kaiser window to create a lowpass FIR filter.
taps = firwin(N,component.cut, window=('kaiser', beta))
#------------------------------------------------
# Writes coefficients and metadata to header file
#------------------------------------------------
fir = {}
fir['dtype']='double'
fir['type']='lpf'
fir['name']=component.name
fir['num_coeff'] = N
fir['cutoff']=component.cut
fir['samp_rate']=component.sr
fir['coeff_data']=taps
#source code generation based on the templates
with open('fir_coeff_'+component.name+'.hpp', "w") as fh:
fh.write(coeff_hpp.render(fir=fir))
#------------------------------------------------
# Plot the magnitude response of the filter.
#------------------------------------------------
if component.show:
figure(2)
clf()
w, h = freqz(taps, worN=8000)
plot((w/pi)*nyq_rate, absolute(h), linewidth=2)
xlabel('Frequency (Hz)')
ylabel('Gain')
title('Amplitude Frequency Response')
grid(True)
show()