Pergunta

Estou tentando escrever um algoritmo DCT simples em Java. Eu quero que meu método finddct tenha como parâmetro uma matriz inteira como esta:

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;
    }
}

Agora, como eu declararia esse método e seria capaz de aprovar 'int [] [] f' como um parâmetro em vez de usar f [] [] declarado como uma variável privada e inicializada no construtor da classe atual?

Foi útil?

Solução

Que tal extrair o initializeCoefficients e mudar o construtor de

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

para

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

Você poderia então usar a classe como

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

Além disso, eu não usaria N do jeito que você faz. Eu olhava para as dimensões de f por si só ao aplicar o DCT.

Isto é, eu mudaria

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

para algo como

    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++) {
              // ...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top