音频信标在 18 khz 到 19 khz 之间产生不同的频率。我正在尝试使用 AudioTrack Api 记录所有频率。我参考了这个链接How to get frequency from fft result? . 应用汉宁窗函数后,我得到的所有数据都是 0。1)如何应用汉宁窗?2)如何过滤频率?3)我录制不同范围频率的音频并将其保存在 .wav 格式中。我正在读取该音频文件并转换为频率。但我只得到高频。如何获得多个峰值频率?
int fftSize = 1024;
public void startRecord() {
short[] bytebuff = new short[2 * fftSize];
while (started) {
int bufferReadResult = audioRecord.read(bytebuff, 0, bytebuff.length);
if (bufferReadResult >= 0) {
fft(bytebuff);
}
}
}
public void fft(short[] bufferByte) {
int N = bufferByte.length;
DoubleFFT_1D fft1d = new DoubleFFT_1D(N);
double[] fft = new double[N * 2];
double[] magnitude = new double[N / 2];
for (int i = 0; i < N; i++) {//Hann window function
bufferByte[i] = (byte) (bufferByte[i] * 0.5 * (1.0 - Math.cos(2.0 * Math.PI * i / (bufferByte.length))));//here i'm getting all data is zero.
}
for (int i = 0; i < N - 1; ++i) {
fft[2 * i] = bufferByte[i];
fft[2 * i + 1] = 0;
}
fft1d.complexForward(fft);
// calculate power spectrum (magnitude) values from fft[]
for (int i = 0; i < (N / 2) - 1; i++) {
double real = fft[2 * i];
double imaginary = fft[2 * i + 1];
magnitude[i] = Math.sqrt(real * real + imaginary * imaginary);
}
double max_magnitude = -1;
int max_index = -1;
for (int i = 0; i < (N / 2) - 1; i++) {
if (magnitude[i] > max_magnitude) {
max_magnitude = magnitude[i];
max_index = i;
}
}
int freq = max_index * 44100 / N;
Log.e("AudioBEacon", "---" + freq);
}