What's the simplest way to convert an audio signal to a list of frequencies and volumes?

Viewed 38

I have a signal that is like:

input=[0,0.1,0.5,2,3,4,2,0,...]

And I need to convert it to a series of frequency lists and volume lists like this:

output=[...,[[50,100,325,950,4000,...],[0.1,0.9,4,2,0.3,...]],...]

I tried with signal.spectrogram(), but it seems to return a frequency average by time period, what is it not what I want. I need a explicit map of frequencies and levels by frequency over time.

1 Answers

Here is a way to do it if it helps anyone in the future, although some libraries probably do it altogether:

spectrogram = []
N_window = ...
shift = ...
SAMPLING_RATE = ...
frequencies = np.fft.fftfreq(N_window, d = 1/SAMPLING_RATE) # get frequencies list

spectrogram = []
for i in range((len(signal) - N_window)//shift): # run through all windows that fit in signal
    window = signal[i*shift : (i*shift) + N_window] # get window to compute DFT on
    amplitudes = np.abs(np.fft.fft(window)) # get amplitudes of complex values
    spectrogram.append([frequencies, amplitudes])

This will give you a spectrogram as a list of lists [frequencies, volumes]

Few remarks:

  • The frequencies are all the same if you take the same windows, so you actually only need the list of amplitudes and one frequencies list
  • The window size and shift are up to you, window size will set how much you are precise in frequency vs in time localization of your window, shift will change how much your spectrogram is smooth
  • SAMPLING_RATE depends of your recording (how many samples/s)
Related