Problems with DCT and IDCT algorithm in java

前端 未结 1 1524
萌比男神i
萌比男神i 2021-02-06 11:40

Here I have my DCT algorithm class with \"applyDCT\" and \"applyIDCT\" methods. Technically after doing a forward DCT (discrete cosine transform) on a 2x2 table of random intege

相关标签:
1条回答
  • 2021-02-06 11:58

    I have resolved this problem, I am sorry if my question was unclear but here is what was not right: The IDCT method had to have the coefficient inside the i and j for loops :

    public double[][] applyIDCT(double[][] F) {
            double[][] f = new double[N][N];
            for (int i=0;i<N;i++) {
              for (int j=0;j<N;j++) {
                double sum = 0.0;
                for (int u=0;u<N;u++) {
                  for (int v=0;v<N;v++) {
                    sum+=(c[u]*c[v])/4.0*Math.cos(((2*i+1)/(2.0*N))*u*Math.PI)*Math.cos(((2*j+1)/(2.0*N))*v*Math.PI)*F[u][v];
                  }
                }
                f[i][j]=Math.round(sum);
              }
            }
            return f;
        }
    

    This only works for a 8x8 bloc of data, or else you would have to change this:

    (c[u]*c[v])/4.0)
    

    into something like this:

    (2*c[u]*c[v])/Math.sqrt(M*N)
    

    Where M and N are the dimentions of the table...

    Here are the results with a 2x2 block of data:

    Original values
    ---------------
    54.0 => f[0][0]
    35.0 => f[0][1]
    128.0 => f[1][0]
    185.0 => f[1][1]
    
    From f to F
    -----------
    200.99999999999994 => F[0][0]
    -18.99999999999997 => F[0][1]
    -111.99999999999997 => F[1][0]
    37.99999999999999 => F[1][1]
    
    Back to f
    ---------
    54.0 => f[0][0]
    35.0 => f[0][1]
    128.0 => f[1][0]
    185.0 => f[1][1]
    
    0 讨论(0)
提交回复
热议问题