Guest User

Numpy: calculate Power Spectral Density for two test files

a guest
Apr 28th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. #!/usr/bin/python
  2. import numpy as np
  3. import scipy.io.wavfile
  4. import scipy.signal
  5. import matplotlib.pyplot as plt
  6.  
  7. pwr_to_dB_fct = 10 / np.log(10)
  8. pwr_to_dB = lambda x: np.log(x)*pwr_to_dB_fct
  9.  
  10.  
  11. fig = plt.figure()
  12. ax = fig.add_subplot(1,1,1)
  13. ax.set_title('Difference 48kHz - 96kHz')
  14. ax.set_xlabel('Frequency [Hz]')
  15. ax.set_ylabel('Power Density [1/Hz]')
  16. ax.set_xlim(400, 40e3)
  17. ax.grid()
  18.  
  19. for fn in ['dynamic-test-ospac.wav', 'dynamic-highpass.wav'] :
  20.     fS, wave = scipy.io.wavfile.read(fn)
  21.     print('%s: %d samples at %f Hz'%(fn, wave.shape[0], fS))
  22.     freq, Plin = scipy.signal.welch(wave, fS)
  23.     PdB = pwr_to_dB(Plin)
  24.  
  25.     # "highpass" file is artificially amplified, we correct by
  26.     # substracting the difference between the highest two frequencies
  27.     # in test-ospac and highpass
  28.     if fn == 'dynamic-highpass.wav' :
  29.         PdB -= 46.43541
  30.  
  31.     print(freq[-5:], PdB[-5])
  32.     ax.semilogx(freq, PdB, label=fn[:-4])
  33.  
  34. ax.legend()
  35. fig.savefig('plot.png')
Advertisement
Add Comment
Please, Sign In to add comment