The leaky integrator of Difference equations (6.1) gets four test sines, one after another. Watch the gain fall as the frequency grows, and the output squares lag the input dots by less and less.
Test sines through the leaky integrator
y[n] = 0.5y[n−1] + 0.5x[n], from 6.1. Each test sine has been playing for a long time.
Ω = 0.25π: the output is the same wave, 0.679 times as big and 28.7° late.
Describe this picture
Three panels for the leaky integrator , fed test sines that have been playing for a long time. The top panel shows the test sine as stems with dot heads and the output as stems with square heads, side by side at each sample. Below it, a gain panel, , and a phase panel, in degrees, share the axis in rad/sample. Each test leaves a filled dot in the gain panel and a filled diamond in the phase panel, labelled with its value. There is no control.
The readouts are the test frequency, the gain and the phase. At they read 0.679 and −28.7°: the output is the same wave, 0.679 times as big and 28.7° late. At 4.75 s, gives 0.447 and −26.6°. At 8.25 s, gives 0.357 and −14.6°. At 11.25 s, gives 0.333 and no shift: the input alternates, and the output alternates at a third of its size. At the end a thin curve in each panel, labelled “from the equation”, passes through the stamped marks, and the caption says the four tests lie on , read straight off the difference equation.
A test sine goes in, a scaled and shifted sine comes out
On the page Properties of LTI systems (5.4) you found that an arrow goes through a linear time-invariant system and comes out as the same arrow times one number. That number is , the DTFT of the impulse response from The DTFT (12.2). It is the system’s frequency response. This page measures it, reads it off a difference equation, and then asks what its angle does to a signal.
A real signal is not a single arrow. From Properties of the DTFT (12.3), a cosine is two half arrows, at and at , and a real gives . The two outputs are therefore a pair of conjugates, and they add to a cosine:
where and are the size and angle of at that . This holds once the test sine has been playing for a long time, so that the start-up part of the output has died away. I call that the steady state. A negative means the output crests come later than the input crests.
To get from a difference equation, put and into the leaky integrator, . The delayed output is , so
In general, with , the response is divided by . That is what SciPy’s freqz(b, a) computes. Its size is , because .
The four tests in the picture at the top lie on this , and the picture ends by drawing it through them. The last test, at , is the sine , which alternates between and . The phase is at , dips to a minimum of at , and returns to at .
The gain falls from 1 at to at , so the leaky integrator keeps slow waves and weakens fast ones. In decibels (1.3, How big is a signal) the four tests read , , and dB.
A moving average, probed
A moving average replaces each sample by the mean of the last few. The 4-point version is . If a sine fits a whole number of cycles into those four samples, the four values always cancel. So I expect the output to be exactly zero for some test sines, and nearly the input for slow ones. Watch the output go flat at 0 for two of the five tests.
A moving average, probed
y[n] = (x[n] + x[n−1] + x[n−2] + x[n−3])/4. Gain only.
Ω = 0.125π, a slow wave: the average of four samples is nearly the sample itself. Gain 0.906.
Describe this picture
Two stacked panels for : the signals, as in the picture at the top, and the gain, with no phase panel. The readouts are the test frequency and the gain. The 16 s clip plays five tests and then holds on its end frame.
At , a slow wave, the average of four samples is nearly the sample itself: gain 0.906 (the readout shows 0.13π rad/sample). At the gain is 0.653. At , one cycle every 4 samples, the four samples in the average always add to 0: gain 0. At the gain is 0.271, and a little comes through. At , and pairs cancel: gain 0. The final caption reads “A low-pass: gain 1 at Ω = 0, and 0 wherever a whole number of cycles fits in the 4 samples, at 0.5π and π.”
When the clip has finished, a slider named “Test frequency Ω” chooses any test frequency from 0 to : drag across the gain panel, or use the arrow keys. An open ring labelled “from the formula” marks your test on the curve, and the caption gives its gain, for example “Ω = 0.30π: gain 0.524.”
Notice that the output is flat at 0 for and . When the clip has finished, drag across the gain panel to try any test frequency from 0 to .
Now the formula. Each term is a delay, so . This is a short geometric sum, and it folds into
The fraction is the pulse spectrum of 12.2, and the factor is its centre shifted by 1.5 samples. The numerator is zero at and , where the denominator is not. For points the zeros sit at .
That is why a 12-month rolling mean removes a yearly pattern completely. One cycle per year is rad per month, which is the first zero. A 7-day mean puts its first zero at rad per day, and removes the weekly cycle from daily counts.
One more system, the first difference from Oversampling and noise shaping (11.3). There you fed it four test sines and plotted the dots. Its frequency response is
so . This is the curve that page promised. The four dots of 11.3 sit on it.
The squared curve is the power gain: , , , and at , , , and . The curve rises with , so the first difference is a high-pass system, the opposite of the moving average.
The ideal low-pass cannot be built
What would a perfect low-pass look like? It would keep every frequency below a cutoff at gain 1 and remove all the others. In discrete time it is repeated every , so on it is for and 0 elsewhere. Its impulse response is the inverse DTFT (12.2),
The stems are drawn for . They shrink as but never stop, in either direction. So is not causal and not FIR (5.4): a system that responds before the input arrives, and with infinitely many taps, cannot be run. This is the discrete twin of the brick wall in Frequency response and Bode plots (8.4).
Delay and cut it off, and you get a filter you can run. The cut produces a ripple at the edge of the passband, the same overshoot as cutting a series short in Convergence and the Gibbs phenomenon (7.3). Designing with that trade is the subject of Window-method FIR design (19.1).
Straight phase, bent phase
Phase is a delay in disguise. In Frequency response and Bode plots (8.4, “Two delays”) you measured it in seconds. In samples, the phase delay is
the lateness of the crests inside a signal. The group delay is
the lateness of the envelope. Both use the unwrapped phase, the angle drawn without its jumps of (12.2). The leaky integrator, for example, has , , and samples at , , and .
A pure delay of samples has . Its phase is the straight line , so at every and the shape passes through unchanged. A filter whose phase is a straight line is linear phase.
Two short FIR filters show the difference. The triangle is the one from 12.3. Its response is
The bracket is squared, so it is never negative, and the phase is exactly . The ramp is not symmetric, and its phase bends. Watch the two group delays, minus the slopes of the phases: the triangle’s stays at 2.00 samples, and the ramp’s does not.
Straight phase, bent phase
Unwrapped phase of the triangle (1, 2, 3, 2, 1)/9 and the ramp (1, 2, 3)/6. Group delay τ_g is minus the slope.
At Ω = 0 both phases start at 0. The triangle's slope gives τ_g = 2 samples, the ramp's 1.33.
Describe this picture
One panel: the unwrapped phase in degrees against in rad/sample, for the triangle and the ramp . The triangle is a solid straight line and the ramp a dashed curve, each labelled. A dotted vertical line marks the current , and on each curve a short thick segment touches it, solid on the triangle and dashed on the ramp. The readouts are the group delay of each.
At both phases start at 0, and the delays read 2.00 samples for the triangle and 1.33 for the ramp. At 4.75 s, at , they read 2.00 and 1.43. At the end, at , they read 2.00 and 2.93, and the caption says “A straight phase delays every frequency equally; a bent one delays each by its own amount, and a mix of them changes shape.” When the clip has finished, a slider named “Frequency Ω” moves the dotted line: drag it, or use the arrow keys. At the caption reads “Ω = 0.60π: triangle 2.00, ramp 2.61 samples.”
The ramp’s principal angle jumps from to at . Unwrapped, it runs on smoothly to at . When the clip has finished, drag the dotted line to read both delays at any ; at the ramp delays by 2.61 samples.
A bent phase delays each frequency by its own amount, so a signal made of several frequencies comes out reshaped. This is phase distortion. Picture runners who all keep the same pace: they cross the line in formation. Give each a different pace and the formation breaks up.
Linear phase comes from a symmetric , and Linear-phase systems (17.3) builds on that. You can check it on the triangle. Its output for equals exactly, from on. The gain is the bracket squared at , and the delay is the 2 samples of the straight line.
Worked example
Take the leaky integrator and read its response at by hand. Here , so . Multiply top and bottom by : . Its size is , and its angle is . This matches the second test in the picture at the top.
Now run the 4-point moving average from rest on . The samples are , so the outputs are , , then from on. The start-up part lasts only while the four-sample window is filling. After that the output is exactly the steady-state value, 0, which the gain of 0 predicted.
For the first difference at , , so a sine at that frequency comes out the same size. At the gain is , close to the largest value, 2, at .
The ramp at has gain and group delay samples. Its largest group delay, samples, is at . The triangle delays everything by exactly 2.
Where you will meet this
Every filter datasheet in discrete time is a plot of this page, a gain curve and a phase or delay curve against . The moving average is the simplest smoother, and Simple smoothing filters (18.2) compares it with others. The poles and zeros that shape such curves come in Transfer functions, poles & zeros (16.3). The ideal low-pass returns, cut down to a usable length, in Window-method FIR design (19.1).
The maths behind it · eigenvalues
The frequency response is the list of a system’s eigenvalues, one for each arrow (5.4). In The DFT as a matrix (13.5), the DFT turns the system’s matrix, for signals of points, into a diagonal matrix holding these numbers.
The maths behind it · rolling means
A rolling mean of points in a time series is this moving average. Its zeros at are why a 12-month rolling mean removes a yearly pattern exactly, and why a 7-day mean removes the weekly cycle from daily counts.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Frequency response | the DTFT of | |
| Test sine | real , steady state | |
| From a difference equation | , | SciPy freqz(b, a) |
| Leaky integrator | gain 1 at 0, at | |
| -point moving average | low-pass, zeros at | |
| First difference | , size | 11.3’s curve |
| Ideal low-pass | for ; | never ends either way: not buildable |
| Phase delay | samples; the crests | |
| Group delay | samples; unwrapped phase; the envelope | |
| Linear phase | for all ; symmetric |