Thursday, May 31, 2007

Anti Aliasing Filter

Aliasing: "signals" that are essentially continuous in space or time must be sampled, and the set of samples is never unique to the original signal. The other signals that could (or did) produce the same samples are called aliases of the original signal.

Nyquist Criteria: The minimum sampling rate required to allow unambiguous reconstruction of a bandlimited continuous signal from its samples (without having any alias) is to sample it at a rate more that twice the bandwidth of the signal.

Generally, any real world signal (be it sound or image or video) is not band limited. It is the perceiver (the ear, the eye) that is band limited or responsive to certain frequencies. Thus when a real world signal is converted to digital to avoid aliasing the signals are first filtered to make them band limited and then by Nyquist criteria they are sampled.

In case of voice, the maximum frequency response is around 4KHz and so a anti-aliasing filter of 4 Khz is first applied.Similarly, for music the audible range is around 22Khz and so a anti-aliasing filter is used.( Anti-aliasing filter is a low pass filter.)

The concept of aliasing is more clear in case of sound or audio but when it comes to video or images it is not so clear or it is not mapped to the things that we use for sound. So this article aims at clarifying this.

The example I am going to take here is from a article I read in "Extreme Tech" named "Anti-aliasing and Anisotropy filtering explained". Consider the following image that needs to be sampled based on the pixel grid shown behind it.

A diagonal line on a set of pixels.
The image you’re trying to represent—a diagonal line.
A diagonal line on a set of pixels.
A rough way to do this would be to just make points in which the black area is covered mostly by making it a black pixel and areas covered by white mostly by white pixel. Doing this we get the following image.

Aliased pixels
Aliasing at work—your pixels display one color or the other.
Aliased pixels
From the above figure it is clear that the image has jagged effect and does not represent the diagonal line clearly. To make it more clear lets consider a more sampled grid.

Aliasing at higher resolution
As resolution goes up, the ugliness of aliasing become less apparent.
Aliasing at higher resolution
Here even though the diagonal line is better seen (since by going for higher number of samples we have increased the sampling rate and thus reduced aliasing effect) we still observe the jagged effect.
To reduce this effect the solution would be thinking in the blind way is to fill regions which are partially with white and black with shades of gray.
So if the pixel is 25% filled by the black edge, you make that pixel only 25% black—a light gray color. If it's 80% covered by the black line, you make that pixel 80% black—dark grey. The result is an antialiased edge.

Antialiased edge
Antialiased edge

What we have really done here is that we have filtered (or applied a anti-aliasing filter) to the input image before sampling it. That is if the diagonal edge is filtered by a low pass filter before sampling then we would observe gray shades along the edges. A much clear picture of the same is found below.

AA edge higher resolution
Antialiased edge on an 80x60 pixel image
AA edge higher resolution

In 3D Graphics, this aliasing effect is described in a different view and the methods described is given below.
Super-sampling:
"The simple brute-force approach is called supersampling. For every pixel on your screen, you determine the color for multiple pixels, and then combine them. Let's say your screen is running at a resolution of 800x600 pixels. If you want to perform 4x supersampling, you effectively draw the screen at 1600x1200 pixels (four times as large) and then scale that down to 800x600 by combining the colors of four neighboring pixels into one. Most modern 3D cards don't exactly draw a higher-resolution screen. Rather, they calculate, store, and combine "subpixel samples" for each pixel."
Sub-pixel samples
In other words, (since we do not have analog filters while capturing or since in graphics we cannot have a perfect analog signal), we over sample the input signal then apply a linear filter across it and then down sample the over sampled input.

Effects of Super Sampling In Real World Images:
No AA vs. 2xAA
Even 2xAA makes a huge difference, and this is the fastest, lowest-quality mode available.
No AA vs. 2xAA

No AA vs. 4xAA
Stepping up to 4xAA makes a big difference.
No AA vs. 4xAA

Full quality comparison
Even higher levels of AA, together with AA modes that supersample objects with transparent textures (like the trees), makes the game look a lot better.
Full quality comparison

From the above comparison pictures effect of anti-aliasing effect is better shown.

No comments: