端口Matlab';将FFT转换为本机Java
我想将Matlab的快速傅立叶变换函数fft()移植到本机Java代码中 作为起点,我使用FFT实现的代码,如下所示:端口Matlab';将FFT转换为本机Java,java,matlab,port,fft,Java,Matlab,Port,Fft,我想将Matlab的快速傅立叶变换函数fft()移植到本机Java代码中 作为起点,我使用FFT实现的代码,如下所示: // given double[] x as the input signal n = x.length; // assume n is a power of 2 nu = (int)(Math.log(n)/Math.log(2)); int n2 = n/2; int nu1 = nu - 1; double[] xre
// given double[] x as the input signal
n = x.length; // assume n is a power of 2
nu = (int)(Math.log(n)/Math.log(2));
int n2 = n/2;
int nu1 = nu - 1;
double[] xre = new double[n];
double[] xim = new double[n];
double[] mag = new double[n2];
double tr, ti, p, arg, c, s;
for (int i = 0; i < n; i++) {
xre[i] = x[i];
xim[i] = 0.0;
}
int k = 0;
for (int l = 1; l <= nu; l++) {
while (k < n) {
for (int i = 1; i <= n2; i++) {
p = bitrev (k >> nu1);
arg = 2 * (double) Math.PI * p / n;
c = (double) Math.cos (arg);
s = (double) Math.sin (arg);
tr = xre[k+n2]*c + xim[k+n2]*s;
ti = xim[k+n2]*c - xre[k+n2]*s;
xre[k+n2] = xre[k] - tr;
xim[k+n2] = xim[k] - ti;
xre[k] += tr;
xim[k] += ti;
k++;
}
k += n2;
}
k = 0;
nu1--;
n2 = n2/2;
}
k = 0;
int r;
while (k < n) {
r = bitrev (k);
if (r > k) {
tr = xre[k];
ti = xim[k];
xre[k] = xre[r];
xim[k] = xim[r];
xre[r] = tr;
xim[r] = ti;
}
k++;
}
// The result
// -> real part stored in xre
// -> imaginary part stored in xim
//给定双[]x作为输入信号
n=x.length;//假设n是2的幂
nu=(int)(Math.log(n)/Math.log(2));
int n2=n/2;
int nu1=nu-1;
double[]xre=新的double[n];
double[]xim=新的double[n];
double[]mag=新的双精度[n2];
双tr,ti,p,arg,c,s;
对于(int i=0;ik){
tr=xre[k];
ti=xim[k];
xre[k]=xre[r];
xim[k]=xim[r];
xre[r]=tr;
xim[r]=ti;
}
k++;
}
//结果
//->存储在xre中的真实零件
//->存储在xim中的虚部
不幸的是,当我对它进行单元测试时,它并没有给出正确的结果,例如使用数组
双[]x={1.0d,5.0d,9.0d,13.0d}
Matlab中的结果:
28.0-8.0-8.0i
-8.0
-8.0+8.0i 我实施的结果是: 28.0
-8.0+8.0i
-8.0
-8.0-8.0i 注意复杂部分的符号是如何错误的 当我使用更长、更复杂的信号时,实现之间的差异也会影响数字。因此,实现上的差异不仅仅与一些符号“错误”有关 我的问题是:如何调整我的实现,使其与Matlab实现“相等”?
或者:已经有这样的库了吗?为了在矩阵上使用Jtransforms进行FFT,您需要逐列进行FFT,然后将它们合并到矩阵中。这是我与Matlab fft比较的代码
double [][] newRes = new double[samplesPerWindow*2][Matrixres.numberOfSegments];
double [] colForFFT = new double [samplesPerWindow*2];
DoubleFFT_1D fft = new DoubleFFT_1D(samplesPerWindow);
for(int y = 0; y < Matrixres.numberOfSegments; y++)
{
//copy the original col into a col and and a col of zeros before FFT
for(int x = 0; x < samplesPerWindow; x++)
{
colForFFT[x] = Matrixres.res[x][y];
}
//fft on each col of the matrix
fft.realForwardFull(colForFFT); //Y=fft(y,nfft);
//copy the output of col*2 size into a new matrix
for(int x = 0; x < samplesPerWindow*2; x++)
{
newRes[x][y] = colForFFT[x];
}
}
如果我在Matlab中运行它,我会得到第二组数字。
native Java
不是矛盾修饰法吗?:-)你能举一个8号的不一致的例子吗?16号?谢谢。投票结束:你一定是搞错了,因为Matlab(或手动计算)得出的是你列出的第二组数字,而不是第一组。@Oli Charlesworth Aha:当将4x1矩阵放入Matlab时,它实际上产生了相同的数字,而当作为1x4矩阵时,它给出了我问题中给出的错误数字。如何调整实现以使用双[][]阵列,从而支持实矩阵(具有多行和多列)?
array[2*k] = Re[k], array[2*k+1] = Im[k]