5
여기에는 "applyDCT"및 "applyIDCT"메소드가 포함 된 DCT 알고리즘 클래스가 있습니다. 기술적으로 0에서 255 사이의 무작위 정수의 2x2 테이블에 대해 순방향 DCT (이산 코사인 변환)를 수행 한 다음이 숫자에 대해 역 DCT를 수행하면 처음부터 원래의 정수로 되돌아 와야합니다. 내 경우에는 그렇지 않습니다. 여기서 내가 뭘 잘못하고 있니? 표시되지 않습니다 "뒤로 F에"Java에서 DCT 및 IDCT 알고리즘 관련 문제
149.0 => f[0][0]
237.0 => f[0][1]
122.0 => f[1][0]
147.0 => f[1][1]
From f to F
-----------
81.87499999999999 => F[0][0]
-14.124999999999993 => F[0][1]
14.62500000000001 => F[1][0]
-7.875 => F[1][1]
Back to f
---------
9.3125 => f[0][0]
14.812499999999998 => f[0][1]
7.624999999999999 => f[1][0]
9.187499999999998 => f[1][1]
위와 같이 : 여기
public class Main {
private static final int N = 2;
private static double[][] f = new double[N][N];
private static Random generator = new Random();
public static void main(String[] args) {
// Generate random integers between 0 and 255
int value;
for (int x=0;x<N;x++) {
for (int y=0;y<N;y++) {
value = generator.nextInt(255);
f[x][y] = value;
System.out.println(f[x][y]+" => f["+x+"]["+y+"]");
}
}
DCT dctApplied = new DCT();
double[][] F = dctApplied.applyDCT(f);
System.out.println("From f to F");
System.out.println("-----------");
for (int x=0;x<N;x++) {
for (int y=0;y<N;y++) {
try {
System.out.println(F[x][y]+" => F["+x+"]["+y+"]");
} catch (Exception e) {
System.out.println(e);
}
}
}
double f[][] = dctApplied.applyIDCT(F);
System.out.println("Back to f");
System.out.println("---------");
for (int y=0;y<N;y++) {
for (int z=0;z<N;z++) {
System.out.println(f[y][z]+" => f["+y+"]["+z+"]");
}
}
}
}
결과의 예입니다 :
public class DCT {
private static final int N = 2;
private double[] c = new double[N];
public DCT() {
this.initializeCoefficients();
}
private void initializeCoefficients() {
for (int i=1;i<N;i++) {
c[i]=1;
}
c[0]=1/Math.sqrt(2.0);
}
public double[][] applyDCT(double[][] f) {
double[][] F = new double[N][N];
for (int u=0;u<N;u++) {
for (int v=0;v<N;v++) {
double sum = 0.0;
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
sum+=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];
}
}
sum*=((c[u]*c[v])/4.0);
F[u][v]=sum;
}
}
return F;
}
public double[][] applyIDCT(double[][] F) {
double[][] f = new double[N][N];
for (int u=0;u<N;u++) {
for (int v=0;v<N;v++) {
double sum = 0.0;
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
sum+=((c[u]*c[v]))*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];
}
}
sum/=4.0;
//sum*=((c[u]*c[v])/4.0);
f[u][v]=sum;
}
}
return f;
}
}
그리고 여기에 그것으로가는 주요 클래스 처음에는 f에 포함 된 같은 값이 ...
입력 어떤 경우와 상기 예상되는 결과와 실제 결과 어떤 무엇? 어느 것이 틀렸는 지 알아보기 위해 간단한 입력 (예 : [1 0; 0 0])에서 각 루틴을 실행 해 보았습니까? –
원래의 정수를 다시 얻지 못한다고 말할 때 어떤 결과가 나타 납니까? 일부 부동 소수점 반올림 오류를 도입 할 수 있습니다. – rsp
DCT 자체가 손실됩니다. 무손실 (가역) 연산을 얻으려면 수정 된 DCT (무손실 DCT)가 필요합니다. – osgx