fft的java代码_Java代码从FFT结果中获取频率

我有一个包含音频样本的双精度阵列,以44100的采样率直接从麦克风传来。我想要得到基频(样本包含幅度)。在自相关页面的维基百科上,我找到了基于维纳 - 钦钦定理的解决方案的描述,我通过对互联网的更多研究完成了该算法,并最终编写了下面的代码,但我不确定它是否正确:

private double determineFrequency(double[] signal) {

//Get a FastFourierTransformer instance (Apache library)

FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);

//The size of the array used by the fft must be a power of two, wrapping

//the original array in a bigger one padded to zero

//NOTE: Here I assume that the input array is smaller than 8192

double[] paddedSignal = new double[8192];

System.arraycopy(signal, 0, paddedSignal, 0, signal.length);

//First fft (forward) to switch from amplitude domain to the frequency domain

Complex[] transformed = fft.transform(paddedSignal, TransformType.FORWARD);

// Calculate the conjugate of the complex array

for (int i=0; i

transformed[i] = transformed[i].conjugate();

//Second fft (inverse) to complete the autocorrelation

transformed = fft.transform(transformed, TransformType.INVERSE);

//Calculate the array of corresponding real values to switch

// from the frequency domain to the amplitude domain

double[] autocorrelationMatrix = new double[transformed.length];

for (int i=0; i

if (Double.isNaN(transformed[i].abs()) || Double.isInfinite(transformed[i].abs()))

autocorrelationMatrix[i] = 0;

else

autocorrelationMatrix[i] = transformed[i].abs();

}

//Get the index of the max amplitude

Integer indexOfMax = Utils.indexOfMax(autocorrelationMatrix);

return transformed[indexOfMax].getReal()*audioFormat.getSampleRate()/transformed.length;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值