Welcome to fdls’s documentation!

This package implements the Frequency Domain Least Squares filter desing method.

Example

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import freqz

from fdls import fdls_design

NUMTAPS = 4096
SAMPLERATE = 44100

# create a desired magnitude spectrum
freq = np.linspace(0, SAMPLERATE/2, NUMTAPS)
phi = np.cumsum(np.logspace(2, 0, NUMTAPS))*2*np.pi/SAMPLERATE
magnitude = 0.5*np.cos(phi) - freq/SAMPLERATE + 1

# FDLS filter design of desired magnitude spectrum 
# with minimum phase calculated from magnitude spectrum
bcoef, 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=lambda w,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()

(Source code, png, hires.png, pdf)

_images/example2.png

Implementation of the Frequency Domain Least Squares Filter Design Method according to Soderstrand and Berchin.

fdls.magnitude_to_minimumphase(magnitude, twosided=False)[source]

Returns minimumphase for given onesided magnitude spectrum.

Parameters:
  • magnitude (ArrayLike) – Magnitude spectrum (expects onesided magnitude spectrum by default)

  • twosided (bool) – Set to True if your given magnitude spectrum is twosided. Default: False

Returns:

Minimum phase (onesided) for given magnitude.

Return type:

phase (ndarray)

fdls.fdls_design(ma_order, ar_order, magnitude, phase, weights=None)[source]

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

Indices and tables