1

I need to obtain an FFT spectrum and Power spectrum in dB for a .wav file with 2s of data. I need to obtain an "averaged power in dB" for the complete time period. I use the following code :

# Read 2s of data from wav file
y, sr = librosa.load('Noise Recorder.wav',sr=None,duration=2) 

# Short FFT, no overlap, nfft=8192, Hanning window
S = librosa.stft(y=y,n_fft=8192,window='hann')   
  
# Power in dB
power = librosa.power_to_db(np.abs(S)**2)

# Frequency components
freqs = librosa.fft_frequencies(sr=sr, n_fft=n_fft)

Here i see other formulas to calculate power. For ex: D = librosa.amplitude_to_db(np.abs(S), ref=np.max) # Convert to DB and in some cases energy = np.mean(mag**2, axis=1)/(n_fft/2)**2 Which formula should i use to obtain power spectrum for my window averaged over the entire 2s?

Also, I would like to 1) visualize Frequency vs dB (averaged for the entire 2s in a plot) and 2)average of the power spectrum(RMS/Leq) over the entire 2s. Please let me know how to proceed further.

Thanks

Shan
  • 11
  • 5
  • That STFT is *not* non-overlapping. `hop_length` defaults to `win_length // 4` and `win_length` should be `8192 ` in your case. – Hendrik Apr 19 '21 at 18:33
  • It would probably help, if you clarified in your question what you need that db power spectrum for. – Hendrik Apr 19 '21 at 18:34
  • Thanks. I need the dB Power spectrum to calculate a single value power average. This would be something like a single value RMS over a specified time(2s)/ Leq(2s) in Time domain. – Shan Apr 20 '21 at 09:34
  • In my case, where I do not need an overlap, If I set the hop_length = 0, I receive errors. I need the window continuously on samples with no overlap. – Shan Apr 20 '21 at 09:37
  • To get no overlap, the `hop_length` must be equal to `win_length` (or `n_fft`). – Hendrik Apr 20 '21 at 11:53
  • Thanks @Hendrik. Is the ```np.abs(s)``` here normalized for windows? Because with fft from numpy, we need to scale by window and half the spectrum ```s_mag = np.abs(sp) * 2 / np.sum(win)```. Don't we need to do that magnitude from librosa.fft? – Shan Apr 22 '21 at 08:51
  • `np.abs(s)` is applied per frequency bin, per frame (just check the shape before and after normalization). By default, there is no normalization. Whether and how you want to normalize depends on what you want to do with the data in the end. – Hendrik Apr 22 '21 at 12:47

0 Answers0