Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 ยท Filters: FIR

Equiripple and Frequency-Sampling FIR Design

Design optimal FIR filters with the Parks-McClellan equiripple method, least squares (firls) and frequency sampling (firwin2), and compare them with the window method.

The window method is quick, but it wastes taps: the ripple is lowest near the transition band and shrinks away from it, so the filter is better than required in most places and just barely good enough in the worst place. If you want the shortest filter that meets a specification, you need an optimization method. This lesson covers three designs, each with its own definition of "best".

Three ways to specify a filter

All of them take a desired response: a list of frequency bands and the gain wanted in each, leaving gaps (transition bands) where you do not care.

Method Goal SciPy
Frequency sampling Response passes exactly through chosen points signal.firwin2
Least squares Minimum total squared error signal.firls
Equiripple (Parks-McClellan) Minimum of the largest error; ripple of equal height signal.remez
flowchart TB
    A["desired response<br/>(bands, gains)"] --> B["firwin2<br/>interpolate points, inverse FFT, window"]
    A --> C["firls<br/>minimize squared error"]
    A --> D["remez<br/>minimize maximum error"]
    B --> E["symmetric taps<br/>(linear phase)"]
    C --> E
    D --> E

All three produce symmetric taps, so linear phase is kept.

Frequency sampling (firwin2)

The idea: pick the gain you want at a set of frequencies, take the inverse FFT to get a time-domain filter, then window it. It is the best choice for arbitrary shapes such as an equalizer curve.

Python An arbitrary magnitude shape with firwin2

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The gain tracks the sketched curve: boosted by 2 at low frequencies, falling to 1, and a notch at 1600 Hz.

Least squares (firls)

firls minimizes the sum of squared errors over the bands you specify, with optional weights. It gives the lowest average error, and the ripples shrink away from band edges.

Equiripple (remez)

The Parks-McClellan algorithm minimizes the peak error instead, and the solution has the telltale shape of ripples of equal height in the passband and the stopband. For a given number of taps no other linear-phase filter has smaller maximum error, so for a given specification it needs the fewest taps. In SciPy, signal.remez(numtaps, bands, desired, weight=None, fs=...) takes the band edges in pairs.

Python Equiripple low-pass versus the Kaiser design

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the printed table: remez with 65 taps reaches about 59 dB, which is what the Kaiser window needed 75 taps for, and the gap grows for stricter specifications. The firls filter with equal weights has a lower average error but a worse worst-case stopband level (about 49 dB), because it does not aim at the peak error. The price of equiripple is a more complicated algorithm, and a passband ripple tied to the stopband ripple unless you weight the bands.

Weights and band edges

With weight, you tell the optimizer which band matters more. A weight of 10 on the stopband means "make its error ten times smaller than the passband error", which trades passband ripple for stopband attenuation.

Python Weighting the stopband

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A heavier stopband weight raises the attenuation and enlarges the passband ripple, and the sum of the two is bounded by the filter length.

Special filters

remez can also design differentiators and Hilbert transformers (type="differentiator" or "hilbert"). A Hilbert transformer shifts every frequency by 90 degrees and is used to build the analytic signal for demodulation. Antisymmetric taps are the source of the 90 degree phase shift.

Python A Hilbert transformer from remez

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output is the cosine shifted by 90 degrees, which with this design's sign convention is -sin. Negate the taps if you want the opposite sign.

Choosing a design method

  • Need the shortest filter and a hard spec: equiripple (remez).
  • Want smooth, small error on average, simple to reason about: least squares (firls) or the window method.
  • Want a custom magnitude curve (equalizer, compensation): firwin2.
  • Quick, standard low/high/band filters: firwin with a Kaiser or Hamming window.
  • If the design misses the spec, increase the number of taps first. Check the result with freqz every time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the Parks-McClellan (equiripple) algorithm minimize?

    It minimizes the largest weighted error, which yields ripples of equal height.
  2. 2. For which job is firwin2 the most natural tool?

    firwin2 interpolates any list of (frequency, gain) points.
  3. 3. What happens if you raise the stopband weight in remez?

    The optimizer shifts error from the stopband to the passband.
  4. 4. What is typical for the taps of a Hilbert transformer?

    Antisymmetric taps give a constant 90 degree phase shift plus linear phase.

Summary

  • Optimization methods spend taps wisely: equiripple spreads the error evenly and needs the fewest taps.
  • firwin2 interpolates arbitrary gain points, firls minimizes squared error, remez minimizes the maximum error.
  • Weights trade passband accuracy against stopband attenuation.
  • remez also designs Hilbert transformers and differentiators.
  • Always verify a design with freqz, and add taps if it misses the specification.

Next: Fast Convolution: Overlap-Add and Overlap-Save