Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Calculating frequency over time from a wav file in Python?

I am using scipy's wavfile library to read a wavfile.

rate, data = scipy.io.wavfile.read(filename)

This will return the rate and RAW data of the given wav filename.

What transformation on the data array do I need to do to go from RAW data to frequency?

I understand FFT is used to go to the frequency domain, but I would like to go to the time domain.

Any help is appreciated! :)

like image 813
Eduardo Morales Avatar asked Feb 22 '19 20:02

Eduardo Morales


People also ask

How do I find the frequency of a WAV file?

If you have a . wav file you can calculate the frequency directly. If the file is a single pure tone simply count n the number of samples between successive 0 crossings and divide 44.1/n to get the frequency in kHz. If the file is a mix of tones then you will need to do a Fourier transform.

How do you find the frequency of an audio file?

If you need to determine the bitrate or frequency of an audio file, such as an MP3, or the audio in a movie file, such as an MPEG or MP4, you can do so on Windows using a free program called Pazera Free Audio Extractor. This program includes the powerful FFMPEG library with an easy-to-use and quick interface.

How do you find the frequency of a signal in Python?

We can obtain the magnitude of frequency from a set of complex numbers obtained after performing FFT i.e Fast Fourier Transform in Python. The frequency can be obtained by calculating the magnitude of the complex number. So simple ab(x) on each of those complex numbers should return the frequency.


1 Answers

This will give you the mean frequency over given signal:

def spectral_statistics(y: np.ndarray, fs: int) -> float:
    """
    Compute mean frequency

    :param y: 1-d signal
    :param fs: sampling frequency [Hz]
    :return: mean frequency
    """
    spec = np.abs(np.fft.rfft(y))
    freq = np.fft.rfftfreq(len(y), d=1/fs)    
    amp = spec / spec.sum()
    mean = (freq * amp).sum()
    return mean 

As you have said, you can read the wave with scipy. If you'd like to get frequencies for chunks of audio, simply split your array into pieces and pass each separately to the function.

like image 52
Lukasz Tracewski Avatar answered Oct 17 '22 03:10

Lukasz Tracewski