在Android中使用FFT

发布于 2024-12-08 13:04:40 字数 2476 浏览 0 评论 0原文

我无法理解如何将 PCM 数据从麦克风传递到我正在使用的由 Piotr Wendykier 制作的 FFT 类(它是 JTransforms 中的 DoubleFFT_1D 类)。

我想我必须返回一个实数和虚数,然后将实数加倍才能最终获得Frequency = 8000 * i / 1024,其中i是最高幅度的索引。

有人可以帮我找到演奏音符的频率吗?

我的录音课如下:

import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;

...other various imports...

class recorderThread {

...public variables...

  public static void getFFtresult(){

         AudioRecord recorder;
            short[] audioData;
            int bufferSize;
            int samplerate = 8000;//or 8192?


            bufferSize= AudioRecord.getMinBufferSize(samplerate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
 AudioFormat.ENCODING_PCM_16BIT)*2; //get the buffer size to use with this audio record

recorder = new AudioRecord (AudioSource.MIC,samplerate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,bufferSize); //instantiate the AudioRecorder

recording=true; //variable to use start or stop recording
audioData = new short [bufferSize]; //short array that pcm data is put into.

int recordingLoops = 0;

 while (recordingLoops < 4) {  //loop while recording is needed

    if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED) // check to see if the recorder has initialized yet.
    if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
          recorder.startRecording();  //check to see if the Recorder has stopped or is not recording, and make it record.

    else {
       recorder.read(audioData,0,bufferSize);   //read the PCM audio data into the audioData array

       DoubleFFT_1D fft = new DoubleFFT_1D(1023); //instance of DoubleFFT_1D class

       double[] audioDataDoubles = new double[1024];

       for (int j=0; j <= 1023; j++) { // get audio data in double[] format
           audioDataDoubles[j] = (double)audioData[j];          
       }

       fft.complexForward(audioDataDoubles);  //this is where it falls

       for (int i = 0; i < 1023; i++) {
           Log.v(TAG, "audiodata=" + audioDataDoubles[i] + " no= " + i);
       }

       recordingLoops++;
      }//else recorder started

    } //while recording

    if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) recorder.stop(); //stop the recorder before ending the thread
    recorder.release(); //release the recorders resources
    recorder=null; //set the recorder to be garbage collected

   }//run

}//recorderThread

非常感谢!

I am having trouble understanding how I should pass PCM data from the mic to this FFT class I am using made by Piotr Wendykier (it's the DoubleFFT_1D class in JTransforms).

I think I have to return a real and imaginary number and then double the real number to eventually obtain Frequency = 8000 * i / 1024 where i is the index of the highest magnitude.

Can someone help me in finding the frequency of a note played in?

I have a recording class as follows:

import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;

...other various imports...

class recorderThread {

...public variables...

  public static void getFFtresult(){

         AudioRecord recorder;
            short[] audioData;
            int bufferSize;
            int samplerate = 8000;//or 8192?


            bufferSize= AudioRecord.getMinBufferSize(samplerate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
 AudioFormat.ENCODING_PCM_16BIT)*2; //get the buffer size to use with this audio record

recorder = new AudioRecord (AudioSource.MIC,samplerate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,bufferSize); //instantiate the AudioRecorder

recording=true; //variable to use start or stop recording
audioData = new short [bufferSize]; //short array that pcm data is put into.

int recordingLoops = 0;

 while (recordingLoops < 4) {  //loop while recording is needed

    if (recorder.getState()==android.media.AudioRecord.STATE_INITIALIZED) // check to see if the recorder has initialized yet.
    if (recorder.getRecordingState()==android.media.AudioRecord.RECORDSTATE_STOPPED)
          recorder.startRecording();  //check to see if the Recorder has stopped or is not recording, and make it record.

    else {
       recorder.read(audioData,0,bufferSize);   //read the PCM audio data into the audioData array

       DoubleFFT_1D fft = new DoubleFFT_1D(1023); //instance of DoubleFFT_1D class

       double[] audioDataDoubles = new double[1024];

       for (int j=0; j <= 1023; j++) { // get audio data in double[] format
           audioDataDoubles[j] = (double)audioData[j];          
       }

       fft.complexForward(audioDataDoubles);  //this is where it falls

       for (int i = 0; i < 1023; i++) {
           Log.v(TAG, "audiodata=" + audioDataDoubles[i] + " no= " + i);
       }

       recordingLoops++;
      }//else recorder started

    } //while recording

    if (recorder.getState()==android.media.AudioRecord.RECORDSTATE_RECORDING) recorder.stop(); //stop the recorder before ending the thread
    recorder.release(); //release the recorders resources
    recorder=null; //set the recorder to be garbage collected

   }//run

}//recorderThread

Thanks so much!

Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2024-12-15 13:04:40

如果您正在寻找音符的音高,您会发现音高通常与 FFT 产生的频谱频率峰值不同,特别是对于低音音符。

要从复数 FFT 中找到频率峰值,您需要计算实部和虚部结果的矢量幅度。

mag(i) = sqrt(real[i]*real[i] + imag[i]*imag[i]);

If you are looking for the pitch of a musical note, you will find that pitch is often different from the spectral frequency peak produced by an FFT, especially for lower notes.

To find the frequency peak from a complex FFT, you need to calculate the vector magnitude of both the real and imaginary results.

mag(i) = sqrt(real[i]*real[i] + imag[i]*imag[i]);
比忠 2024-12-15 13:04:40

由于您使用真实的音频数据作为输入,因此您应该使用 realForward 函数:

fft.realForward(audioDataDoubles);

然后您可以通过计算实部和虚部的幅度来计算频率上的能量:

magn[i] = audioDataDoubles[2*i]*audioDataDoubles[2*i] + audioDataDoubles[2*i+1]*audioDataDoubles[2*i+1]

Since you're using real audio data as the input you should use realForward function:

fft.realForward(audioDataDoubles);

Then you can compute the energy on a frequency by computing the magnitude of real and imaginary parts:

magn[i] = audioDataDoubles[2*i]*audioDataDoubles[2*i] + audioDataDoubles[2*i+1]*audioDataDoubles[2*i+1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文