Espectro bilateral de un suma de senoides en Python.

  1. import numpy as np
  2. #from scipy import signal
  3. from scipy.fftpack import fft,fftshift
  4. import matplotlib.pyplot as plt
  5.  
  6. N = 1000 # number of sample points
  7. Fs=500.
  8. dt = 1. / Fs # sample spacing
  9.  
  10. frequency1 = 50.
  11. frequency2 = 150.
  12. npp=4
  13. nN=N*npp
  14. t = np.linspace(0.0, N*dt, nN)
  15. s1 = 0.8*np.sin(2*np.pi * frequency1 * t)
  16. s2 = 0.4* np.sin(2*np.pi * frequency2 * t)
  17. y = s1 + s2
  18.  
  19. plt.figure(1)
  20. plt.plot(t, s1)
  21. plt.grid()
  22. plt.title('Senoide de '+str(frequency1)+' Hz')
  23. plt.ylabel('Amplitud')
  24. plt.xlabel('Tiempo [s]')
  25. plt.axis([0, 8/frequency2, -1.5, 1.5])
  26.  
  27. plt.figure(2)
  28. plt.plot(t, s2)
  29. plt.grid()
  30. plt.title('Senoide de '+str(frequency2)+' Hz')
  31. plt.ylabel('Amplitud')
  32. plt.xlabel('Tiempo [s]')
  33. plt.axis([0, 8/frequency2, -1.5, 1.5])
  34.  
  35.  
  36. plt.figure(3)
  37. plt.plot(t, s1+s2)
  38. plt.grid()
  39. plt.title('Suma de senoides de '+str(frequency1)+' Hz'+' y '+str(frequency2)+' Hz')
  40. plt.ylabel('Amplitud')
  41. plt.xlabel('Tiempo [s]')
  42. plt.axis([0, 8/frequency2, -1.5, 1.5])
  43. # FFT
  44. yf = fft(y)
  45. tf = npp*np.linspace( -1./(2.*dt), 1./(2.*dt), nN)
  46. spectrum = 2./nN * np.abs(fftshift(yf))
  47.  
  48. #figure1 = plt.figure(4, (10, 5))
  49. plt.figure(4)
  50. plt.plot(tf, spectrum, '-')
  51. plt.grid()
  52. plt.title(u'Espectro de magnitud |X(j$\omega$)|')
  53. plt.xlabel('Frequencia [Hz]')
  54. plt.ylabel('Magnitud |X(j$\omega$)|')
  55. #plt.axis([-200, 200, 0., 1.5])

No hay comentarios.:

Publicar un comentario

Related Posts Plugin for WordPress, Blogger...