importnumpyasnpimportmatplotlib.pyplotaspltfromscipy.signalimportfreqzfromfdlsimportfdls_designNUMTAPS=4096SAMPLERATE=44100# create a desired magnitude spectrumfreq=np.linspace(0,SAMPLERATE/2,NUMTAPS)phi=np.cumsum(np.logspace(2,0,NUMTAPS))*2*np.pi/SAMPLERATEmagnitude=0.5*np.cos(phi)-freq/SAMPLERATE+1# FDLS filter design of desired magnitude spectrum # with minimum phase calculated from magnitude spectrumbcoef,acoef=fdls_design(ma_order=12,ar_order=12,magnitude=magnitude,phase=None)plt.semilogx(freq,20*np.log10(magnitude))freqz(bcoef,acoef,NUMTAPS,fs=SAMPLERATE,plot=lambdaw,h:plt.semilogx(w,20*np.log10(np.abs(h)),'--'))plt.legend(['desired','fdls design'])plt.grid(True)plt.xlabel('Frequency in Hz')plt.ylabel('Magnitude in dB(FS)')plt.show()
Returns b,a filter coefficients (ARMA model) using Frequency Domain Least Squares method
Parameters:
ma_order (int) – Order of b coefficients i.e. numerator. Moving average part of the model.
ar_order (int) – Order of a coefficients i.e. denominator. Autregressive part of the model.
magnitude (ArrayLike[N]) – Magnitude of desired frequency response.
phase (None or ArrayLike[N]) – Phase of desired frequency response. If None, a minimum phase is calculated from the provided magnitude.
weights (None or ArrayLike[N]) – Frequency weighting or LS error.
Returns:
b, a Coefficients of numerator (optimal in least squares sense).
Return type:
b_coeffs, a_coeffs (Tuple[ndarray, ndarray])
Raises:
ValueError – If magnitude or phase are not real valued or not of the same length.
ValueError – If magnitude, phase (if given) and weights (if given) are not of the same length.
References
G. Berchin, “Precise Filter Design [DSP Tips & Tricks],” in IEEE Signal Processing Magazine,
vol. 24, no. 1, pp. 137-139, Jan. 2007,
DOI: 10.1109/MSP.2007.273077.
Michael A. Soderstrand, Gregory Berchin & Randy S. Roberts
(1995) Frequency domain least-squares system identification, international journal of
electronics, 78:1, 25-35,
DOI: 10.1080/00207219508926137