gpt4 book ai didi

java - Java 中二维矩阵的 DCT 和 IDCT 实现

转载 作者:行者123 更新时间:2023-11-30 08:14:41 24 4
gpt4 key购买 nike

我已经有一个 dct 和 idct 的实现,但是尽管进行了适当的优化,但随着矩阵大小的增加,它们变得非常慢。有谁知道两者或任何为二维案例提供更快实现的 Java 库的更快实现。谢谢

 public final double[][] initCoefficients(double[][] c) 
{
final int N = c.length;
final double value = 1/Math.sqrt(2.0);

for (int i=1; i<N; i++)
{
for (int j=1; j<N; j++)
{
c[i][j]=1;
}
}

for (int i=0; i<N; i++)
{
c[i][0] = value;
c[0][i] = value;
}
c[0][0] = 0.5;
return c;
}

/* Computes the discrete cosine transform
*/
public final double[][] forwardDCT(double[][] input)
{
final int N = input.length;
final double mathPI = Math.PI;
final int halfN = N/2;
final double doubN = 2.0*N;

double[][] c = new double[N][N];
c = initCoefficients(c);

double[][] output = new double[N][N];

for (int u=0; u<N; u++)
{
double temp_u = u*mathPI;
for (int v=0; v<N; v++)
{
double temp_v = v*mathPI;
double sum = 0.0;
for (int x=0; x<N; x++)
{
int temp_x = 2*x+1;
for (int y=0; y<N; y++)
{
sum += input[x][y] * Math.cos((temp_x/doubN)*temp_u) * Math.cos(((2*y+1)/doubN)*temp_v);
}
}
sum *= c[u][v]/ halfN;
output[u][v] = sum;
}
}
return output;
}

/*
* Computes the inverse discrete cosine transform
*/
public final double[][] inverseDCT(double[][] input)
{
final int N = input.length;
final double mathPI = Math.PI;
final int halfN = N/2;
final double doubN = 2.0*N;

double[][] c = new double[N][N];
c = initCoefficients(c);

double[][] output = new double[N][N];


for (int x=0; x<N; x++)
{
int temp_x = 2*x+1;
for (int y=0; y<N; y++)
{
int temp_y = 2*y+1;
double sum = 0.0;
for (int u=0; u<N; u++)
{
double temp_u = u*mathPI;
for (int v=0; v<N; v++)
{
sum += c[u][v] * input[u][v] * Math.cos((temp_x/doubN)*temp_u) * Math.cos((temp_y/doubN)*v*mathPI);
}
}
sum /= halfN;
output[x][y] = sum;
}
}
return output;
}

最佳答案

现在它是一个 O(n4) 算法,四个嵌套循环都在执行 n 迭代。可分离性将其降低到 O(n3)(或 O(n2log n),如果您有足够的勇气尝试快速余弦变换)。它实际上比使用 2D 公式更简单,因为它就是这样:

  • 在所有行上运行 1D DCT
  • 对(先前结果的)所有列运行 1D DCT

或者(可选)使两个部分完全相同:

  • 对所有行运行 1D DCT,保存转置结果
  • 再做一次

转置就是第二次真正做列,并且在两次转置中互相撤销。

所以,余弦。你注意到

precomputing the cosine seems difficult since am computing the cosine of the inner (loop) locals variables

那些余弦实际上只是以公式形式写下的常量,该数组仅取决于 n。比如看FFmpeg在dctref.c中是怎么做的

关于java - Java 中二维矩阵的 DCT 和 IDCT 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29334919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com