gpt4 book ai didi

android - Jtransforms,输出频率不准确。

转载 作者:太空狗 更新时间:2023-10-29 13:23:33 25 4
gpt4 key购买 nike

我正在为 Google Glass 开发一个应用程序,该应用程序自录制音频以来实时显示峰值电流峰值频率。我当前的问题是频率报告变化非常快,因此很难确定频率我也不确定我的 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");
}
});


}

最佳答案

您的代码有几个问题,但最重要的一个是您的峰值查找循环完全中断 - 更改:

    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
}
}

关于android - Jtransforms,输出频率不准确。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24973642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com