0

我想在java中編寫一個簡單的DCT算法。我希望我的findDCT方法有作爲參數的整數數組是這樣的:Java參數傳遞int [] []

public class DCT { 
    private Random generator = new Random(); 
    private static final int N = 8; 
    private int[][] f = new int[N][N]; 
    private double[] c = new double[N]; 

    public DCT() { 
     this.initializeCoefficients(); 
    } 

    private void initializeCoefficients() { 
     int value; 

     // temporary - generation of random numbers between 0 and 255 
     for (int x=0;x<8;x++) { 
      for (int y=0;y<8;y++) { 
       value = generator.nextInt(255); 
       f[x][y] = value; 
       System.out.println("Storing: "+value+" in: f["+x+"]["+y+"]"); 
      } 
     } 

     for (int i=1;i<N;i++) { 
      c[i]=1/Math.sqrt(2.0); 
      System.out.println("Storing: "+c[i]+" in: c["+i+"]"); 
     } 
     c[0]=1; 
    } 

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

現在,我將如何聲明這個方法,並能夠通過「INT [] [] F」作爲一個參數,而不是使用f [] []聲明爲私有變量並在當前類的構造函數中初始化?

回答

0

如何從

public DCT() { 
    this.initializeCoefficients(); 
} 

提取initializeCoefficients和改變構造函數

public DCT(int[][] f) { 
    this.f = f; 
} 

然後,您可以使用類像

double[][] dctApplied = new DCT(yourTwoDimF).applyDCT(); 

另外,我想不要用你的方式使用N。在應用DCT時,我會考慮f本身的尺寸。

也就是說,我會改變

double[][] F = new double[N][N]; 
    for (int u=0;u<N;u++) { 
      for (int v=0;v<N;v++) { 
       // ... 

喜歡的東西

double[][] F = new double[f.length][]; 
    for (int u = 0; u < f.length; u++) { 
      F[u] = new double[f[u].length]; 
      for (int v=0;v<N;v++) { 
       // ... 
+0

感謝您的快速反應!我同意你的觀點,我應該在f的維度上更加靈活。然而,我不明白你爲什麼這麼做: – 2010-11-19 00:31:18

+0

F [u] = new double [f [u] .length]; – 2010-11-19 00:31:33