3

We are trying to build a program to get amplitude and frequency list from an .wav file, trying it in Python.

We tried pyaudio for that I don't know much about pyaudio, so I need some suggestions on it.

import scipy
import numpy as np

file = '123.wav'
from scipy.io import wavfile as wav
fs, data = wav.read(file)
length=len(data.shape)
#if length==2:
#    data= data.sum(axis=1)/2
n = data.shape[0]
sec = n/float(fs)
ts = 1.00/fs
t = scipy.arange(0,sec,ts)
FFT = abs(scipy.fft(data))
FFT_size = FFT[range(n//2)]
freq = scipy.fftpack.fftfreq(data.size, t[1]-t[0])
max_freq = max(freq)
min_freq = min(freq)
plot_freq(freq, n, t, data)

The actual result returning is frequency list. I also want amplitude list don't know how to get it.

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
Subhajit Dey
  • 91
  • 2
  • 4
  • Possible duplicate of [Trying to get the frequencies of a .wav file in Python](https://stackoverflow.com/questions/54612204/trying-to-get-the-frequencies-of-a-wav-file-in-python) – Lukasz Tracewski May 07 '19 at 18:59

3 Answers3

0

typically a call to an fft api will return an array of imaginary numbers where each array element contains a complex number in the form of ( Areal, AImaginary ) where each element of the array represents a frequency (the value of the freq is implied by array index [find the formula to calc freq based on array index])

on the complex array element 0 represents frequency 0 which is your direct current offset, then freq of each subsequent freq is calculated using

incr_freq := sample_rate / number_of_samples

so for that to be meaningful you must have prior knowledge of the sample rate of your source input time series ( audio or whatever ) and number of samples is just the length of the floating point raw audio curve array you fed into your fft call

... as you iterate across this array of complex numbers calculate the amplitude using the Areal and AImaginary of each frequency bin's complex number using formula

curr_mag = 2.0 * math.Sqrt(curr_real*curr_real+curr_imag*curr_imag) / number_of_samples

as you iterate across the complex array returned from your fft call be aware of notion of Nyquist Limit which means you only consume the first half of the number of elements of that complex array (and double the magnitude of each freq - see formula above)

... see the full pseudocode at Get frequency with highest amplitude from FFT

... I ran your code and nothing happened ... what is the meaning of your python

[range(n//2)]

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
  • I am getting a list freq which is the frequency list, and after that I am plotting it using that function plot_freq(). Here is the code for plot_freq() `def plot_freq(freqs, N, t, signal): fft_freqs = np.array(freqs) freqs_side = freqs[range(N//2)] # one side frequency range fft_freqs_side = np.array(freqs_side) plt.subplot(311) p1 = plt.plot(t, signal, "g") plt.xlabel('Time') plt.ylabel('Frequency')` – Subhajit Dey May 07 '19 at 02:32
  • range(n//2) will start from 0 to n/2 if n/2 is coming in point then making it round up. – Subhajit Dey May 08 '19 at 16:15
0

You possibly want pitch, not spectral frequency, which is a different algorithm than just using an FFT to find the highest magnitude. An FFT returns the entire spectral frequency range (every frequency up to Fs/2, not just one frequency), in your case for the entire file. And the highest magnitude is often not for the pitch frequency (possibly for some high overtone instead).

You also took the FFT of the entire file, not a bunch of FFTs for time slices (usually small overlapping windows) at the time increment you desire for your list's temporal resolution. This will produce a time array of all the FFT frequency arrays (thus, a 2D array). Usually called a spectrogram. There may be a built in function for this in some library.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • can you tell me how to get the the pitch frequency and amplitude. – Subhajit Dey May 08 '19 at 16:16
  • That's a new question. Search here on stackoverflow (or on the dsp.stackexchange), as that question may have already been asked and answered in various forms many times. – hotpaw2 May 08 '19 at 17:32
-1

Can I make amplitude from this formula

the frequency of the wave is set by whatever is driving the oscillation in the medium. Examples are a speaker that sets up a sound wave, or the hand that shakes the end of a stretched string. the speed of the wave is a property of the medium. the wavelength of the wave is then determined by the frequency and speed: λ = v/f

I don't know it gonna be the right process or not

Subhajit Dey
  • 91
  • 2
  • 4