简体   繁体   中英

Python FFT an audio file

I'm trying to plot a magnitude-frequency spectrum from a wav file, the sample rate of the file is 44.1KHz, I only want to compute FFT of the first 100 samples, for that I'm using np.fft.fft() However I am getting unexpected results, see image 1.

在此处输入图片说明

I am only getting expected results when I'm computing FFT of at least 2048 samples. why?

Here is my code:

import numpy as np
import matplotlib.pyplot as plt

def normalizeAudio(data):
    return np.float32((data / max(data)))

SAMPLE_FOR = 1 # in seconds
samplerate, data = scipy.io.wavfile.read(r'Recording.wav')
data = normalizeAudio(data[0:int(samplerate*SAMPLE_FOR)])


fft_out = np.fft.fft(data[0:100])
freq_vector = np.arange(0, 44100, 44100 / 100)
plt.plot(freq_vector, np.abs(fft_out))
plt.show()

Have a look into librosa , it's a very good library for audio analysis in python, including chromatographs, spectograms, percussion graphs and other cool stuff.

It's very well documented with examples and plenty others on stack overflow so I won't copy them here.

Additionally, most applications I've seen tend to use STFT, short-term fourier transform (aka DFT, Discrete Fourier Transform). This is much faster still than plain FFT and more useful for modelling and things to control input shape and also removes a lot of the noise that you can get with FFT as it takes windows rather than instantaneous changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM