Apr-11-2018, 03:52 AM
Hello, I'm trying to graph the magnitude and phase spectrum of the following function and defined as it follows:
x(t) = -10^6 * t + 0.5 for 0 <= t <= 5*10^-7
0 for any other case
I'm told the period T = 10^-6
So basically in my code what i'm trying to do is calculate the coef. of fourier ao, an and bn to later get the amplitud An = sqrt(an^2+bn^2) and plot An vs n(harmonics, in my case i'll do 30) and the same with the phase plot (still haven't started to try to do the phase plot, doing magnitude first).
The code I have until now is:
Any help is highly appreciated, maybe I'm having a problem with defining the excerise itself also so any comment is appreciated.
x(t) = -10^6 * t + 0.5 for 0 <= t <= 5*10^-7
0 for any other case
I'm told the period T = 10^-6
So basically in my code what i'm trying to do is calculate the coef. of fourier ao, an and bn to later get the amplitud An = sqrt(an^2+bn^2) and plot An vs n(harmonics, in my case i'll do 30) and the same with the phase plot (still haven't started to try to do the phase plot, doing magnitude first).
The code I have until now is:
#Import functions import matplotlib.pyplot as plotter from sympy import * from numpy import * import numpy as np import sympy as sp from sympy.abc import t, n, T, a, b, i, j #func1 = (-10**6) * t + 0.5 function to work #T = 0.00001 Period #a = -0.0000005 lower limit #b = 0.0000005 superior limit #n = 30 number of harmonics to use def calculateCoef(func, T, a, b, n): ao = ((1/T) * sp.integrate(func, (t, a, b))) print "Ao = " pprint(ao) an = ((1/T) * sp.integrate(func * sp.cos((n*pi*t)/T),(t,a,b))) print "An = " pprint(an) bn = together((1/T) * sp.integrate(func * sp.sin((n*pi*t)/T),(t,a,b))) print "Bn = " pprint(bn) ann = 0 bnn = 0 for i in range(1, n+1, 1): ann = ann + an.subs(n,i) for j in range(1, n+1, 1): bnn = ann + bn.subs(n,j) amplitude = sp.sqrt(ann**2 + bnn**2) fase = -sp.atan(bnn/ann) print "Amplitude = " pprint(amplitude) print "Phase = " pprint(phase) lista = arange(-n,n,1) y = array(lista) plotter.plot(y, amplitude) plotter.title('Magnitude Spectrum') plotter.xlabel('Amplitude') plotter.ylabel('harmonics') plotter.show() return ao, an, bn, amplitude, phasewhere at the end I call the function and give it the values I want to calculate. I'm getting a ton of errors only when on the part of plotting with plotter, but they are errors from other files from the library so I'm not understanding them.
Any help is highly appreciated, maybe I'm having a problem with defining the excerise itself also so any comment is appreciated.