The discrete cosine transform is a frequency transform similar to the Fourier transform, but using only real numbers. For a vector x, of size n, an n×n matrix is made, as follows:
/ sqrt(1/2) sqrt(1/2) ... sqrt(1/2) \ | cos(1/2n) cos(3/2n) ... cos(-1/2n) | | cos(2/2n) cos(6/2n) ... cos(-2/2n) | | cos(3/2n) cos(9/2n) ... cos(-3/2n) | | ............................................ | \ cos((n-1)/2n cos(2(n-2)/2n ... cos((1-n)/2n) /
To invert the transform, multiply by the transpose of this matrix.
As used in JPEG, n is 8 and both the input and output are two-dimensional arrays. This is done by transforming each row of the array, transposing it, transforming each row (originally column), and transposing it back. The result is an array in which the top left corner is the DC component and lower and rightmore entries represent higher vertical and horizontal frequencies. For the chrominance components, n is 16 but the frequency components beyond the first 8 are discarded.
As in the fast Fourier transform, it is possible to speed up the algoritthm by combining like coefficients.