我现在已经玩过这个了一段时间,我无法弄清楚我本来打算做什么.
我正在将PCM音频数据读入audioData数组:
recorder.read(audioData,0,bufferSize); //read the PCM audio data into the audioData array
Run Code Online (Sandbox Code Playgroud)
我想使用Piotr Wendykier的JTransform库,以便在我的PCM数据上执行FFT以获得频率.
import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;
Run Code Online (Sandbox Code Playgroud)
目前我有这个:
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);
Run Code Online (Sandbox Code Playgroud)
我无法理解如何工作,有人可以给我一些指示吗?我必须在此之后进行任何计算吗?
我相信我已经离开了,任何事情都会非常感激!
本
如果您只是在寻找输入波形中单个正弦音的频率,那么您需要找到幅度最大的FFT峰值,其中:
Magnitude = sqrt(re*re + im*im)
Run Code Online (Sandbox Code Playgroud)
i这个最大幅度峰值的指数将告诉你正弦波的大概频率:
Frequency = Fs * i / N
Run Code Online (Sandbox Code Playgroud)
哪里:
Fs = sample rate (Hz)
i = index of peak
N = number of points in FFT (1024 in this case)
Run Code Online (Sandbox Code Playgroud)
由于我花了几个小时来让它工作,这里有一个完整的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
FrequencyScanner将返回所呈现样本数据中主频率的近似值。它将汉明窗口应用于其输入,以允许从音频流中传入任意样本。通过在进行 FFT 变换之前对样本数据进行内部补零来实现精度。(我知道有更好的——也更复杂的——方法来做到这一点,但填充方法足以满足我的个人需求)。
我针对从 220hz 和 440hz 的参考声音创建的原始 16 位 PCM 样本进行测试,结果匹配。
| 归档时间: |
|
| 查看次数: |
16422 次 |
| 最近记录: |