BBest24
ENPL

Before choosing a digital filter: sampling, interference and latency

Square waves, triangle waves and a short pulse reveal how sampling and filtering change information. Worked calculations, limitations and practical selection criteria.

Undulating mesh surface with discrete points. Conceptual illustration of signal representation.

Start with the information you need

Does the system need to measure a signal’s frequency, preserve its shape or detect a short event? These are three different tasks. Smoothing can make a plot look calmer while removing the pulse that motivated the entire measurement. Choosing a filter therefore starts with identifying the information that must survive.

For room temperature, a slow trend may be enough. For a sensor pulse, amplitude and the instant of crossing a threshold may matter. A control loop also needs a timely response. Express these needs as requirements: useful bandwidth, meaning the range of frequencies to preserve, acceptable amplitude error, minimum event duration and a latency limit. Only then can a filter be judged useful.

Look upstream of the analogue-to-digital converter (ADC) too. A saturated amplifier, a slow sensor or an unsuitable input bandwidth changes the signal before the first sample exists. A later algorithm cannot generally restore information that the measurement chain never recorded.

Two waves, identical samples

With ideal sampling at 1000 Hz, compare sine waves at 100 Hz and 1100 Hz with the same amplitude and phase. At times t = k / 1000, where k is the sample number starting at zero, the second wave has completed an additional k full cycles. Both functions therefore have identical values at every sampling instant.

100 Hz and 1100 Hz sine waves with identical samples at 1000 Hz
Two different signals, identical samples at a sampling rate of 1000 Hz.

This is a calculated example: lines show continuous signals, while points show their samples. Amplitude is normalised; noise, jitter (variations in sampling instants) and ADC nonlinearity are omitted. The samples alone cannot tell us which wave reached the input. This is aliasing: different components become indistinguishable after sampling.

In the case considered here, the band starts at zero. Reconstructing a signal with no components above a specified frequency requires a sampling rate exceeding twice that frequency. Half the sampling rate is called the Nyquist boundary. The boundary is not a comfortable design target: a sine wave sampled exactly twice per period can produce only zeros at a particular phase. A real input filter also needs margin. Analog Devices explains the relationship between aliasing and filtering before the ADC.

A 100 Hz square wave does not stop at 100 Hz

A 100 Hz waveform has a period of 10 ms. That does not mean a single sine wave describes its shape. A symmetrical, zero-mean square wave with a 50% duty cycle contains odd harmonics: 100, 300, 500 Hz and beyond. Their sum creates flat sections and sharp transitions. Removing higher components produces a more rounded waveform.

A symmetrical triangle wave also contains odd harmonics, but their amplitudes decrease faster. Relative to the fundamental, the amplitude of harmonic n is 1/n for the square wave and 1/n² for the triangle wave. The triangle wave also has alternating signs in its sine coefficients. Phase describes how the waves are shifted relative to one another within their cycles. It matters to the resulting shape just as the individual component amplitudes do.

Scroll horizontally if needed.

Harmonic Frequency Square: amplitude relative to fundamental Triangle: amplitude relative to fundamental
1 100 Hz 100% 100%
3 300 Hz 33.3% 11.1%
5 500 Hz 20% 4%
7 700 Hz 14.3% 2.04%
9 900 Hz 11.1% 1.23%

These are amplitude ratios, not shares of energy. They apply to symmetrical waveforms; changing a square wave’s duty cycle changes its spectrum and can introduce even harmonics. Tektronix illustrates the relationship between harmonics and waveform shape.

Eight samples per period: enough for what?

The next plot builds each waveform from five components: harmonics 1, 3, 5, 7 and 9. Let n denote the harmonic number and t the time in seconds. The square-wave model sums the terms (4/π) · sin(n · 2π · 100 · t)/n for those five values of n. For the triangle model, the scale factor is 8/π², the denominator is n² and successive terms alternate in sign. This scaling corresponds to ideal waveforms ranging from −1 to 1; finite sums need not reach exactly those limits. One unit on the vertical axis refers to the ideal waveform peak, rather than the fundamental amplitude used as the reference in the table.

100 Hz square and triangle approximations sampled at 800 and 4000 Hz; points are not joined by a reconstruction line
100 Hz square and triangle waves approximated by harmonics 1, 3, 5, 7 and 9. Lines show the continuous model with bandwidth up to 900 Hz; points are samples without anti-aliasing filtering. Higher harmonics alias at 800 Hz; at 4000 Hz all model components lie below the Nyquist boundary.

In the plot, fs denotes the sampling rate. At fs = 800 Hz we have eight samples per fundamental period, but the Nyquist boundary is 400 Hz. Our model’s 500, 700 and 900 Hz components already exceed it. At 4000 Hz the boundary is 2000 Hz, so all five components fit below it. Under ideal assumptions this permits reconstruction of the limited model, but not of an ideal square wave containing infinitely many harmonics.

Consider the 700 Hz component. At sampling instants with an 800 Hz rate, sin(2π · 700 · k/800) = −sin(2π · 100 · k/800), where k is the sample number. It therefore appears in the data as a 100 Hz component with reversed sign. Similarly, 500 Hz folds onto 300 Hz with opposite sign, while 900 Hz folds onto 100 Hz with the same sign. For the square-wave model, the 100 Hz sine coefficient in the samples is consequently (4/π) · (1 − 1/7 + 1/9). Simply adding the positive amplitudes from the table would give the wrong answer.

The plot deliberately samples without first removing components above 400 Hz. The grey line for each shape is identical in both panels: it shows the signal before sampling, not a reconstructed output. What changes is the density of the points representing samples. Joining them with straight lines would not demonstrate correct reconstruction. Shifting the sampling instants relative to the waveform changes the sample values, but does not solve the bandwidth problem.

An ideal shape and a real input chain are different things

An ideal square wave has instantaneous edges and unlimited bandwidth. A real device has a finite rise time, and its sensor, amplifier and filter limit bandwidth. A finite harmonic sum is another case again: an explicitly defined mathematical model. It should not be mistaken for a device measurement or a model of a particular analogue filter.

Overshoot near the edges of the square approximation results from truncating the series: the Gibbs phenomenon. Adding harmonics narrows the affected region without eliminating the relative overshoot height at an ideal discontinuity. Rounding of the triangle shows the loss of sharp vertices. Both effects explain why “100 Hz” is an incomplete measurement requirement.

If only the fundamental matters, restricting bandwidth may be the right decision. If the task is to measure edge timing or a short pulse, the removed components may carry the information we need. Bandwidth is a trade-off determined by the task, rather than a universal multiple of the fundamental frequency.

Filtering before and after the ADC

An anti-aliasing filter attenuates components that would corrupt the useful band after sampling. A real filter needs a transition region between its passband and the required attenuation. For example, with a useful band up to 300 Hz and an 800 Hz sampling rate, only 100 Hz separates 300 Hz from the Nyquist boundary. That is a tight filter budget if substantial attenuation is required before that boundary.

The required attenuation also depends on interference amplitude. A large out-of-band signal may need much stronger suppression than small noise. A digital filter can subsequently select a band or reduce noise, but cannot generally determine whether an in-band component came from the intended signal or from aliasing.

The same principle applies to decimation, which reduces the sampling rate. Before moving from 4000 to 1000 samples/s, digital filtering must restrict bandwidth appropriately for the new 500 Hz boundary. Merely keeping every fourth sample folds higher frequencies into the lower band again. Oversampling ADCs perform some filtering internally, but analogue input requirements still follow from their documentation.

Smoothing can remove an event

A 21-sample moving average adds the current value and the previous 20 values, then divides by 21. This filter is causal: it uses only the current sample and the past. The centre of its window lies 10 samples behind the current instant. At 1000 Hz this gives a group delay of 10/1000 s = 10 ms. For slow changes that the filter passes with little distortion, this can be understood as an approximate time shift of the waveform. This does not mean 10 ms of silence before the first response: the output starts changing as soon as the first nonzero sample arrives. Event detection time and step-response settling time are separate quantities.

Now assume five consecutive samples of value 1 at 5, 6, 7, 8 and 9 ms, surrounded by zeros. The filter starts with zero state. No window contains more than five ones, so the maximum output is 5/21 ≈ 0.238. The pulse occupies five sample periods, corresponding to 5 ms; the precise analogue pulse duration cannot be inferred from those five samples alone.

A five-sample pulse, its 21-sample moving average and a detection threshold of 0.5 that the filtered output never reaches
Five samples of amplitude 1 passed through a causal 21-sample moving average reach a maximum of 5/21 ≈ 0.238. The output never reaches the 0.5 threshold. Sampling rate: 1000 Hz; initial state: zero. Steps show successive sample values, not analogue reconstruction.

A detector with a threshold of 0.5 responds to the input sequence, but not to the filtered output. This is more than a delayed response: the event is missed. The result extends across 25 nonzero samples and has a lower peak. In this model the sum of sample values is preserved, but an amplitude detector does not use that property.

A moving average is also not an ideal filter rejecting every frequency above a single boundary. It has response zeros: frequencies at which it completely suppresses an ideal sine wave in steady state. The first here occurs at 1000/21 ≈ 47.6 Hz. Between successive zeros, some signal still passes. Good time-domain smoothing does not automatically mean good band separation. The chapter on moving averages explains this trade-off.

Turning the examples into a filter decision

Compare candidates using the same inputs: a slow change, a step, the minimum required pulse and out-of-band interference. Record amplitude error and detector behaviour for each. If both a calm display and a fast alarm are needed, consider separate processing paths, each assessed against its own requirement.

Check startup, missing data and parameter changes. Filling gaps with zeros can create artificial edges; shortening the window during startup temporarily changes the algorithm’s properties. Fixed-point arithmetic adds accumulator range and rounding concerns. Changing the sampling rate while retaining coefficients shifts the response expressed in hertz.

The assessment should produce a statement such as: “this candidate preserves the required pulse and useful-band amplitude within the allowed latency.” Attach the configuration, coefficients and test data. A smooth plot then becomes a consequence of a deliberate decision rather than the sole argument for the filter.

Sources

All notes →