Jorge Quitério Search
IT & Engineering

Sampling, aliasing and monitoring

Sampling a signal too slowly turns a fast cycle into an apparent slow one. The Nyquist-Shannon theorem states how fast sampling must be, and it applies to monitoring dashboards as much as to audio. A figure demonstrates frequency folding.

In films, wagon wheels sometimes appear to turn backwards. The effect is caused by the camera, not the wheel. Film takes 24 pictures a second, and a spoke that moves almost one spoke-spacing between frames looks as if it moved a little in the opposite direction. The result is an apparent slow backward rotation.

The same effect occurs in every sampled system. A monitoring stack that scrapes a metric every 15 seconds can display a smooth one-minute wave for a job that actually spikes every 20 seconds. Nothing in the dashboard indicates that the wave is an artefact. This note explains the arithmetic of the effect, the theorem that states when it cannot occur, and its implications for monitoring based on sampled metrics.

The theorem has several originators. Whittaker (1915) studied the interpolation function now called the cardinal series. Nyquist (1928) showed in a telegraphy paper that a channel of bandwidth BB can carry at most 2B2B independent pulses per second. Kotelnikov (1933) and Shannon (1949) stated the sampling theorem in its modern form: a signal containing no frequencies above BB is completely determined by samples taken at a rate above 2B2B. Unser (2000) reviews fifty years of work that followed, including what happens when the assumptions are only approximately true.

The theorem

A signal whose spectrum contains nothing above frequency BB can be reconstructed exactly from samples taken at rate fs>2Bf_s > 2B. Half the sampling rate, fs/2f_s/2, is the Nyquist frequency: the highest frequency a sampled system can represent.

Aliasing

Sample a cosine of frequency ff at times k/fsk/f_s. Because the cosine is periodic, cos⁡(2πfk/fs)\cos(2\pi f k/f_s) and cos⁡(2π(f−mfs)k/fs)\cos(2\pi (f - m f_s) k / f_s) are identical at every sample, for any whole number mm. The samples cannot tell ff from f−mfsf - m f_s, and the reconstruction picks the slowest candidate:

fapparent=∣ f−fs⋅round⁡(f/fs) ∣. f_{\text{apparent}} = \big|\, f - f_s \cdot \operatorname{round}(f/f_s) \,\big| .

Frequencies above fs/2f_s/2 fold back into the range [0,fs/2][0, f_s/2]. A signal at 0.97fs0.97 f_s shows up at 0.03fs0.03 f_s, as in the wagon-wheel effect. The same folding occurs in space: a phone photographing a computer screen often shows coloured wavy bands, moiré patterns, because the screen's pixel grid is finer than the camera can resolve.

Implications for monitoring

A batch job runs every 20 seconds and pushes CPU up for a moment each time: 3 cycles per minute. Prometheus scrapes every 15 seconds: 4 samples per minute, so the Nyquist limit is 2 cycles per minute. The job is above it. The apparent frequency is ∣3−4∣=1|3 - 4| = 1 cycle per minute, so the graph shows a slow, smooth one-minute oscillation. An engineer looking for a process with a one-minute period will not find one.

Two features of real monitoring aggravate the problem:

  • Averaging over the interval (a counter's rate over the scrape window) is a crude low-pass filter. It weakens aliasing but also hides short spikes completely: a 2-second burst in a 60-second window shrinks to a thirtieth of its height. A household electricity meter that reports consumption every 30 minutes shows the same effect: a kettle drawing 3 kW for two minutes appears in the half-hourly data as a rise of only 0.2 kW.
  • Downsampling for long time ranges samples the samples. A dashboard that is accurate over one hour can alias badly over thirty days.

The practical rules follow from the theorem: sample at least twice as fast as the fastest behaviour of interest, preferably much faster; record maxima and minima per interval as well as averages when spikes matter; and treat with caution any slow, regular wave whose period corresponds to no known process.

In the figure below, both the signal and the sampling rate can be set.

The stems are the samples; the orange curve is the slowest wave consistent with them, which any reconstruction will display.

Suggested settings:

  1. Monitoring example. With the preset (a 20-second job, 15-second scrapes) the samples trace a clean one-minute wave. The apparent period readout says 60 s. The actual job has no one-minute component.
  2. Sufficient sampling rate. Press "Sample just fast enough": the rate moves just above twice the signal frequency and the orange curve coincides with the true one.
  3. Wagon-wheel effect. Press "f ≈ fs": the signal is almost exactly as fast as the sampling, and the samples show an extremely slow wave. At exactly f=fsf = f_s every sample lands on the same phase and the signal looks frozen.
  4. Frequency folding. Slowly increase the signal frequency from 0 to 12 with 4 samples per minute. In the lower chart the dot climbs to the Nyquist line at 2, then descends to 0 at 4, climbs again, and so on.

Derivation of folding. For integer kk and mm, cos⁡(2π(f−mfs)k/fs)=cos⁡(2πfk/fs−2πmk)=cos⁡(2πfk/fs)\cos\big(2\pi (f - m f_s) k/f_s\big) = \cos(2\pi f k/f_s - 2\pi m k) = \cos(2\pi f k/f_s), because mkmk is an integer. So every frequency f−mfsf - m f_s produces the same samples, and by symmetry of the cosine so does ∣f−mfs∣|f - m f_s|. Choosing m=round⁡(f/fs)m = \operatorname{round}(f/f_s) gives the smallest such value, which lies in [0,fs/2][0, f_s/2]. The theorem's positive half, exact reconstruction below the Nyquist frequency, follows from the cardinal series x(t)=∑kx(k/fs) sinc⁡(fst−k)x(t) = \sum_k x(k/f_s)\, \operatorname{sinc}(f_s t - k).

Aliasing is inherent in observation at discrete intervals, whatever the tool. Every monitoring decision, from the scrape interval to the retention policy's downsampling, is a choice of sampling rate, and therefore determines which behaviours can be observed. The same logic applies to how often to check for configuration drift, as in Terraform as a change-management control, and to how often an auditor samples a control.

References

  1. E. T. Whittaker, "On the functions which are represented by the expansions of the interpolation-theory", Proceedings of the Royal Society of Edinburgh, 35, 181-194, 1915.
  2. H. Nyquist, "Certain topics in telegraph transmission theory", Transactions of the AIEE, 47(2), 617-644, 1928.
  3. V. A. Kotelnikov, "On the transmission capacity of 'ether' and wire in electrocommunications" (in Russian), Material for the First All-Union Conference on Questions of Communications, 1933.
  4. C. E. Shannon, "Communication in the presence of noise", Proceedings of the IRE, 37(1), 10-21, 1949.
  5. M. Unser, "Sampling, 50 years after Shannon", Proceedings of the IEEE, 88(4), 569-587, 2000.