我正在为从麦克风录制的音频数据中恢复原始频率等问题而奋斗。
对不起我的英语不好...
让我更清楚地解释这个问题。我使用下面的代码I生成了一些特定的频率:
void genTone() {
numSamples = (int)(0.2 * sampleRate); //duration * sampleRate;
sample = new double[numSamples];
generatedSnd = new byte[2 * numSamples];
// fill out the array
for (int i = 0; i < numSamples; ++i) {
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone));
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
}我用下面的代码II记录了声音:
private void recordInBackground() {
int read = 0;
while (isRecording) {
short data[] = new short[bufferSize]; // bufferSize = 4096
read = audioRecorder.read(data, 0, bufferSize);
if (read != AudioRecord.ERROR_INVALID_OPERATION) {
try {
float tempHammingRes[] = null;
hamming(bufferSize);
Complex[] complexs = new Complex[bufferSize];
Complex[] results = new Complex[bufferSize];
for (int i = 0; i < bufferSize; ++i) {
data[i] /= 32767;
tempHammingRes[i] = tempHammingRes[i] * data[i];
complexs[i]= new Complex(tempHammingRes[i], 0);
}
results = FFT.fft(complexs);
double highScore = 0.0;
int freq = 1;
for (int line = 1; line < bufferSize; ++line) {
double magnitude = Math.log(results[line].abs() + 1) / Math.log(10.0)*20.0;
if (magnitude > highScore) {
highScore = magnitude;
freq = line;
}
}
double currentFrequence = ComputeFrequency(freq, bufferSize);
Log.d(TAG, "highScore = " + highScore + " freq = " + currentFrequence);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}现在,我有一个问题,在代码块II中,在连续的FFT计算间隔中将获得相同的频率。例如,从代码块II输出一些日志:
highScore = 151.77662972416104 freq = 7999.5849609375 //第一个8000
highScore = 146.33073029829455 freq = 7999.5849609375 //第8000个
highScore = 146.44411729898255 freq = 9000.87890625
highScore = 144.43481176938155 freq = 9000.87890625
highScore = 142.78046692784702 freq = 10002.1728515625
highScore = 141.91874938214298 freq = 10002.1728515625
highScore = 136.47269911015098 freq = 11003.466796875
highScore = 136.6873278405228 freq = 11003.466796875
我只生成一个8khz,但我得到两个声音频率。我也减少输出音调的持续时间或增加录音机的输入缓冲区大小。不幸的是,它并没有帮助我想做的事情..
有没有人知道我是错的还是fft的输出是这样的?
非常感谢您的任何答案!