TExcelTAD
Home › Signals, Systems & Digital Filters for Engineers › Frequency Response, Phase and Group Delay

Frequency Response, Phase and Group Delay

A thermocouple amplifier sampled at 1 kSa/s1\ \text{kSa/s} picks up 50 Hz50\ \text{Hz} mains hum. A colleague suggests averaging 20 samples. Another suggests 16, "because powers of two are faster". Which one removes the hum, how much does each one dent a 10 Hz10\ \text{Hz} signal, and how late does the filtered reading arrive? Time-domain reasoning gets stuck here; the frequency response answers all three questions at a glance. This lesson computes it for digital filters with scipy.signal.freqz, reads magnitude in dB, phase and group delay, and measures it the way you would on a bench. The underlying theory is in Frequency Response; the analog version with Bode plots is in Circuit Theory.

Sinusoids in, scaled sinusoids out

A complex exponential is an eigenfunction of every LTI system: put in x[n]=ejΩnx[n] = e^{j\Omega n} and convolution gives

y[n]=∑kh[k] ejΩ(n−k)=H(ejΩ) ejΩn,H(ejΩ)=∑kh[k] e−jΩky[n] = \sum_k h[k]\,e^{j\Omega (n - k)} = H(e^{j\Omega})\,e^{j\Omega n}, \qquad H(e^{j\Omega}) = \sum_k h[k]\,e^{-j\Omega k}

A real sinusoid Acos⁡(Ωn+ϕ)A\cos(\Omega n + \phi) therefore leaves as A∣H∣cos⁡(Ωn+ϕ+∠H)A\lvert H \rvert\cos(\Omega n + \phi + \angle H): same frequency, scaled amplitude, shifted phase. Here Ω=2πf/fs\Omega = 2\pi f/f_s is the frequency in radians per sample. Three consequences matter in practice:

  • H(ejΩ)H(e^{j\Omega}) is periodic with period 2π2\pi (i.e. fsf_s): a digital filter cannot tell ff from f+fsf + f_s, the aliasing of Sampling. For real hh it is also conjugate-symmetric, so only 0≤f≤fs/20 \le f \le f_s/2 needs plotting.
  • Gains are quoted in decibels, 20log⁡10∣H∣20\log_{10}\lvert H \rvert: −3 dB-3\ \text{dB} is 0.7080.708, −20 dB-20\ \text{dB} is 0.10.1, −40 dB-40\ \text{dB} is 0.010.01.
  • The group delay τg(Ω)=−d∠HdΩ\tau_g(\Omega) = -\frac{d\angle H}{d\Omega} (samples) is the delay of the envelope of a narrow-band signal near Ω\Omega. A filter with constant group delay (linear phase) delays every frequency equally, so pulse shapes survive; a symmetric FIR filter with MM taps has τg=(M−1)/2\tau_g = (M - 1)/2 samples at every frequency.

For a recursive filter, H(ejΩ)=∑kbke−jΩk∑kake−jΩkH(e^{j\Omega}) = \frac{\sum_k b_k e^{-j\Omega k}}{\sum_k a_k e^{-j\Omega k}}, the ratio of the coefficient polynomials evaluated on the unit circle (made precise by the z-transform).

Worked example: the mains-rejecting average

Given: fs=1 kSa/sf_s = 1\ \text{kSa/s}, an MM-sample moving average h[n]=1/Mh[n] = 1/M, n=0…M−1n = 0 \ldots M-1, with M=20M = 20 or M=16M = 16.

Find: the gain at 50 Hz50\ \text{Hz} and 10 Hz10\ \text{Hz}, and the delay.

Solve: summing the geometric series,

H(f)=sin⁡(πMf/fs)Msin⁡(πf/fs) e−jπ(M−1)f/fsH(f) = \frac{\sin(\pi M f/f_s)}{M\sin(\pi f/f_s)}\,e^{-j\pi (M-1) f/f_s}
  • M=20M = 20: zeros wherever Mf/fsMf/f_s is a whole number, i.e. at 50,100,150,… Hz50, 100, 150, \ldots\ \text{Hz}: the hum and all its harmonics are rejected. At 10 Hz10\ \text{Hz}: sin⁡(0.2π)/(20sin⁡(0.01π))=0.9356\sin(0.2\pi)/(20\sin(0.01\pi)) = 0.9356, −0.58 dB-0.58\ \text{dB}. Delay (M−1)/2=9.5(M-1)/2 = 9.5 samples =9.5 ms= 9.5\ \text{ms}.
  • M=16M = 16: zeros at multiples of 62.5 Hz62.5\ \text{Hz}, so 50 Hz50\ \text{Hz} is not on a zero; see the code for how much survives.

Check: an average over exactly one mains period integrates a full sine cycle to zero, which is the same statement in the time domain. In a country with 60 Hz60\ \text{Hz} mains this filter gives only −16.1 dB-16.1\ \text{dB} at 60 Hz60\ \text{Hz}; average M=fs/60M = f_s/60 samples instead (not an integer at 1 kSa/s1\ \text{kSa/s}, so change fsf_s to 1.2 kSa/s1.2\ \text{kSa/s}).

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import freqz, group_delay

fs = 1000.0
f_check = [10.0, 50.0, 100.0, 60.0]
fig, ax = plt.subplots(figsize=(7, 3.2))
for M in (20, 16):
    b = np.ones(M) / M
    _, H = freqz(b, 1, worN=f_check, fs=fs)
    gains = ", ".join(f"{f:.0f} Hz {20 * np.log10(max(abs(h), 1e-12)):7.1f} dB" for f, h in zip(f_check, H))
    _, gd = group_delay((b, 1), w=[10.0, 30.0], fs=fs)
    print(f"M = {M}: {gains}; group delay {gd[0]:.1f} samples at 10 Hz, {gd[1]:.1f} at 30 Hz")
    f, Hf = freqz(b, 1, worN=2048, fs=fs)
    ax.plot(f, 20 * np.log10(np.maximum(np.abs(Hf), 1e-6)), label=f"{M}-sample average")
ax.set(xlabel="frequency (Hz)", ylabel="gain (dB)", ylim=(-60, 3), xlim=(0, 500))
ax.axvline(50, color="gray", ls=":")
ax.legend(); ax.grid(True, alpha=0.3)
plt.tight_layout(); plt.show()
M = 20: 10 Hz    -0.6 dB, 50 Hz  -240.0 dB, 100 Hz  -240.0 dB, 60 Hz   -16.1 dB; group delay 9.5 samples at 10 Hz, 9.5 at 30 Hz
M = 16: 10 Hz    -0.4 dB, 50 Hz   -12.6 dB, 100 Hz   -14.3 dB, 60 Hz   -27.6 dB; group delay 7.5 samples at 10 Hz, 7.5 at 30 Hz

The −240 dB-240\ \text{dB} is the floor max(abs(h), 1e-12) puts on an exact zero. The 16-sample average leaves about a quarter of the hum (−12.6 dB-12.6\ \text{dB}) and is the wrong choice despite being faster to divide. The group delay is the same at every frequency ((M−1)/2(M-1)/2 samples), as for every symmetric FIR filter. Do not ask for it exactly on a zero of HH (for M=20M = 20: 50,100,… Hz50, 100, \ldots\ \text{Hz}): there the phase jumps by π\pi, the group delay is undefined and group_delay warns and returns nonsense.

Measuring a frequency response

A frequency response can be measured exactly the way a network analyser does it: apply one sine at a time, wait for the transient to end, and compare amplitude and phase of output and input. Here the "device" is a recursive exponential smoother, whose group delay, unlike the average's, varies with frequency:

python · try it yourself
import numpy as np
from scipy.signal import lfilter, freqz, group_delay

fs, a = 1000.0, 0.9
b_s, a_s = [1 - a], [1, -a]                       # y[n] = 0.9 y[n-1] + 0.1 x[n]
n = np.arange(4000)
print(" f (Hz)  measured |H| dB  phase deg   freqz |H| dB  phase deg   group delay (samples)")
for f in (1.0, 10.0, 50.0, 200.0):
    x = np.cos(2 * np.pi * f * n / fs)
    y = lfilter(b_s, a_s, x)[2000:]               # discard the start-up transient
    e = np.exp(-2j * np.pi * f * n[2000:] / fs)   # lock-in: correlate with the test tone
    Hm = (y @ e) / (x[2000:] @ e)
    _, H = freqz(b_s, a_s, worN=[f], fs=fs)
    _, gd = group_delay((b_s, a_s), w=[f], fs=fs)
    print(f"{f:7.0f}  {20 * np.log10(abs(Hm)):14.2f}  {np.degrees(np.angle(Hm)):9.2f}   {20 * np.log10(abs(H[0])):11.2f}  {np.degrees(np.angle(H[0])):9.2f}   {gd[0]:6.2f}")
 f (Hz)  measured |H| dB  phase deg   freqz |H| dB  phase deg   group delay (samples)
      1           -0.02      -3.24         -0.02      -3.24     8.97
     10           -1.32     -29.04         -1.32     -29.04     6.51
     50           -9.92     -62.62         -9.92     -62.62     0.47
    200          -20.98     -49.86        -20.98     -49.86    -0.42

The "measured" and computed values agree to the printed digits. The smoother delays slow changes by 9 samples (a/(1−a)a/(1-a)) but fast ones much less, so a sharp edge comes out smeared and skewed: a non-linear-phase filter distorts waveforms even where it barely attenuates.

Guided lab: measure the 20-sample average on a microcontroller

Hand calculation. With fs=1 kSa/sf_s = 1\ \text{kSa/s} and M=20M = 20: −0.58 dB-0.58\ \text{dB} at 10 Hz10\ \text{Hz} (a 1 V1\ \text{V} amplitude becomes 0.936 V0.936\ \text{V}), a null at 50 Hz50\ \text{Hz}, −16.1 dB-16.1\ \text{dB} at 60 Hz60\ \text{Hz}, −3.9 dB-3.9\ \text{dB} at 25 Hz25\ \text{Hz}.

Simulation. Change f_check to [25.0, 50.5] in the first program: 50.5 Hz50.5\ \text{Hz} is attenuated by only about 40 dB40\ \text{dB}. Notches are narrow.

What you would measure. Run the average on a board sampling at 1 kSa/s1\ \text{kSa/s} from a timer interrupt, feed sines from a function generator (1 V1\ \text{V} amplitude on a 1.5 V1.5\ \text{V} offset, i.e. inside 00 to 3 V3\ \text{V}) and log the output's amplitude over the serial port. Expect 0.930.93 to 0.94 V0.94\ \text{V} at 10 Hz10\ \text{Hz}. The 50 Hz50\ \text{Hz} notch depth depends on how exactly the generator and the board's timer agree: a 0.1 Hz0.1\ \text{Hz} mismatch (a 0.2 %0.2\,\% clock error) limits it to about −54 dB-54\ \text{dB}, and ADC noise sets a floor of a few LSB. Real mains frequency drifts too: in Continental Europe it stays within ±0.05 Hz\pm 0.05\ \text{Hz} most of the time, with ±0.2 Hz\pm 0.2\ \text{Hz} as the steady-state limit (EU System Operation Guideline 2017/1485, Annex III; other grids differ), so the rejection of real hum is tens of dB, not infinite.

Hazards and safe setup. Never connect the board to mains to "test with real hum": use the generator. USB power, signals kept within 00 to 3 V3\ \text{V} (checked on a scope first), 1 kΩ1\ \text{k}\Omega series resistor at the ADC pin.

Pitfalls

  • Plotting ∣H∣\lvert H \rvert against Ω\Omega without converting to hertz with the actual fsf_s (freqz(..., fs=fs) does it for you).
  • Calling a filter "linear phase" because its phase plot looks straight in a narrow band.
  • Reading group delay at a frequency where ∣H∣=0\lvert H \rvert = 0.
  • Using 20log⁡1020\log_{10} for power ratios (power uses 10log⁡1010\log_{10}).

Practice

  1. A 4-sample average runs at fs=200 Sa/sf_s = 200\ \text{Sa/s}. Where are its nulls, and what is its delay in milliseconds?
  2. A filter gives ∣H∣=0.5\lvert H \rvert = 0.5 at some frequency. What is that in dB?
  3. A symmetric FIR filter has 41 taps and runs at 48 kSa/s48\ \text{kSa/s}. What is its group delay?
Answer
  1. Multiples of fs/M=50 Hzf_s/M = 50\ \text{Hz} (50 and 100 Hz100\ \text{Hz} up to fs/2f_s/2); delay 1.51.5 samples =7.5 ms= 7.5\ \text{ms}.
  2. 20log⁡100.5=−6.02 dB20\log_{10}0.5 = -6.02\ \text{dB}.
  3. 2020 samples =20/48 000 s=0.417 ms= 20/48\,000\ \text{s} = 0.417\ \text{ms}.

Test yourself

A 20-sample moving average at 1 kSa/s has nulls at

  1. 20 Hz and its multiples
  2. 50 Hz and its multiples
  3. only 500 Hz
  4. it has no nulls

Group delay is defined as

  1. −∠H/Ω-\angle H/\Omega
  2. −d∠H/dΩ-d\angle H/d\Omega
  3. ∣H∣\lvert H \rvert in dB
  4. the length of hh

The frequency response of a digital filter repeats every

  1. fs/2f_s/2
  2. fsf_s
  3. 2fs2f_s
  4. it never repeats

Why does a non-linear-phase filter distort a pulse even in its passband?

  1. it adds noise
  2. different frequency components are delayed by different amounts
  3. it is unstable
  4. it changes the sampling rate