audio - FFT in Python with Explanations -


i have wav file visualize in frequency domain. next, write simple script takes in wav file , outputs whether energy @ frequency "f" exceeds threshold "z" (whether tone has strong presence in wav file). there bunch of code snippets online show how plot fft spectrum in python, don't understand lot of steps.

  1. i know wavfile.read(myfile) returns sampling rate (fs) , data array (data), when run fft on (y = numpy.fft.fft(data)), units y in?
  2. to array of frequencies x-axis, posters n = len(data):

    x = numpy.linspace(0.0, 1.0/(2.0*t), n/2)

    and others this:

    x = numpy.fft.fftfreq(n) * fs)[range(n/2)]

    is there difference between these 2 methods , there online explanation these operations conceptually?

  3. some of online tutorials ffts mention windowing, not lot of posters use windowing in code snippets. see numpy has numpy.hamming(n), should use input method , how "apply" output window fft arrays?
  4. for threshold computation, correct find frequency in x that's closest desired tone/frequency , check if corresponding element (same index) in y has amplitude greater threshold?

  1. fft data in units of normalized frequency first point 0 hz , one past last point fs hz. can create frequency axis linspace(0.0, (1.0 - 1.0/n)*fs, n). can use fftfreq components negative.

  2. these same if n even. can use rfftfreq think. note "positive half" of frequencies, want audio (which real-valued). note can use rfft produce positive half of spectrum, , frequencies rfftfreq(n,1.0/fs).

  3. windowing decrease sidelobe levels, @ cost of widening mainlobe of frequencies there. n length of signal , multiply signal window. however, if looking in long signal might want "chop" pieces, window them, , add absolute values of spectra.

  4. "is correct" hard answer. simple approach said, find bin closest frequency , check amplitude.


Comments