我已经玩了一段时间了,我不知道我在这里要做什么。
我正在读取PCM音频数据到audioData数组:
recorder.read(audioData,0,bufferSize); //read the PCM audio data into the audioData array
我想使用Piotr Wendykier的JTransform库,以便对我的PCM数据进行FFT,以获得频率。
import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;
现在我有这个:
DoubleFFT_1D fft = new DoubleFFT_1D(1024); // 1024 is size of array
for (int i = 0; i < 1023; i++) {
a[i]= audioData[i];
if (audioData[i] != 0)
Log.v(TAG, "audiodata=" + audioData[i] + " fft= " + a[i]);
}
fft.complexForward(a);
我不知道该怎么做,有人能给我点建议吗?在此之后,我必须执行任何计算吗?
我肯定我错了,任何事情都会非常感激!
本如果你只是在寻找输入波形中单个正弦波的频率,那么你需要找到幅度最大的FFT峰值,其中:
Magnitude = sqrt(re*re + im*im)
这个最大震级峰的指数i
将告诉你正弦波的近似频率:
Frequency = Fs * i / N
地点:
Fs = sample rate (Hz)
i = index of peak
N = number of points in FFT (1024 in this case)
由于我花了一些时间来让它工作,这里是一个完整的Java实现:
import org.jtransforms.fft.DoubleFFT_1D;
public class FrequencyScanner {
private double[] window;
public FrequencyScanner() {
window = null;
}
/** extract the dominant frequency from 16bit PCM data.
* @param sampleData an array containing the raw 16bit PCM data.
* @param sampleRate the sample rate (in HZ) of sampleData
* @return an approximation of the dominant frequency in sampleData
*/
public double extractFrequency(short[] sampleData, int sampleRate) {
/* sampleData + zero padding */
DoubleFFT_1D fft = new DoubleFFT_1D(sampleData.length + 24 * sampleData.length);
double[] a = new double[(sampleData.length + 24 * sampleData.length) * 2];
System.arraycopy(applyWindow(sampleData), 0, a, 0, sampleData.length);
fft.realForward(a);
/* find the peak magnitude and it's index */
double maxMag = Double.NEGATIVE_INFINITY;
int maxInd = -1;
for(int i = 0; i < a.length / 2; ++i) {
double re = a[2*i];
double im = a[2*i+1];
double mag = Math.sqrt(re * re + im * im);
if(mag > maxMag) {
maxMag = mag;
maxInd = i;
}
}
/* calculate the frequency */
return (double)sampleRate * maxInd / (a.length / 2);
}
/** build a Hamming window filter for samples of a given size
* See http://www.labbookpages.co.uk/audio/firWindowing.html#windows
* @param size the sample size for which the filter will be created
*/
private void buildHammWindow(int size) {
if(window != null && window.length == size) {
return;
}
window = new double[size];
for(int i = 0; i < size; ++i) {
window[i] = .54 - .46 * Math.cos(2 * Math.PI * i / (size - 1.0));
}
}
/** apply a Hamming window filter to raw input data
* @param input an array containing unfiltered input data
* @return a double array containing the filtered data
*/
private double[] applyWindow(short[] input) {
double[] res = new double[input.length];
buildHammWindow(input.length);
for(int i = 0; i < input.length; ++i) {
res[i] = (double)input[i] * window[i];
}
return res;
}
}
FrequencyScanner
将返回给出的样本数据中主导频率的近似值。它将汉明窗口应用于它的输入,以允许从音频流中传入任意样本。精度是通过在做FFT变换之前对样本数据进行内部零填充来实现的。(我知道有更好的——也更复杂的——方法来做到这一点,但填充方法已经足够满足我的个人需求了)。
我测试它对原始的16位PCM样本创建的参考声音为220hz和440hz,结果匹配。
是的,你需要使用realForward函数而不是complexForward,因为你传递给它一个真正的数组,而不是一个复杂的数组从doc.
编辑:或者你可以得到实部,像这样执行复数到复数的fft:
double[] in = new double[N];
read ...
double[] fft = new double[N * 2];
for(int i = 0; i < ffsize; ++i)
{
fft[2*i] = mic[i];
fft[2*i+1] = 0.0;
}
fft1d.complexForward(fft);
我尝试和我比较结果与matlab,我没有得到相同的结果…(级)
如果您正在寻找音频输入(1D,真实数据)的FFT,您不应该使用1D real FFT吗?