Guest User

Untitled

a guest
Oct 22nd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import division
  4. import numpy as np
  5. import scipy.signal as sig
  6. import matplotlib.pyplot as plt
  7.  
  8. def dbfft(x, fs, win=None):
  9. N = len(x) # Length of input sequence
  10.  
  11. if win is None:
  12. win = np.ones(x.shape)
  13. if len(x) != len(win):
  14. raise ValueError('Signal and window must be of the same length')
  15. x = x * win
  16.  
  17. # Calculate real FFT and frequency vector
  18. sp = np.fft.rfft(x)
  19. freq = np.arange((N / 2) + 1) / (float(N) / fs)
  20.  
  21. # Scale the magnitude of FFT by window and factor of 2,
  22. # because we are using half of FFT spectrum.
  23. s_mag = np.abs(sp) * 2 / np.sum(win)
  24.  
  25. # Convert to dBFS
  26. ref = s_mag.max()
  27. s_dbfs = 20 * np.log10(s_mag/ref)
  28.  
  29. return freq, s_dbfs
  30.  
  31. if __name__ == "__main__":
  32. # Sweep Parameters
  33. f1 = 10
  34. f2 = 100
  35. T = 3
  36. fs = 1000
  37. t = np.arange(0, T*fs)/fs
  38. R = np.log(f2/f1)
  39.  
  40. # ESS generation
  41. x = np.sin((2*np.pi*f1*T/R)*(np.exp(t*R/T)-1))
  42. # Inverse filter
  43. k = np.exp(t*R/T)
  44. f = x[::-1]/k
  45. # Impulse response
  46. ir = sig.fftconvolve(x, f, mode='same')
  47.  
  48. # Get spectra of all signals
  49. freq, Xdb = dbfft(x, fs)
  50. freq, Fdb = dbfft(f, fs)
  51. freq, IRdb = dbfft(ir, fs)
  52.  
  53. plt.figure()
  54. plt.subplot(3,1,1)
  55. plt.grid()
  56. plt.plot(t, x)
  57. plt.title('ESS')
  58. plt.subplot(3,1,2)
  59. plt.grid()
  60. plt.plot(t, f)
  61. plt.title('Inverse filter')
  62. plt.subplot(3,1,3)
  63. plt.grid()
  64. plt.plot(t, ir)
  65. plt.title('Impulse response')
  66.  
  67. plt.figure()
  68. plt.grid()
  69. plt.semilogx(freq, Xdb, label='ESS')
  70. plt.semilogx(freq, Fdb, label='Inverse filter')
  71. plt.semilogx(freq, IRdb, label='IR')
  72. plt.title('Spectrum')
  73. plt.xlabel('Frequency [Hz]')
  74. plt.ylabel('Amplitude [dBFS]')
  75. plt.legend()
  76.  
  77. plt.show()
Add Comment
Please, Sign In to add comment