Take the slope of a noisy signal and the noise usually wins. Below, one noisy sine goes through two filters that take its slope. Watch the bottom panel: one output is buried in noise, and the other lies close to the true slope.
A differentiator that ignores the noise
sin(0.04πn) with noise 0.05 RMS, through the first difference and a 61-tap band-limited differentiator (cutoff 0.2π). Outputs are drawn moved back by their delays, 0.5 and 30 samples.
Describe this picture
Three stacked panels for the sine with noise of 0.05 RMS, sent through the first difference and through a 61-tap band-limited differentiator with its cutoff at . The gain panel plots the gain, from 0 to 2.2, against from 0 to rad/sample: the ideal, , as a thin dashed line, the first difference as a dotted line and the 61-tap filter as a solid line. A small upward open triangle under the axis at , labelled “the sine”, marks where the signal is. The input panel draws the noisy sine as a thin solid line through the samples, for from 0 to 239. The slopes panel, for from 30 to 210, draws the true slope as a thin dashed line, the first difference’s output as a dotted line with small dots and the 61-tap output as a solid line. The outputs are drawn moved back by their delays, 0.5 and 30 samples. The two readouts are each output’s error, the RMS difference from the true slope; each reads “not yet” until its output is drawn.
The clip lasts 13 s. First the three gain curves draw and the input appears: the first difference follows all the way up, and the 61-tap one follows it to about and then falls to 0. Then the first difference’s output draws, and at 7 s its error reads 0.0752: the slope, 0.126 at most, is buried in noise. Last the 61-tap output draws, with an error of 0.0075, ten times smaller. From 13 s an overlay slider on the gain panel, named “Cutoff Ω_c”, sets the cutoff from to in steps of ; its value text reads like “0.20π rad/sample: error 0.0075”. The arrow keys move it by , Page Up and Page Down by , and Home and End jump to the ends. The 61-tap curve and its output redraw at once, and the caption reads like “Cutoff 0.10π: error 0.0025.” The setting is kept in the link, as deriv.wc, in units of .
A slope from samples
Suppose you log a car’s speed once per sample and want its acceleration. Acceleration is the rate of change of speed, its slope. The speedometer jitters a little, and that is the trouble. The jitter changes faster than the speed does, so it is the jitter that changes the slope most.
The design methods of Window-method FIR design (19.1) to Optimal FIR design (19.3) made band filters: pass some frequencies, stop others. On this page the same methods make four other filters. Each one starts from a different ideal response, and that ideal response is the only new thing on each part of the page.
On Properties of the Fourier transform (8.2) you saw that differentiation multiplies the spectrum by . The same holds for samples.
Take with , from a wave with nothing at or above half the sample rate. The slope of at the sample times has the spectrum for . So the ideal differentiator is the filter
where “des” means desired, as in 19.1. Its gain is : a wave twice as fast comes out twice as steep.
The simplest slope you can take from samples is the first difference of Oversampling and noise shaping (11.3). On Frequency response of discrete-time systems (12.4) its gain came out as . For slow waves that is close to : at it is 0.12558, against 0.12566. At it is 2 instead of .
Both curves rise all the way to . That is the problem: the jitter has energy at every frequency, and its fastest parts get the most gain.
A slow signal needs the gain only as far as the signal goes. Above that I want gain 0. A differentiator that follows up to a cutoff and is 0 above it is a band-limited differentiator:
Its taps come from the inverse DTFT of The DTFT (12.2), the same step that gave 12.4 the ideal low-pass:
The second line splits into . The cosine part, , is odd in and integrates to 0, and gives the minus sign. The third line is integration by parts. At the integrand is odd, so .
These taps are antisymmetric, . The window method of 19.1 does the rest. Keep taps and multiply by a window: . I use the Kaiser window with that 19.1 chose for 40 dB, and 61 taps, so .
An antisymmetric filter of odd length is type III of Linear-phase systems (17.3), so its gain is forced to 0 at and at . At that is what a differentiator should do, because a constant has slope 0. At it does no harm as long as is below , because the ideal is 0 there too.
A differentiator that ignores the noise
The picture at the top of the page sends one noisy sine through the first difference and through a 61-tap band-limited differentiator with , and compares both outputs with the true slope.
It draws the outputs moved back by their delays. Why are they moved back? The first difference is antisymmetric with two taps, so it delays by samples (17.3). The 61-tap filter delays by 30.
Look at the input first: the noise is barely visible there. Then look at the first difference: the noise swamps the slope. The sine’s slope is at most 0.126 per sample, and the noise in the output is 0.075 RMS, more than half of that. The 61-tap output lies close to the true slope.
When the clip has finished, drag across the gain panel to set yourself, or use the arrow keys.
Choosing trades noise against signal. At the error falls to 0.0025, because less noise gets through. At it rises to 0.0423, because now the cutoff cuts into the sine itself: the gain at is only 0.066, about half of the 0.126 it should be. Above the error grows again, to 0.0138 at and 0.0319 at .
At the error is 0.0954, worse than the first difference. With the design follows over the whole band. It follows it more closely than the first difference does, so it gives the noise even more gain. Its taps are , and this full-band ideal really wants type IV, like the first difference: it needs gain at , and type III is forced to 0 there (17.3). An even length, with its centre between two samples, avoids that zero.
The maths behind it · least-squares slopes
Differentiating noisy data magnifies the noise, so spectroscopy and chemometrics use Savitzky–Golay filters. They fit a low-degree polynomial to each window of points by least squares and take its slope. That is a band-limited differentiator designed by least squares. Interpolating a time series at times between its samples is the fractional delay at the end of this page.
Half-band: every other tap is zero
Now a low-pass, with one special cutoff. A half-band low-pass has its cutoff at , half of the band from 0 to . The ideal low-pass of 12.4 has the taps and . With they are
At every even other than 0, is the sine of a whole number of half turns, which is 0. So every other tap is 0, like a checkerboard on which half the squares are empty by design. The window cannot undo that, because it multiplies each tap and 0 times anything is 0. The Kaiser window is 1 at its centre, so the centre tap stays 0.5.
The instrument below draws a 23-tap half-band filter, its taps and its gain, and then turns the gain curve half a turn.
Half-band: every other tap is zero
A 23-tap low-pass with its cutoff at 0.5π: the ideal times a Kaiser window, β = 3.40.
Describe this picture
Two stacked panels and no control, for a 23-tap low-pass with its cutoff at : the ideal times a Kaiser window, . The taps panel plots against from −11 to 11. Taps that are not zero are stems with dot heads; zero taps are open circles on the axis, and the first one is labelled “zero”. The gain panel plots the zero-phase response of 17.3 against from 0 to as one solid curve, with a filled diamond at . The readouts are the taps that are 0 and the gain at , which read “not yet” until their part is drawn.
The clip lasts 12 s. First the taps appear from the centre outwards, and at 3.5 s the readout shows that 10 of the 23 taps are 0, at every even except the centre. Then the curve draws, and at 7.25 s the gain at reads 0.500. Next a dashed copy of the curve turns half a turn about the diamond and lands on the curve. The end caption gives : the pass-band ripple mirrors the stop band’s, and only 13 of the 23 taps need a multiplication.
Watch the dashed copy as it turns. It lands on the curve at every point, ripples included. Why? From 17.3, a symmetric filter of odd length has
In a half-band filter only the odd are left in the sum. For odd , , so
Add the two lines and every cosine cancels. Only is left, which is . A ripple of 0.0035 above 1 at some in the pass band is a ripple of 0.0035 above 0 at in the stop band.
Half-band filters are used where a signal’s rate is halved or doubled, in Downsampling and decimation (22.1) and Upsampling and interpolation (22.2). Halving the rate throws away half of the outputs, and half of the taps are zero. So a cascade of half-band stages is the cheapest way to lower a rate by a power of 2.
A Hilbert transformer
The third filter, in its ideal form, changes no sizes. It turns every cosine into a sine. A Hilbert transformer has the ideal response
Its gain is 1 everywhere except at 0 and . Its phase is , a quarter turn back, for positive frequencies. A cosine is two arrows, . The filter multiplies the first arrow by and the second by , which gives .
Its taps are again an inverse DTFT. Folding the negative frequencies onto the positive ones, as for the differentiator, leaves
For odd , and the taps are . For even , including , they are 0.
The taps are antisymmetric, and with an odd length that is type III again: its gain is forced to 0 at 0 and at (17.3). Its phase is . That is 17.3’s generalised linear phase with , because here the real factor of 17.3 is negative.
The ideal jumps from to at , and again at . No filter with a finite number of taps can jump. So the gain of a real design must fall to 0 near both ends, and type III makes it reach 0 exactly there. The figure shows a 31-tap design, windowed like the others.
The figure is static, because The Hilbert transform and the analytic signal (27.2) gives the Hilbert transformer a page of its own. There it turns a real signal into a complex one whose size is the signal’s envelope.
The maths behind it · rotations in a plane
For each frequency, the cosine and the sine at that frequency span a two-dimensional space. The Hilbert transformer is a rotation in that plane: it maps to and to , a quarter turn ( rad), so applied twice it gives . The differentiator acts on the same planes as a quarter turn combined with a stretch by .
Delay by a fraction of a sample
The last filter does nothing to a signal but delay it. Delaying by a whole number of samples is easy: . But what if two microphones are 7.25 samples apart, and you want to line up their signals?
A delay by a number of samples that is not a whole number is a fractional delay. Its output should be the wave read between the samples, .
On Reconstruction (10.3) you rebuilt from its samples by adding a sinc pulse at every sample. Read that sum at the time , with :
That is a convolution of with the taps : samples of a sinc centred at . The sinc never ends, so I keep 16 taps and taper them with a Hann window centred at too, for . The taps are at , for to 15.
Below, moves from 7 to 7.25 and then to 7.5.
Delay by a fraction of a sample
16 taps of a sinc centred at n_0, with a Hann window centred there too. Test signal cos(0.2πn).
n_0 = 7, a whole number: every tap is 0 except h[7] = 1. A plain delay, and the output samples sit on the input's.
Describe this picture
Two stacked panels for 16 taps of a sinc centred at , with a Hann window centred there too, and the test signal . The taps panel plots against the sample from 0 to 15: the taps as stems with dot heads, the continuous windowed sinc as a dashed curve labelled “shifted sinc”, and a dotted vertical line at . The signal panel, for from 16 to 40, draws the wave as a thin dashed curve, the input samples as dots and the output samples as filled squares, moved back by . The readouts are the delay , in samples to 2 decimals, and the largest error, the largest gap between an output sample and the wave, to 5 decimals.
The clip lasts 12 s. It holds first: every tap is 0 except , a plain delay, and the output samples sit on the input’s, with an error of 0.00000. Then the sinc and its window slide right to , held at 5.5 s: every tap changes, 0.898 at , 0.294 at and −0.169 at , and the output samples land a quarter of the way to the next input sample, on the same wave, with an error of 0.00012. At the end , halfway: the taps are symmetric about 7.5 (type II of 17.3), and the error is 0.00018. From 12 s the dotted line is a handle, named “Delay n_0”, from 7 to 8 in steps of 0.01, with value text like “7.50 samples”. The arrow keys move it by 0.01, Page Up and Page Down by 0.1, and Home and End jump to 7 and 8. At 7, 7.25 and 7.5 the clip’s captions come back; elsewhere the caption reads like “n_0 = 7.30: largest error 0.00015.” The delay is kept in the link, as fraction.n0.
Notice the taps at : every one is 0 but , because a sinc is 0 at every whole number but its centre. Move by a quarter of a sample, and every tap changes.
When the clip has finished, drag the dotted line yourself, from 7 to 8, or use the arrow keys.
Why does it work? The taps are the reconstruction pulses of 10.3, read at times shifted by , so . The window keeps the sum short.
It fails near . At the taps are type II, which forces the gain to 0 at (17.3), and at the gain is already 0.70. At it is 0.863 at . Our test wave at is far from that edge.
An all-pass filter from All-pass systems (17.1) also gives a fractional delay. Its gain is 1 at every frequency, but its phase is bent, so the delay is not the same at every frequency.
Worked example
Here are the numbers behind the four filters, so you can check them by hand or in a few lines of code.
- First difference as a differentiator. Its gain is 0.12558 at , against , and 2 at , against . White noise of RMS comes out of a filter with RMS , its noise gain from Simple smoothing filters (18.2). For the first difference that is . For the 61-tap design with it is 0.149. Times the measured noise RMS of 0.0530, that predicts 0.0750 and 0.0079 of noise in the outputs, close to the errors 0.0752 and 0.0075. Almost all of each error is noise.
- Band-limited taps. With and 61 taps, the formula gives at and at . The window makes them and , and . The taps are exactly antisymmetric.
- Half-band. Of the 23 taps, 13 are not zero, and the centre is 0.5. The nearest taps are before the window and 0.315 after it. The pass band deviates from 1 by at most 0.0035 up to . The stop band from is at most 0.0035, which is −49.0 dB. The two are mirror images.
- Hilbert, 31 taps. Before the window the taps next to the centre are and . After it they are 0.633 and 0.200. The gain is within 0.0143 of 1 from to , but only 0.382 at and 0.812 at . Multiply by to take out the delay: the real part of what is left is 0 to within , so the phase is for every between 0 and .
- Fractional delay. At the taps from to 9 are 0.082, −0.169, 0.898, 0.294 and −0.114. At they are 0.099, −0.194, 0.631, 0.631 and −0.194. The largest errors on are 0, 0.00012 and 0.00018 for , 7.25 and 7.5. The group delay (12.4) at , , and equals to 3 decimals, both for 7.25 and for 7.5.
Where you’ll meet this
Band-limited differentiators find velocity from position and acceleration from velocity in sensors, robotics and vehicle logs, where the signal is slow and the noise is not. Half-band stages do most of the work in decimators and interpolators (22.1, 22.2). Hilbert transformers make the analytic signal (27.2), which radio receivers and envelope detectors use.
Fractional delays align microphone arrays and steer beamformers. They change sample rates by any ratio in Resampling by any factor (22.3), and they tune the strings and tubes of physical models in Audio effects (29.2). Modems use them to read a received signal at the right instant, in Digital modulation and OFDM (27.5).
One more special FIR filter is the minimum-phase one. Flip the zeros of a linear-phase design that lie outside the unit circle, as on Minimum phase (17.2), and the gain stays while the delay shrinks.
Reference card
| Filter | Ideal response | Taps | Type |
|---|---|---|---|
| Differentiator, band-limited | for | III (IV for full band) | |
| Half-band low-pass | 1 for | , 0 at even | I; |
| Hilbert transformer | at odd , 0 at even | III; 0 at 0 and π | |
| Fractional delay | , windowed | II at half-samples; fails near π | |
| Noise through a filter | RMS × | first difference: √2 | 18.2 |