更改块大小会导致 FFT 分析失败
Posted
技术标签:
【中文标题】更改块大小会导致 FFT 分析失败【英文标题】:Changing block size causes FFT analysis to fail 【发布时间】:2016-02-08 17:52:56 【问题描述】:我正在尝试录制音频并获取频率。我可以以 44100 的采样率和 2048 的块大小成功地做到这一点。我相信 bin 大小约为 20。但是,如果我尝试将块大小增加到 4096,而不是获得准确的频率,我只会得到相同的不准确频率,没有幅度/分贝。
我的录音任务如下:
private class RecordAudio extends AsyncTask<Void, double[], Boolean>
@Override
protected Boolean doInBackground(Void... params)
int bufferSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(
MediaRecorder.Audiosource.DEFAULT, frequency,
channelConfiguration, audioEncoding, bufferSize);
int bufferReadResult;
short[] buffer = new short[blockSize];
double[] toTransform = new double[blockSize];
try
audioRecord.startRecording();
catch (IllegalStateException e)
Log.e("Recording failed", e.toString());
while (started)
if (isCancelled() || (CANCELLED_FLAG == true))
started = false;
//publishProgress(cancelledResult);
Log.d("doInBackground", "Cancelling the RecordTask");
break;
else
bufferReadResult = audioRecord.read(buffer, 0, blockSize);
for (int i = 0; i < blockSize && i < bufferReadResult; i++)
toTransform[i] = (double) buffer[i] / 32768.0; // signed 16 bit
transformer.ft(toTransform);
publishProgress(toTransform);
return true;
@Override
protected void onProgressUpdate(double[]...progress)
int mPeakPos = 0;
double mMaxFFTSample = 150.0;
for (int i = 100; i < progress[0].length; i++)
int x = i;
int downy = (int) (150 - (progress[0][i] * 10));
int upy = 150;
//Log.i("SETTT", "X: " + i + " downy: " + downy + " upy: " + upy);
if(downy < mMaxFFTSample)
mMaxFFTSample = downy;
mMag = mMaxFFTSample;
mPeakPos = i;
mFreq = (((1.0 * frequency) / (1.0 * blockSize)) * mPeakPos)/2;
//Log.i("SSS", "F: " + mFreq + " / " + "M: " + mMag);
Log.i("SETTT", "FREQ: " + mFreq + " MAG: " + mMaxFFTSample);
@Override
protected void onPostExecute(Boolean result)
super.onPostExecute(result);
try
audioRecord.stop();
catch(IllegalStateException e)
Log.e("Stop failed", e.toString());
希望我缺少一个快速修复。谢谢。
【问题讨论】:
transformer 是如何初始化的,它的类型是什么,所以我们至少可以参考它的文档?另外,“没有大小”是什么意思?一切都是零吗? 我正在使用 RealDoubleFFT,可以在这里找到:github.com/charlesmunger/RoomDetector/blob/master/src/ca/uol/… 【参考方案1】:您需要仔细查看RealDoubleFft.ft
函数的文档。输入函数的值是实数,但输出的值是复数 FFT 系数,例如 toTransform[0]
是第一个系数的实部,toTransform[1]
是第一个系数的虚部,依此类推。最终的数组大小相同,但由于每个复数占用 2 个双精度数,因此总共有 N/2 个系数,其中最后一个是 sampleRate/2 的系数。
接下来,由于您对计算复数大小所需的大小感兴趣。对于复数x = a + bj
,大小为|x| = sqrt(a*a + b*b)
double maxMag = 0;
int peakIndex = 0;
for (int i = 0; i < progress[0].length/2; i++)
double re = progress[i*2];
double im = progress[i*2+1];
double mag = Math.sqrt(re*re + im*im);
if (mag > maxMag)
peakIndex = i;
maxMag = mag;
double peakFreq = sampleRate/fftLen * i/2; // might need a bit of tweaking.
double magInDb = 20*Math.log10(mag);
【讨论】:
以上是关于更改块大小会导致 FFT 分析失败的主要内容,如果未能解决你的问题,请参考以下文章