Espectro bilateral de un suma de senoides en Python.

import numpy as np
#from scipy import signal
from scipy.fftpack import fft,fftshift
import matplotlib.pyplot as plt

N = 1000 # number of sample points
Fs=500.
dt = 1. / Fs # sample spacing

frequency1 = 50.
frequency2 = 150.
npp=4
nN=N*npp
t = np.linspace(0.0, N*dt, nN)
s1 = 0.8*np.sin(2*np.pi * frequency1 * t)
s2 = 0.4* np.sin(2*np.pi * frequency2 * t)
y = s1 + s2

plt.figure(1)
plt.plot(t, s1)
plt.grid()
plt.title('Senoide de '+str(frequency1)+' Hz')
plt.ylabel('Amplitud')
plt.xlabel('Tiempo [s]')
plt.axis([0, 8/frequency2, -1.5, 1.5])

plt.figure(2)
plt.plot(t, s2)
plt.grid()
plt.title('Senoide de '+str(frequency2)+' Hz')
plt.ylabel('Amplitud')
plt.xlabel('Tiempo [s]')
plt.axis([0, 8/frequency2, -1.5, 1.5])


plt.figure(3)
plt.plot(t, s1+s2)
plt.grid()
plt.title('Suma de senoides de '+str(frequency1)+' Hz'+' y '+str(frequency2)+' Hz')
plt.ylabel('Amplitud')
plt.xlabel('Tiempo [s]')
plt.axis([0, 8/frequency2, -1.5, 1.5])
# FFT
yf = fft(y)
tf = npp*np.linspace( -1./(2.*dt), 1./(2.*dt), nN)
spectrum = 2./nN * np.abs(fftshift(yf))

#figure1 = plt.figure(4, (10, 5))
plt.figure(4)
plt.plot(tf, spectrum, '-')
plt.grid()
plt.title(u'Espectro de magnitud |X(j$\omega$)|')
plt.xlabel('Frequencia [Hz]')
plt.ylabel('Magnitud |X(j$\omega$)|')
#plt.axis([-200, 200, 0., 1.5])

No hay comentarios.:

Publicar un comentario