我正在开发一款适用于Google Glass的应用程序,该应用程序自录制的音频以来实时显示峰值电流峰值频率(ish)。我目前的问题是频率报告变化非常快,因此很难确定频率,我也不确定我的NumberFormat输出格式是否正确,因为它只能达到“00.000”。我可能需要一些窗口帮助,但我对它的理解是存在的。
谢谢!
public class RTAactivity extends Activity {
private static final int SAMPLING_RATE = 44100;
private TextView tvfreq;
private TextView tvdb;
private RecordingThread mRecordingThread;
private int mBufferSize;
private short[] mAudioBuffer;
private String mDecibelFormat;
private double mFreqFormat = 0.0;
private int blockSize = 1024; //4096
private DoubleFFT_1D fft;
private int[] bufferDouble, bufferDouble2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rta_view);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
tvfreq = (TextView) findViewById(R.id.tv_freq);
tvdb = (TextView) findViewById(R.id.tv_decibels);
// Compute the minimum required audio buffer size and allocate the buffer.
mBufferSize = AudioRecord.getMinBufferSize(SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
mAudioBuffer = new short[mBufferSize / 2];
bufferDouble2 = new int[mBufferSize /2];
bufferDouble = new int[(blockSize-1) * 2 ];
mDecibelFormat = getResources().getString(R.string.decibel_format);
}
@Override
protected void onResume() {
super.onResume();
mRecordingThread = new RecordingThread();
mRecordingThread.start();
}
@Override
protected void onPause() {
super.onPause();
if (mRecordingThread != null) {
mRecordingThread.stopRunning();
mRecordingThread = null;
}
}
private class RecordingThread extends Thread{
private boolean mShallContinue = true;
@Override
public void run() {
android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO);
AudioRecord record = new AudioRecord(AudioSource.MIC, SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, mBufferSize);
short[] buffer = new short[blockSize];
double[] audioDataDoubles = new double[(blockSize * 2)];
double[] re = new double[blockSize];
double[] im = new double[blockSize];
double[] magnitude = new double[blockSize];
//start collecting data
record.startRecording();
DoubleFFT_1D fft = new DoubleFFT_1D(blockSize);
while (shallContinue()) {
/**decibels */
record.read(mAudioBuffer, 0, mBufferSize / 2);
updateDecibelLevel();
/**frequency */
///windowing!?
for(int i=0;i<mAudioBuffer.length;i++) {
bufferDouble2[i] = (int) mAudioBuffer[i];
}
for(int i=0;i<blockSize-1;i++){
double x=-Math.PI+2*i*(Math.PI/blockSize);
double winValue=(1+Math.cos(x))/2.0;
bufferDouble[i]= (int) (bufferDouble2[i]*winValue); }
// bufferDouble[2*i]=bufferDouble2[i];
// bufferDouble[2*i+1] = (int) 0.0;}
int bufferReadResult = record.read(buffer, 0, blockSize);
// Read in the data from the mic to the array
for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
audioDataDoubles[2 * i] = (double) buffer[i] / 32768.0; // signed 16 bit
audioDataDoubles[(2 * i) + 1] = 0.0;
}
//audiodataDoubles now holds data to work with
fft.complexForward(audioDataDoubles); //complexForward
// Calculate the Real and imaginary and Magnitude.
for (int i = 0; i < blockSize; i++) {
double real = audioDataDoubles[2 * i];
double imag = audioDataDoubles[2 * i + 1];
magnitude[i] = Math.sqrt((real * real) + (imag * imag));
}
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++) {
peak = magnitude[i];
}
// calculated the frequency
mFreqFormat = (SAMPLING_RATE * peak) / blockSize;
updateFrequency();
}
record.stop(); //stop recording please.
record.release(); // Deystroy the recording, PLEASE!
}
/**true if the thread should continue running or false if it should stop
*/
private synchronized boolean shallContinue() {return mShallContinue; }
/** Notifies the thread that it should stop running at the next opportunity. */
private synchronized void stopRunning() { mShallContinue = false; }
private void updateDecibelLevel() {
// Compute the root-mean-squared of the sound buffer and then apply the formula for
// computing the decibel level, 20 * log_10(rms). This is an uncalibrated calculation
// that assumes no noise in the samples; with 16-bit recording, it can range from
// -90 dB to 0 dB.
double sum = 0;
for (short rawSample : mAudioBuffer) {
double sample = rawSample / 32768.0;
sum += sample * sample;
}
double rms = Math.sqrt(sum / mAudioBuffer.length);
final double db = 20 * Math.log10(rms);
// Update the text view on the main thread.
tvdb.post(new Runnable() {
@Override
public void run() {
tvdb.setText(String.format(mDecibelFormat, db));
}
});
}
}
/// post the output frequency to TextView
private void updateFrequency() {
tvfreq.post(new Runnable() {
@Override
public void run() {
NumberFormat nM = NumberFormat.getNumberInstance();
tvfreq.setText(nM.format(mFreqFormat) + " hz");
}
});
}
}
答案 0 :(得分:1)
补充:仅使用FFT的峰值幅度bin的频率分辨率将被设置(量化)为采样率除以FFT的长度(参数为44100/1024 Hz)。对于短FFT,430 Hz可能是距离440最近的FFT结果仓。为了做得更好,您需要进行插值,使用更长的FFT或使用其他频率估算算法。
如果您正在尝试显示音高(音高或者音高),这通常与FFT结果的峰值频谱频率不同。查找音调检测/估计方法(关于该主题的许多学术论文),因为这通常需要比计算FFT幅度峰值更复杂和更健壮的算法。
答案 1 :(得分:1)
您的代码存在一些问题,但最重要的是您的峰值查找循环完全被破坏 - 更改:
double peak = -1.0;
// Get the largest magnitude peak
for (int i = 0; i < blockSize; i++) {
peak = magnitude[i];
}
为:
double peak_val = magnitude[0]; // init magnitude of peak
peak = 0; // init index of peak
for (int i = 1; i < blockSize; i++) {
double val = magnitude[i];
if (val > peak_val) {
peak_val = val; // update magnitude of peak
peak = i; // update index of peak
}
}