MMDVM/Tools/FMGenerateFilterCoefficients.py

46 lines
1.1 KiB
Python
Raw Normal View History

2020-04-24 09:10:50 +02:00
# based on https://github.com/berndporr/iir_fixed_point/blob/master/gen_coeff.py
import numpy as np
import scipy.signal as signal
import pylab as pl
# Calculate the coefficients for a pure fixed point
# integer filter
# sampling rate
fs = 24000
# cutoffs
f1 = 300
f2 = 2700
# ripple
rp = 0.2
# scaling factor in bits, do not change !
2020-05-24 07:44:38 +02:00
q = 15
2020-04-24 09:10:50 +02:00
# scaling factor as facor...
scaling_factor = 2**q
# let's generate a sequence of 2nd order IIR filters
2020-04-29 20:43:01 +02:00
sos = signal.cheby1(3,rp,[f1, f2],'bandpass', output='sos', fs=fs)
2020-05-24 07:44:38 +02:00
#os = signal.cheby1(4, rp, f2, 'lowpass', output='sos', fs=fs) #deemphasis filter
2020-04-29 20:43:01 +02:00
#sos = signal.cheby1(1, rp, 2122, 'highpass', output='sos', fs=fs) #deemphasis filter
2020-04-24 09:10:50 +02:00
2020-05-24 07:44:38 +02:00
sosrounded = np.round((sos) * scaling_factor)
2020-04-24 09:10:50 +02:00
# print coefficients
2020-05-24 07:44:38 +02:00
for biquad in sosrounded:
2020-04-24 09:10:50 +02:00
for coeff in biquad:
2020-05-24 07:44:38 +02:00
print(int(coeff),",",sep="",end="")
#print((coeff),",",sep="",end="")
2020-04-24 09:10:50 +02:00
print("")
# plot the frequency response
b,a = signal.sos2tf(sos)
2020-05-24 07:44:38 +02:00
w,h = signal.freqz(b,a, worN=2048)
2020-04-24 09:10:50 +02:00
pl.plot(w/np.pi/2*fs,20*np.log(np.abs(h)))
pl.xlabel('frequency/Hz');
pl.ylabel('gain/dB');
2020-05-24 07:44:38 +02:00
pl.ylim(top=1,bottom=-30);
2020-04-24 09:10:50 +02:00
pl.xlim(left=250, right=12000);
pl.show()