Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>np.fft.fftfreq</code> tells you the frequencies associated with the coefficients:</p> <pre><code>import numpy as np x = np.array([1,2,1,0,1,2,1,0]) w = np.fft.fft(x) freqs = np.fft.fftfreq(len(x)) for coef,freq in zip(w,freqs): if coef: print('{c:&gt;6} * exp(2 pi i t * {f})'.format(c=coef,f=freq)) # (8+0j) * exp(2 pi i t * 0.0) # -4j * exp(2 pi i t * 0.25) # 4j * exp(2 pi i t * -0.25) </code></pre> <hr> <p>The OP asks how to find the frequency in Hertz. I believe the formula is <code>frequency (Hz) = abs(fft_freq * frame_rate)</code>.</p> <p>Here is some code that demonstrates that.</p> <p>First, we make a wave file at 440 Hz:</p> <pre><code>import math import wave import struct if __name__ == '__main__': # http://stackoverflow.com/questions/3637350/how-to-write-stereo-wav-files-in-python # http://www.sonicspot.com/guide/wavefiles.html freq = 440.0 data_size = 40000 fname = "test.wav" frate = 11025.0 amp = 64000.0 nchannels = 1 sampwidth = 2 framerate = int(frate) nframes = data_size comptype = "NONE" compname = "not compressed" data = [math.sin(2 * math.pi * freq * (x / frate)) for x in range(data_size)] wav_file = wave.open(fname, 'w') wav_file.setparams( (nchannels, sampwidth, framerate, nframes, comptype, compname)) for v in data: wav_file.writeframes(struct.pack('h', int(v * amp / 2))) wav_file.close() </code></pre> <p>This creates the file <code>test.wav</code>. Now we read in the data, FFT it, find the coefficient with maximum power, and find the corresponding fft frequency, and then convert to Hertz:</p> <pre><code>import wave import struct import numpy as np if __name__ == '__main__': data_size = 40000 fname = "test.wav" frate = 11025.0 wav_file = wave.open(fname, 'r') data = wav_file.readframes(data_size) wav_file.close() data = struct.unpack('{n}h'.format(n=data_size), data) data = np.array(data) w = np.fft.fft(data) freqs = np.fft.fftfreq(len(w)) print(freqs.min(), freqs.max()) # (-0.5, 0.499975) # Find the peak in the coefficients idx = np.argmax(np.abs(w)) freq = freqs[idx] freq_in_hertz = abs(freq * frate) print(freq_in_hertz) # 439.8975 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload