6

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! :)

Eduardo Morales
  • 764
  • 7
  • 29
  • What do you mean by "I would like to go to the time domain"? Frequency over short time intervals? What frequency? Mean, median, mode, pitch, peaks...? All frequencies are indeed returned by FFT. – Lukasz Tracewski Feb 22 '19 at 21:58
  • Im guessing the most used represe of frequency vs time and amplitude vs time is the mean of the frequencies? – Eduardo Morales Feb 22 '19 at 22:34
  • I am glad it helped! Mind there are great many more features of a sound that you might want to calculate if you want to understand its characteristic. It goes beyond the scope of the question, but you can drop me an email or more SO audio questions :). – Lukasz Tracewski Feb 24 '19 at 08:27

1 Answers1

3

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.

Lukasz Tracewski
  • 10,794
  • 3
  • 34
  • 53
  • Thank you!! What are usually the chunk sizes most commonly used? :) – Eduardo Morales Feb 23 '19 at 00:49
  • 1
    There is no "size to rule them all". It can be as short as 20 ms or longer than 0.5s. All depends on your application, sampling frequency (the larger the finer resolution you might be able to use), characteristic of the signal etc. You might start with e.g. 100 and 200 ms. – Lukasz Tracewski Feb 23 '19 at 07:31