Search

Search papers and lessons

Anti-Aliasing

Level 2 · 14 min read

Anti-aliasing removes or averages away the fine detail a sampler cannot record, before sampling, so the samples show a faithful lower-detail picture instead of false patterns.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Electromagnetism
  • Discrete math
  • Electrical engineering
  • Classical mechanics
  • ReadReady to readNot yet

TL;DR

A sampler records a signal only at discrete points, so any detail finer than its Nyquist limit folds back as a false, coarser pattern. Anti-aliasing stops this by removing that fine detail, or by averaging it over the area each sample covers, before the samples are taken. The result is a faithful picture at the sampler's resolution instead of jagged edges, false tones or moiré.

Builds on: Nyquist–Shannon Sampling Theorem, Low-Pass Filter, Convolution · Leads to: Digital Image, Moiré Pattern

Where it's used

Audio converters. Every analogue-to-digital converter in a recording chain has an analogue low-pass filter in front of it, and it rounds each sample to a finite number of levels. The compact disc format, introduced in 1982, samples at 44,100 samples per second, so frequencies above 22,050 Hz must be removed before sampling. Otherwise a stray ultrasonic whine from a motor or a microphone would reappear as an audible tone.

Computer graphics. A renderer computes the colour of each pixel by sampling the scene at a few points inside the pixel. Without anti-aliasing, the edge of a sloped line or a tilted roof steps from one pixel to the next as a staircase. Games use multisampling or temporal anti-aliasing to average several samples per pixel, and film renderers use many more.

Digital cameras. A sensor records one value per pixel, so a fine pattern such as a striped shirt or a window screen can alias into large, wavy bands. Many cameras once placed a thin optical low-pass filter in front of the sensor for this reason, at a small cost in sharpness. Photographing a screen is a common way to see the effect, which is called moiré.

Oversampling in measurement. Many modern audio converters sample far above the audio band, for example at 64 times the final rate, then filter digitally and throw away most of the samples. The analogue filter then only has to act well above the audio band, which is much easier to build.

How it works

Picture a single row of pixels looking at a scene with one hard edge. The scene is dark for x<1.5x < 1.5 and bright for x≥1.5x \ge 1.5. Each pixel covers one unit of width, so pixel 1 covers the interval from x=1x = 1 to x=2x = 2, and exactly half of it is bright.

  1. Take one sample per pixel at its centre. The centres are 0.5,1.5,2.5,3.50.5, 1.5, 2.5, 3.5. Pixel 0 is dark, and the sample at x=1.5x = 1.5 falls on the bright side, since bright starts there. The output is [0,1,1,1][0, 1, 1, 1]. Check: pixel 1 should be half bright, but it is fully bright, so the edge has moved by half a pixel.
  2. Take four sub-samples inside each pixel. For pixel 1 the positions are 1.125,1.375,1.625,1.8751.125, 1.375, 1.625, 1.875. Two are on the dark side and two on the bright side. Check: two out of four matches the half of the pixel that is bright.
  3. Average the sub-samples. The value of pixel 1 is (0+0+1+1)/4=0.5(0 + 0 + 1 + 1)/4 = 0.5. Pixel 0 has all four sub-samples dark, giving 0, and pixels 2 and 3 are all bright, giving 1. Check: the new output is [0,0.5,1,1][0, 0.5, 1, 1], which has the intermediate value an area-based picture should have.
  4. Read what the averaging did. The average is a Convolution of the scene with a box as wide as one pixel. It removes the sharp step that the sampler could not represent, leaving a soft edge that the grid can record.
  5. Name the idea. Averaging over each pixel's area before sampling is prefiltering. Taking more sub-samples and averaging them is supersampling. Both act before the samples are taken, because the removed detail cannot be recovered afterwards.
flowchart LR
  S[Continuous scene] --> F[Low-pass or area average]
  F --> P[Sample at the grid]
  P --> Q[Quantise to levels]
  S -. "no filter: fine detail folds back" .-> P

The dotted line is the route anti-aliasing exists to block. Detail that is too fine goes straight into the sampler and comes out as a false pattern.

The math

The walkthrough needs three things: the value a pixel should have, the averaging that approximates it, and the frequency limit that decides how much filtering is needed.

Step 1 in symbols: the ideal pixel value

Let f(x)f(x) be the scene brightness at position xx, and let each pixel have width Δ\Delta, so pixel ii covers [iΔ,(i+1)Δ][i\Delta, (i+1)\Delta]. The ideal value of pixel ii is the average brightness over its area, a definite integral divided by the width:

pi=1Δ∫iΔ(i+1)Δf(x) dx.p_i = \frac{1}{\Delta} \int_{i\Delta}^{(i+1)\Delta} f(x)\,dx.

A single sample at the centre ci=(i+12)Δc_i = (i + \tfrac{1}{2})\Delta gives qi=f(ci)q_i = f(c_i) instead. Supersampling with mm sub-samples per pixel replaces the integral with a sum:

p^i=1m∑k=0m−1f ⁣(iΔ+(k+12)Δm).\hat p_i = \frac{1}{m} \sum_{k=0}^{m-1} f\!\left(i\Delta + \frac{(k + \tfrac{1}{2})\Delta}{m}\right).

In the walkthrough Δ=1\Delta = 1, m=4m = 4, and ff is 0 to the left of 1.51.5 and 1 to the right. Pixel 1 has p1=0.5p_1 = 0.5, and the sum gives p^1=0.5\hat p_1 = 0.5 as well. The centre sample gives q1=1q_1 = 1, which is wrong by half.

Step 2 in symbols: averaging is a convolution

Let h(s)=1/Δh(s) = 1/\Delta for ∣s∣<Δ/2|s| < \Delta/2 and h(s)=0h(s) = 0 otherwise. This is the box filter. Convolving the scene with it and then sampling at the centre gives the area average, since

(f∗h)(ci)=∫f(ci−s) h(s) ds=1Δ∫ci−Δ/2ci+Δ/2f(u) du=pi.(f * h)(c_i) = \int f(c_i - s)\,h(s)\,ds = \frac{1}{\Delta}\int_{c_i - \Delta/2}^{c_i + \Delta/2} f(u)\,du = p_i.

So prefiltering with a box and sampling at the centre is the same as taking the ideal area average. This is the Convolution that the walkthrough calls averaging.

Step 3 in symbols: how much filtering is needed

Sampling at spacing Δ\Delta has sampling frequency 1/Δ1/\Delta and Nyquist frequency fN=1/(2Δ)f_N = 1/(2\Delta). The Nyquist–Shannon Sampling Theorem says the prefiltered signal must have no content at or above fNf_N. The ideal prefilter is a Low-Pass Filter with response H(ν)=1H(\nu) = 1 for ∣ν∣<fN|\nu| < f_N and 00 above. The box filter has frequency response

Hbox(ν)=sin⁡(πΔν)πΔν,H_{\text{box}}(\nu) = \frac{\sin(\pi \Delta \nu)}{\pi \Delta \nu},

which is the Fourier transform of the box. Check two values. At the sampling frequency ν=1/Δ\nu = 1/\Delta the response is sin⁡(π)/π=0\sin(\pi)/\pi = 0, so the box removes exactly the frequency that would alias to zero. At the Nyquist frequency ν=1/(2Δ)\nu = 1/(2\Delta) it is sin⁡(π/2)/(π/2)=2/π≈0.64\sin(\pi/2)/(\pi/2) = 2/\pi \approx 0.64. So the box removes the sharpest detail but leaves about 64 percent of the amplitude at Nyquist, and some of that aliases anyway. This is why a box is a cheap approximation and not a perfect filter.

Step 4 in symbols: the sample

After prefiltering the signal is g=f∗hg = f * h, and the sampler records g(ci)g(c_i). The stored value is g(ci)=pig(c_i) = p_i in the box case. The sampler never sees content above fNf_N if the prefilter has removed it, and then no false frequency can appear.

Worked examples

Example
Tiny: a single pixel and a single edge

Take one pixel covering [0,1][0, 1] and a scene that is dark for x<0.75x < 0.75 and bright for x≥0.75x \ge 0.75. The ideal value is the bright fraction of the pixel, p=0.25p = 0.25.

  1. One centre sample at x=0.5x = 0.5 is dark, so the value is 00. Error: 0.250.25.
  2. Two sub-samples at 0.250.25 and 0.750.75 give dark and bright, so the value is (0+1)/2=0.5(0 + 1)/2 = 0.5. Error: 0.250.25.
  3. Four sub-samples at 0.125,0.375,0.625,0.8750.125, 0.375, 0.625, 0.875 give dark, dark, dark, bright, so the value is 1/4=0.251/4 = 0.25. Error: 00.

Interpretation: a single sample is all or nothing, and the error is as large as the whole bright part. Each doubling of the sub-samples gives a finer estimate, and with four sub-samples the edge fraction is exact because the edge falls on the boundary between two sub-pixels.

Example
Standard: a thin line that a single sample misses

Take one row of pixels, [0,1],[1,2],[2,3],[3,4][0,1], [1,2], [2,3], [3,4], and a bright line of width 0.50.5 from x=1.95x = 1.95 to x=2.45x = 2.45 on a dark background. The ideal pixel values are the fraction of each pixel covered by the line: pixel 1 gets 0.050.05 (from 1.951.95 to 22), pixel 2 gets 0.450.45 (from 22 to 2.452.45), and pixels 0 and 3 get 00.

  1. Single centre samples at 0.5,1.5,2.5,3.50.5, 1.5, 2.5, 3.5. The line covers none of these points, so the output is [0,0,0,0][0, 0, 0, 0]. Check: the line is visible in the scene but gone from the picture.
  2. Four sub-samples in pixel 2 at 2.125,2.375,2.625,2.8752.125, 2.375, 2.625, 2.875. The first two lie on the line and the last two do not, so p^2=2/4=0.5\hat p_2 = 2/4 = 0.5. Check: the true coverage is 0.450.45, and 0.50.5 is close.
  3. Four sub-samples in pixel 1 at 1.125,1.375,1.625,1.8751.125, 1.375, 1.625, 1.875. None lie on the line, so p^1=0\hat p_1 = 0. The true coverage is 0.050.05, which the sampler rounds away.
  4. The output is [0,0.5,0,0][0, 0.5, 0, 0] with the sub-samples, against [0,0,0,0][0, 0, 0, 0] with single samples.

Interpretation: a point sampler can miss a feature completely, and no later step can bring back a line that was never recorded. Supersampling finds the line and gives its brightness roughly right, at the cost of four times the work.

Example
Realistic: an ultrasonic tone in a 48 kHz recording

A studio records at 48,000 samples per second, so the Nyquist frequency is 48,000/2=24,00048{,}000 / 2 = 24{,}000 Hz. A motor produces a tone at 30,00030{,}000 Hz that is well above the audio band.

  1. The tone is above the Nyquist frequency, 30,000>24,00030{,}000 > 24{,}000, so it must be removed before sampling.
  2. Without a filter, the tone folds back to 48,000−30,000=18,00048{,}000 - 30{,}000 = 18{,}000 Hz.
  3. Check: 18,00018{,}000 Hz lies inside the audible band, which runs to about 20,000 Hz, so a listener would hear a tone that was never in the room.
  4. The analogue low-pass filter must therefore attenuate the 30,00030{,}000 Hz tone strongly before the converter samples, because the alias cannot be taken out afterwards.

Interpretation: the filter's job is to act above 24,00024{,}000 Hz, and if it is designed with a gentle slope, it must start rolling off well below that frequency, which is one reason converters oversample.

Try it. The same motor is recorded at 44,100 samples per second. What is the alias of its 30,00030{,}000 Hz tone, and is it audible?

Answer: the Nyquist frequency is 22,05022{,}050 Hz, and the alias is 44,100−30,000=14,10044{,}100 - 30{,}000 = 14{,}100 Hz. That is inside the audible band, so it would be heard.

Common mistakes and tradeoffs

Misconception 1: anti-aliasing is just blurring. It is tempting to think that filtering only makes the picture soft. In fact the filter removes detail that the grid cannot represent, so the blur is the part that the sampler could not have used. A sharper picture made by sharpening the samples does not recover the removed detail, because sharpening adds back frequencies that were never recorded.

Misconception 2: aliasing can be fixed after sampling. It is tempting to think a digital filter can clean up aliased samples. In fact a 30 kHz tone sampled at 48 kHz and a true 18 kHz tone give the same samples, so no digital step can tell them apart. The fix has to come before the sampler.

Misconception 3: higher resolution removes the need for anti-aliasing. It is tempting to think more pixels make the problem go away. In fact higher resolution only raises the Nyquist limit, and a finer pattern, such as a fabric weave, can still alias. The problem moves to a finer scale rather than disappearing.

When to use it. Always use it when the signal or scene may contain detail finer than the sampling grid, which in practice is almost always. The cost is a loss of sharpness and extra computation for supersampling.

SituationWhat goes wrongBetter choice
Real-time rendering with a tight frame budgetFull supersampling is too slowMultisampling on edges, or temporal anti-aliasing that reuses earlier frames
Offline film renderingQuality matters more than speedMany sub-samples per pixel, with a filter wider than one pixel
Audio convertersThe analogue filter must be steep, which is hard and noisyOversampling, then a digital filter and decimation
Camera sensorsFine repeating patterns form moiréAn optical low-pass filter, or a lens that blurs slightly
Distant textures in a gameMany texels fall inside one pixel and the result shimmersPrefiltered textures, such as mipmaps

History

Harry Nyquist described the limit on how fast a signal can be sent and recovered in a 1928 paper on telegraph transmission, and Claude Shannon put the sampling statement in its modern form in 1949. Their result says a signal with no frequencies above half the sampling rate can be rebuilt from its samples, and it implies that frequencies above that limit cannot be told apart from lower ones.

Digital audio made the rule a practical design constraint. The compact disc format of 1982 uses a sampling rate of 44,100 per second and needs a filter that cuts off just above 20 kHz, which is why converters are built with steep analogue filters or with oversampling.

In computer graphics the problem was recognised early. Franklin Crow described aliasing in computer-generated shaded images in the late 1970s, and the methods that followed, from supersampling to filtered textures, are all forms of the same prefiltering idea.

Recap

  • A sampler records a signal only at discrete points, so detail above the Nyquist limit folds back as a false, coarser pattern.
  • Anti-aliasing removes that detail before sampling, either with a Low-Pass Filter or by averaging over each sample's area, which is a Convolution with a box.
  • A box filter is a cheap approximation: it removes the sampling frequency exactly but only partly attenuates content at the Nyquist frequency.
  • Supersampling is the same idea in numerical form, and it can find features that a single sample misses.
  • Next, read Digital Image for how a sampled picture is stored, and Moiré Pattern for the visible result of aliasing in fine repeating patterns.

Check yourself

QuestionRecall: why must anti-aliasing happen before the signal is sampled, and not after?Show answer

Once a signal is sampled, a high frequency above the Nyquist limit produces the same sample values as a lower frequency, so the two cannot be separated afterwards. Removing or averaging away the high detail has to happen while the signal is still continuous, or at least before the samples are taken, because any detail that has aliased is already mixed into the recorded values.

QuestionComputation: a pixel covers [0,1][0, 1] and the scene is bright for x≥0.4x \ge 0.4, dark otherwise. What does a single centre sample give, what does four equally spaced sub-samples give, and what is the true area value?Show answer

The centre sample is at x=0.5x = 0.5, which is bright, so it gives 11. Four sub-samples at 0.125,0.375,0.625,0.8750.125, 0.375, 0.625, 0.875 give dark, dark, bright, bright, so the average is 2/4=0.52/4 = 0.5. The true area value is the bright fraction of the pixel, 1−0.4=0.61 - 0.4 = 0.6. So the centre sample is off by 0.40.4 and the four sub-samples are off by 0.10.1.

QuestionWhy: why does the box filter remove the sampling frequency completely but leave some of the Nyquist frequency?Show answer

The box filter's response is sin⁡(πΔν)/(πΔν)\sin(\pi \Delta \nu)/(\pi \Delta \nu). At the sampling frequency ν=1/Δ\nu = 1/\Delta the numerator is sin⁡(π)=0\sin(\pi) = 0, so that frequency is removed exactly. At the Nyquist frequency ν=1/(2Δ)\nu = 1/(2\Delta) the response is 2/π≈0.642/\pi \approx 0.64, so a substantial part of the content at that frequency survives and can alias. A better prefilter has a response closer to a sharp cut-off at the Nyquist frequency, which is what the ideal low-pass filter provides.

QuestionApplied: a designer records a sensor signal at 1,000 samples per second and finds a 600 Hz hum in the samples that is not present in the physical source. What is the likely cause, and what should be changed?Show answer

The Nyquist frequency is 500500 Hz, so a source above it folds back. A source at 1,6001{,}600 Hz aliases to 1,600−1,000=6001{,}600 - 1{,}000 = 600 Hz, and so does one at 2,6002{,}600 Hz. A source at 600 Hz would itself be above the limit and would alias to 400 Hz, so the 600 Hz hum must come from a higher-frequency component in the physical source. The fix is an analogue low-pass filter with its cut-off below 500 Hz, placed before the converter, because the alias cannot be removed afterwards. Raising the sampling rate moves the Nyquist limit up, but it only helps for sources that fall below the new limit, so the filter is still needed.

Sources