从计算的音频文件FFT [英] Calculate FFT from audio file

查看:348
本文介绍了从计算的音频文件FFT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在,我问了一下<一个问题href=\"http://stackoverflow.com/questions/17429407/get-frequency-wav-audio-using-fft-and-complex-class\">Get使用FFT复杂的阶级频率WAV音频,

Before, I asked question about Get frequency wav audio using FFT and Complex class ,

有,我需要从AudioRecord输入计算FFT值 - >从话筒,我总算得到FFT值...

There, I need to calculate FFT value from AudioRecord input --> from microphone , I somehow managed to get the FFT value...

现在我需要从我以前保存的* .wav音频文件FFT计算值,
我救了里面水库文件夹中的原始文件夹中的声音从我的项目

Now I need to calculate FFT value from *.wav audio file that I saved before, I saved the audio in 'raw' folder inside 'res' folder from my project

我仍然使用相同的FFT类: HTTP://www.cs。 princeton.edu/introcs/97data/FFT.java

I still using the same FFT Class: http://www.cs.princeton.edu/introcs/97data/FFT.java

复杂类去用它:的http:// introcs。 cs.princeton.edu/java/97data/Complex.java.html

我用这个方法从我的生foldern读取音频文件,然后调用方法calculateFFT去用它。

I use this method to read audio file from my raw foldern, then I call method calculateFFT to go with it

private static final int RECORDER_BPP = 16;
  private static final int RECORDER_SAMPLERATE = 44100;
  private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
  private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;


private void asli(){

            int counter = 0;
            int data;
            InputStream inputStream  = getResources().openRawResource(R.raw.b1);
            DataInputStream dataInputStream = new DataInputStream(inputStream);
            List<Integer> content = new ArrayList<Integer>(); 

            try {
                while ((data = dataInputStream.read()) != -1) {
                    content.add(data);
                    counter++; }
            } catch (IOException e) {
                e.printStackTrace();}

                int[] b = new int[content.size()];
                int cont = 0;
                byte[] audio = convertArray(b);
        }

方法转换为字节

public byte[] convertArray(int[] array) { 

            int minBufferSize = AudioTrack.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);
                AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING,minBufferSize, AudioTrack.MODE_STREAM);

        byte[] newarray = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
        newarray[i] = (byte) ((array[i]) & 0xFF);       }

            absNormalizedSignal = calculateFFT(newarray);
            return newarray;
        }

这是CalculateFFT方法

And this is the CalculateFFT method

public double[] calculateFFT(byte[] signal)
        {           
            final int mNumberOfFFTPoints =1024;
            double mMaxFFTSample;
            double temp;
            Complex[] y;
            Complex[] complexSignal = new Complex[mNumberOfFFTPoints];
            double[] absSignal = new double[mNumberOfFFTPoints/2];

            for(int i = 0; i < mNumberOfFFTPoints; i++){
                temp = (double)((signal[2*i] & 0xFF) | (signal[2*i+1] << 8)) / 32768.0F;
                complexSignal[i] = new Complex(temp,0.0);
            }

            y = FFT.fft(complexSignal);

            mMaxFFTSample = 0.0;
            mPeakPos = 0;
            for(int i = 0; i < (mNumberOfFFTPoints/2); i++)
            {
                 absSignal[i] = Math.sqrt(Math.pow(y[i].re(), 2) + Math.pow(y[i].im(), 2));
                 if(absSignal[i] > mMaxFFTSample)
                 {
                     mMaxFFTSample = absSignal[i];
                     mPeakPos = i;
                 } 
            }

            return absSignal;

        }

我用这个方法CalculateFFT也从AudioRecorder处理音频 - >一个带麦克风输入之前......我设法从AudioRecorder获得价值,但我无法从我的音频文件获得价值......我米不打算播放音频..我只是需要FFT处理它。

I used this CalculateFFT method too to process audio from AudioRecorder --> that one with microphone input before... I managed to get value from the AudioRecorder, but I failed to get value from my audio file... I'm not planning to play the audio.. I just need to process it with FFT.

有什么毛病我code 17 :○好像我无法从方法阿斯利获得价值();但我不知道哪一部分是错误的。

Is there any wrong with my code ?? :o Seems like I fail at getting value from method Asli(); But I dont know which part is wrong..

任何帮助将是AP preciated ... :)
谢谢

Any help would be appreciated... :) Thanks

推荐答案

我花了上午的大部分时间编码使用此位和FFT的java代码片段,我发现件解决......但后来我偶然发现了这个令人惊讶的是有对WAV和MP3文件做信号处理任务都一堆UTIL类wondeful谷歌code项目。

I spent a better part of the morning coding a solution for this using bits and pieces of FFT java snippets I was finding... but then I stumbled upon this amazingly wondeful google code project that has a bunch of util classes for doing signal processing tasks on WAV and MP3 files alike.

的https://$c$c.google.com/p/audio -Analysis /
SVN: HTTP://audio-analysis.google$c$c.com/svn/

现在变得非常轻松:

WaveDecoder decoder = new WaveDecoder(new FileInputStream(wavFile));
FFT fft = new FFT(1024, wavFileObj.getSampleRate());

现在您可以使用FFT对象做各种计算。他们有一群伟大的例子,比如生成包含光谱通量列表:

Now you can use the fft object to do various calculations. They have a bunch of great examples, such as generating a List containing the spectral flux:

    float[] samples = new float[1024];
    float[] spectrum = new float[1024 / 2 + 1];
    float[] lastSpectrum = new float[1024 / 2 + 1];
    List<Float> spectralFlux = new ArrayList<Float>();

    while (decoder.readSamples(samples) > 0) {
        fft.forward(samples);
        System.arraycopy(spectrum, 0, lastSpectrum, 0, spectrum.length);
        System.arraycopy(fft.getSpectrum(), 0, spectrum, 0, spectrum.length);

        float flux = 0;
        for (int i = 0; i < spectrum.length; i++)
            flux += (spectrum[i] - lastSpectrum[i]);
        spectralFlux.add(flux);
    }

我公司需要找到一种方法让我分析一些音频,看看一些有望保持音乐没有发生过。因此,首先我生成该确实有保持音乐的例子的WAV文件。然后,我抓住了一个没有保持你的音乐的例子之一的一些音频。现在,所有剩下的就是平均起来WAV的光谱通量和我设置的。

My company needed a way for me to analyze some audio to see if some expected hold music had happened. So first I generated a WAV file for an example that did have the hold music. Then I captured some audio of one of thee examples that did not have the hold music. Now all that is left is to average up the spectral flux of the wav and I'm set.

请注意:我不能简单地采取振幅......但傅立叶变换有,我可以正确使用,使我的比较频率。

Note: I could not have simply taken amplitudes... but the fourier transformation has frequencies that I could correctly use to make my comparison.

我喜欢数学。

这篇关于从计算的音频文件FFT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆