Python Forum
Magnitude/phase Spectrum Fourier
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Magnitude/phase Spectrum Fourier
#1
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:

#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, phase
where 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  phase portrait - Nonlinear damped pendulum Giovanni_62 1 861 Sep-10-2023, 02:25 PM
Last Post: deanhystad
  Magnitude and Phase spectrum ramsese 0 4,383 Apr-15-2018, 10:18 PM
Last Post: ramsese
  Fast Fourier Transform muhsin 1 3,130 Oct-13-2017, 07:50 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020