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! :)
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With