Sympy extract Fourier Series coefficients

Viewed 3311

I am using sympy to carry on some symbolic math manipulations.

Start by creating a Fourier series representation of a rectangular pulse train (duty cycle < 50%), then try to access the multiplying factors, i.e. a_n and b_n of the standard Fourier series.

In a nutshell:

import sympy as sy
from sympy import fourier_series, pi, cos, sin
from sympy.abc import t
from sympy.functions.special.delta_functions import Heaviside
T = sy.symbols('T')
s = fourier_series(Heaviside(t) - Heaviside(t-1/4), (t, 0, 1))
s.truncate(3)

1/π*sin(2πt)+1/π*sin(4πt)+1/π*cos(2πt)+0.25

I would then like to access the coefficients of the base functions. To this extent, I thought I should use as_coefficient(expr).

This produces the expected results in a simpler case:

g = 1/(pi*T)*sin(2*pi*t)
g.as_coefficient(sin(2*pi*t))

1/πT

However, with the object returned by fourier_series, this does not seem to work:

a = s.truncate(3)
a.as_coefficient(sin(2*pi*t))

returns nothing (not even a warning or message).

Other methods like s.as_Add() or s.as_Mul() return both a full expression where the a_n is tied to its sin(2*pi*n*t) term (or b_n to its dual).

2 Answers

The class sympy.series.fourier.FourierSeries has methods to provide sympy.series.sequences.sequence objects with the cosine and sine terms of the series: a0, an and bn.

After computing the series by

import sympy as sym
from sympy import fourier_series
from sympy.abc import t
from sympy.functions.special.delta_functions import Heaviside
s = fourier_series(Heaviside(t) - Heaviside(t-1/4), (t, 0, 1))

The cosine coefficients can be obtained by

s.a0
0.25

and

s.an

a_n cosine coefficients.

For the sine coefficients

s.bn

b_n sine coefficients.

So in order to produce lists of the s series coefficients up to a given order, let say 4 that can be done with

def cosine_fourier_coeffs(fourierSeries, order):
    ### returns a list of fourier series cosine coefficients up to order
    out = []
    out.append(fourierSeries.a0)
    for i in range(1,order):
        out.append(fourierSeries.an.coeff(i).subs(t, 0 ) )
    return out

def sine_fourier_coeffs(fourierSeries, order):
    ### returns a list of fourier series sine coefficients up to order
    out = []
    for i in range(1,order):
        out.append(fourierSeries.bn.coeff(i).subs(t, 1/(4* i) ) )
    return out

cosine_fourier_coeffs(s, 4), sine_fourier_coeffs(s, 4)

that will return

([0.250000000000000, 1/pi, 0, -1/(3*pi)], [1/pi, 1/pi, 1/(3*pi)])
Related