Topic: Discrete Cosine Transform Equation and code?

Hi, I am sorry for my bad English.
I am a student who interested in your DCT frequency domain transform.
I think your DCT transform is faster than  original DCT transform.
I've tried using original DCT transform from Wiki that I created using Delphi,
but it is too slow. May I know what equation DCT that you used in IA?
And may I get that DCT code?

Thanks
Bargie

Re: Discrete Cosine Transform Equation and code?

Hi
Actually I think that my implementation is awfully slow but you can have it:

procedure PerformDCT(var InVal, OutVal: TFloatArray; N: Integer);
var
  x, u : Integer;
  Sum, Alpha, B, TwoCosW, yn_1, yn_2, W, y, Pi2N : Float;
begin

  // y(n) = Sin(nW+B) = 2·cos(W)·y(n-1) - y(n-2), n>=0

  Pi2N:=Pi/(2*N);
  Alpha:=Sqrt(2/N);
  for u:=0 to N-1 do
    begin
      Sum:=0;
      B:=U*Pi2N;
      W:=2*B;
      B:=B+Pi/2;
      yn_1:=Sin(-W+B); yn_2:=Sin(-2*W+B);
      TwoCosW:=2*Cos(W);
      for x:=0 to N-1 do
      begin
        y:=TwoCosW*yn_1-yn_2;
        yn_2:=yn_1;
        yn_1:=y;
        Sum:=Sum+InVal[X]*y;
      end;

      if u=0 then OutVal[u]:=Sqrt(1/N)*Sum
      else OutVal[u]:=Alpha*Sum;
    end;
end;

procedure PerformIDCT(var InVal, OutVal: TFloatArray; N: Integer);
var
  x, u : Integer;
  Sum, Alpha, Alpha0, TwoCosW, yn_1, yn_2, W, y, Pi2N : Float;
begin

  // y(n) = Sin(nW+B) = 2·cos(W)·y(n-1) - y(n-2), n>=0

  Pi2N:=Pi/(2*N);
  Alpha0:=Sqrt(1/N);
  Alpha:=Sqrt(2/N);
  for x:=0 to N-1 do
    begin
      Sum:=0;
      W:=(2*x+1)*Pi2N;
      yn_1:=Sin(-W-(Pi/2)); yn_2:=Sin(-2*W-(Pi/2));
      TwoCosW:=2*Cos(W);
      for u:=0 to N-1 do
      begin
        y:=TwoCosW*yn_1-yn_2;
        yn_2:=yn_1;
        yn_1:=y;

        if u=0 then y:=InVal[u]*y*Alpha0
        else y:=InVal[u]*y*Alpha;
        Sum:=Sum+y;
      end;
      OutVal[x]:=Sum;
    end;
end;

TFloatArray is just an array of Single or Double

Michael Vinther

> software developer <

Re: Discrete Cosine Transform Equation and code?

Hi, thanks for the code, it works with the same result as the original equation of DCT and with faster process.

I want to ask one more question about your equation,  can you please explain something or give me some reference link about why you use sine in the equation, because I failed when I tried to convert original equation (based on DCT II in this link: http://en.wikipedia.org/wiki/Discrete_cosine_transform) to your equation.

Thanks,
Bargie

Last edited by bargie (2010-01-29 20:27:52)

Re: Discrete Cosine Transform Equation and code?

You mean Sin instead of Cos? I don't even remember, but they are the same, just a quarter of period delayed.

Michael Vinther

> software developer <

Re: Discrete Cosine Transform Equation and code?

Yes, I mean Sin instead of Cos.. Okay, I'll try to find it myself.
And thanks again for the code.