java - FFT 中频率检测的问题

标签 java android fft frequency

我正在使用 FFT 来计算通过设备麦克风持续音符的频率。我正在使用 JTransform 来计算 FFT。代码如下:

//Mic reading variables
int audioSource = MediaRecorder.AudioSource.MIC;
// Audio source is the device mic
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
// Recording in mono
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
// Records in 16bit
//Frequency calculation variables
double[] audioDataDoubles;
private DoubleFFT_1D fft;
// The fft double array I am unsure if these values are correct. I cant seem to find a good balance
int blockSize = 256;
// deal with this many samples at a time
int sampleRate = 8000;
// Sample rate in Hz
double[] ringBuffer = new double[10];
int ring = 0;
double avgFreq = 0.0;
double smoothing = 20.0;
// The power for the low pass filter, The higher the more powerful

我的低通滤波器

//Low pass Filter
public void smoothArray(double[] audio, double smoothing){
  /* The Low pass filter removes the high frequency changes to signal.
  * That being background noise, e.g. hum of computers*/
  // Takes the first audio data value
  double smooth = audio[0];
  Long lastUpdate = System.currentTimeMillis()/1000;
  for(int i = 1; i < audio.length; i++){
    Long now = System.currentTimeMillis()/1000;
    double currentValue = audio[i];
    /*Calculates the difference of two signals and 
    * divides it by the smoothing power.
    * A Smoothing power of 1 will leave the data untouched.
    * A higher number will remove the high frequency.
    */
    Long elapsedTime = now - lastUpdate;
    double elapsed = elapsedTime.doubleValue();
    smooth += elapsed * (currentValue - smooth) / smoothing;
    lastUpdate = now;
    audio[i] = smooth;
  }
}

记录类

short[] buffer = new short[blockSize];
// Save the raw PCM samples as short bytes
audioDataDoubles = new double[(blockSize*2)];
// Same values as above, as doubles
int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioEncoding);    
// Gets the minimum buffer needed
audioRecord = new AudioRecord(audioSource, sampleRate, channelConfig,
  audioEncoding, bufferSize);
//bufferSize
audioRecord.startRecording();
// Start working
record = true;
// mic in use
fft = new DoubleFFT_1D(blockSize);
while(started){
  /* Reads the data from the microphone. it takes in data
  * to the size of the window "blockSize". The data is then
  * given in to audioRecord. The int returned is the number
  * of bytes that were read*/
  int bufferReadResult = audioRecord.read(buffer, 0, blockSize);
  // Read in the data from the mic to the array
  // takes from buffer and passes to audiodataDoubles
  fillArray(audioDataDoubles, buffer, blockSize, bufferReadResult);
}
//Apply the low pass filter to remove noise
smoothArray(audioDataDoubles, smoothing);
//audiodataDoubles now holds data to work with
fft.complexForward(audioDataDoubles);
double[] re = new double[blockSize];
double[] im = new double[blockSize];
double[] magnitude = new double[blockSize];
// Calculate the Real and imaginary and Magnitude.
for(int i = 0; i < blockSize; i++){
  // real is stored in first part of array
  re[i] = audioDataDoubles[i*2];
  // imaginary is stored in the sequential part
  im[i] = audioDataDoubles[(i*2)+1];
  // magnitude is calculated by the square root of (imaginary^2 + real^2)
  magnitude[i] = Math.sqrt((re[i] * re[i]) + (im[i]*im[i]));
}
double peak = -1.0;
// Get the largest magnitude peak
for(int i = 0; i < blockSize; i++){
  if(peak < magnitude[i])
    peak = magnitude[i];
}
// calculated the frequency
frequency = sampleRate * peak/blockSize;
ringBuffer[ring] = frequency;
ring++;
if(ring == (ringBuffer.length -1)){
  for(int j = 0; j < ring; j++){
    avgFreq = avgFreq + ringBuffer[j];
  }
  double avg = (double) ring;
  avgFreq = avgFreq/avg;
  Log.i("AudioRecord", "HZ: " + avgFreq);
  /* calls onProgressUpdate
  * publishes the frequency
  */
  publishProgress(avgFreq);
  //restart the ring buffer
  ring = 0;
}

返回的频率不正确且不稳定。我期望至少有一个恒定的频率数,但它不断变化。例如,以 440.1 Hz 读取会返回 290hz-390hz。我已经运行了一个图表,它提供了一个不会改变的预期峰值。有人能发现我的错误吗? 谢谢。

最佳答案

这可能无法解决您的基本问题,但低通滤波器肯定是错误的:您正在使用时钟中的时间 (System.currentTimeMillis()),但音频数据已在之前的某个时间捕获时间间隔。

我不确定过滤器应该做什么。但它在您的评论中说 smoothing=1 应该保持数据不变,但我不认为情况是这样。

关于java - FFT 中频率检测的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16792181/

相关文章:

java - 类之间的 Java 中的 JNI 作用域

android - 方法 startManagingCursor 和构造函数 SimpleCursorAdapter 已弃用

java - Android状态栏中的动画

android - SwitchPreference状态从关闭变为打开时如何更改图标?

audio - WAV 文件和 M4A 文件有什么区别?

java - 如何为 Thread 类编写 Junit 测试用例

java - Hibernate 和 Hibernate JPA 的区别

java - toString 方法返回随机字符

c++ - fftw 和在线 DFT 计算器得到不同的结果

transform - 如何从傅里叶变换绘制频谱