Calculate FFT from audio file

前端 未结 1 1184
野的像风
野的像风 2020-12-19 04:56

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

There, I need to calculate FFT value from AudioRecord input --> from microphone

相关标签:
1条回答
  • 2020-12-19 05:47

    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://github.com/Uriopass/audio-analysis Formerly SVN export was on Google code here: https://storage.googleapis.com/google-code-archive-source/v2/code.google.com/audio-analysis/source-archive.zip

    It now becomes INSANELY easy:

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

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

    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.

    I love math.

    0 讨论(0)
提交回复
热议问题