Power Spectrum Using Librosa Python

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

7
James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article