Python – Fourier series for Piecewise PYTHON

Fourier series for Piecewise PYTHON… here is a solution to the problem.

Fourier series for Piecewise PYTHON

I tried to find the Fourier series

piecewise

Like simpy:

p = Piecewise((sin(t), 0 < t),(sin(t), t < pi), (0 , pi < t), (0, t < 2*pi))
fs = fourier_series(p, (t, 0, 2*pi)).truncate(8)

But it doesn’t seem to work. It’s stuck in * (loop?) )。 Is there any way to fix it? Maybe another option? Thank you very much

Solution

I get, delay a second or two :

In [55]: fourier_series(p,(t,0,2*pi))
Out[55]: FourierSeries(Piecewise((sin(t), (t > 0) | (t < pi)), (0, (pi < t) | (t < 2*pi))), (t, 0, 2*pi), (0, SeqFormula(Piecewise((0, Eq(_n, -1) | Eq(_n, 1)), (cos(2*_n*pi)/(_n**2 - 1) - 1/(_n**2 - 1), True))*cos(_n*t)/pi, (_n, 1, oo)), SeqFormula(Piecewise((-pi, Eq(_n, -1)), (pi, Eq(_n, 1)), (sin(2*_n*pi)/(_n**2 - 1), True))*sin(_n* t)/pi, (_n, 1, oo))))

It’s just a setup.

_.truncate(8) takes (too) long. That must be an assessment.

Are different truncations better? I don’t see any other controls.


.truncate(1) returns sin(t). .truncate(2) hangs. Mixing this simple sin(t) with a flat segment must be building a difficult case that is difficult to analyze. But I’m a little rusty with this area of mathematics.

I use numpy to find Fourier series:

How to calculate a Fourier series in Numpy?


For FS fs1 defined on (0,pi) = fourier_series(p, (t, 0, pi)):

In [5]: fs1.truncate(1)
Out[5]: 2/pi
In [6]: fs1.truncate(2)
Out[6]: -4*cos(2*t)/(3*pi) + 2/pi
In [7]: fs1.truncate(3)
Out[7]: -4*cos(2*t)/(3*pi) - 4*cos(4*t)/(15*pi) + 2/pi
In [8]: fs1.truncate(4)
Out[8]: -4*cos(2*t)/(3*pi) - 4*cos(4*t)/(15*pi) - 4*cos(6*t)/(35*pi) + 2/pi
In [9]: fs1.truncate(5)
Out[9]: -4*cos(2*t)/(3*pi) - 4*cos(4*t)/(15*pi) - 4*cos(6*t)/(35*pi) - 4*cos(8*t)/(63*pi) + 2/pi

Which plot (in numpy) meets expectations:

periodic 0-pi FS

From the table Fourier Series, I found this formula (in numpy terminology) For rectifying sine waves:

z8 = 1/pi + 1/2*sin(t)-2/pi*np.sum([cos(2*i*t)/(4*i**2-1) for i in range(1,8)],axis=0)

rectified half sine wave

This has a similar COS family of terminology, but with the addition of the SIN term. This suggests to me that you can approximate this half of sin as the sum (or something similar) of a*sin(t)+b(sin(2*t)). I imagine that like Sympy, there are some math textbooks or papers that explore the difficulty of deriving Fourier series. Have you seen the Mathworld link?

FS for semi-sinusoidal rectification versus whole sinusoidal rectification

Half sinusoid:

In [434]: z3 = 1/pi + 1/2*sin(t)-2/pi*np.sum([cos(2*i*t)/(4*i**2-1) for i in range(1,3)],axis=0)

Full sine:

In [435]: w3 = 1/pi -2/pi*np.sum([cos(2*i*t)/(4*i**2-1) for i in range(1,3)],axis=0)

In [438]: plt.plot(t,sin(t)/2)
In [439]: plt.plot(t,w3)
In [440]: plt.plot(t,z3)
In [441]: plt.plot(t,w3+sin(t)/2)  # full sine + sine/2 == half sine

rectified half sine vs full sine

I can imagine transferring such insights back to sympy, redefining the periodic function in a way that doesn’t take that long (or possibly hangs).

Related Problems and Solutions