What's new

Help put equation into code

GbaGuy

New member
What I'm struggling with is understanding JPEG.

Now that noone is reading anymore :p, what I don't understand is the DCT.
Wikipedia shows it as (in Discrete cosine transform - Wikipedia, the free encyclopedia) (DCT-II):
Assuming N = 8, in doing one row (or column, but lets just assume row):

Xk = sum(0..7) { Xn * cos((PI/8)(n+.5)(k)) }

Now if X is a 8 element array of ints, and k and n are indeces, then:
Code:
float sum = 0;
for(int n=0;n<8;n++)
{
sum += X[n] * cos((PI/8)*(n+.5)*k);
}

I have no idea what 'k' is supposed to be. I think it's either 1..8, or it could be
1..7. It could be one of those ranges incrementing through the sum, or incrementing for each new Xk.

Can anyone help me out?

Many thanks in advance!
 

zAlbee

Keeper of The Iron Tail
You have to read this sentence again ;):

Wikipedia said:
Formal definition

Formally, the discrete cosine transform is a linear, invertible function F : RN -> RN (where R denotes the set of real numbers), or equivalently an N × N square matrix. There are several variants of the DCT with slightly modified definitions. The N real numbers x0, ..., xN-1 are transformed into the N real numbers X0, ..., XN-1 according to one of the formulas:

So your original 8-element array x determines the values of your new 8-element array X. Make sure the variables are different, you don't want to overwrite the original values of x, or you won't compute it correctly.

Therefore k will also be 0..7.

And you want:

Code:
for(int k=0;k<8;k++)
{
    xnew[k]=0;
    for(int n=0;n<8;n++)
    {
        xnew[k] += X[n] * cos((PI/8)*(n+.5)*k);
    }
}
 
Last edited:
OP
GbaGuy

GbaGuy

New member
Thank you! I had forgotten about the other question (new array, or replacement). So, essentially, you answered two (rather many) questions at once!

Later tomorrow I'm going to try implementing the 2-d formula described and see what I get inputting the array in the Wikipedia article about JPEG. Although... it looks like I can do it again on the results of the above (the columns) into the original array (save space, and I don't need the original values anymore I don't think), and that'll get it done.

My mind is already spinning on how slow this is gonna be. I suck :( .
 

Top