我正在为诸如从麦克风记录的音频数据中恢复原始频率之类的问题而战。
对不起我的英语不好...
让我更清楚地解释这个问题。我使用以下代码生成了一些特定的频率:
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 频率 = 9000.87890625
highScore = 144.43481176938155 频率 = 9000.87890625
highScore = 142.78046692784702 频率 = 10002.1728515625
highScore = 141.91874938214298 频率 = 10002.1728515625
highScore = 136.47269911015098 频率 = 11003.466796875
highScore = 136.6873278405228 频率 = 11003.466796875
我只产生了一个 8khz,但我得到了两个声音频率。我还减少了输出音调的持续时间或增加了录音机的输入缓冲区大小。不幸的是,这对我想做的事没有帮助..
有谁知道我是错的还是fft的输出本质上是这样的?
非常感谢您的任何回答!