1 # -*- coding: utf-8 -*-
3 Python code for basis FIR filter design
4 @author: Matti Pastell <matti.pastell@helsinki.fi>
10 import scipy.signal as signal
12 #Plot frequency and phase response
14 w,h = signal.freqz(b,a)
15 h_dB = 20 * log10 (abs(h))
19 ylabel('Magnitude (db)')
20 xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
21 title(r'Frequency response')
23 h_Phase = unwrap(arctan2(imag(h),real(h)))
24 plot(w/max(w),h_Phase)
25 ylabel('Phase (radians)')
26 xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
27 title(r'Phase response')
28 subplots_adjust(hspace=0.5)
30 #Plot step and impulse response
33 impulse = repeat(0.,l); impulse[0] =1.
35 response = signal.lfilter(b,a,impulse)
39 xlabel(r'n (samples)')
40 title(r'Impulse response')
42 step = cumsum(response)
45 xlabel(r'n (samples)')
46 title(r'Step response')
47 subplots_adjust(hspace=0.5)
59 def bandpass(n, freq):
61 a = signal.firwin(n, cutoff = (freq/Fniq)/transition, window = 'boxcar')
62 b = - signal.firwin(n, cutoff = (freq/Fniq)*transition, window = 'boxcar')
64 d = - (a+b); d[n/2] = d[n/2] + 1
65 w,h = signal.freqz(d,1)
68 def c_dump(fond, harm1, other):
75 print "/* Apply the 3 filters on the bitfield. The results are stored in"
76 print " * fil_fond, fil_harm1 and fil_other. */"
77 print "static void apply_filters(void)"
79 print " fil_fond = 0;"
80 print " fil_harm1 = 0;"
81 print " fil_other = 0;"
83 for i in range(len(f)):
84 print " if (bitfield & (1UL << %d)) {"%(i)
85 print " fil_fond += %d;"%(f[i])
86 print " fil_harm1 += %d;"%(h[i])
87 print " fil_other += %d;"%(o[i])
90 print "#ifdef HOST_VERSION"
91 print " fil_fond = saturate16(fil_fond);"
92 print " fil_harm1 = saturate16(fil_harm1);"
93 print " fil_other = saturate16(fil_other);"
99 fond = bandpass(n, Ffond)
100 harm1 = bandpass(n, Fharm1)
101 other = bandpass(n, Fother)
102 c_dump(fond, harm1, other)
109 if len(sys.argv) >= 2: