>     restart;

# Derive the Fourier Sine/Cosine Series Representation of periodic signal, express up to 9th harmonics.

#     f(t) = 5 ( u(t+2) - u(t-2) ), period of 8.

>    fourierREPT:=
         #func ----------------  any given function,     eg. t^2-6*t+2
         #trange:name=range ---  range of indept var,    eg. t=0..10
         #m:posint ------------  positive integer (>0),  eg. 6
proc( func, trange::  name=range, m:: posint )
local t,A,B,T,n,j,p,q,partsum;
A:=lhs(rhs(trange));
B:=rhs(rhs(trange));
T:=B-A;
 t:=2*Pi*lhs(trange)/T;
partsum:=1/T*evalf(int( func,trange));
for n from 1 to m do
partsum:=partsum
+2/T*evalf(int(func*sin(n*t),trange))*sin(n*t)
+2/T*evalf(int(func*cos(n*t),trange))*cos(n*t);
od;
end:
# ===================================


with(plots):
alias(u=Heaviside):
f:=5*(u(t+2)-u(t-2));
g:=fourierREPT(f,t=-4..4,9);

plot({f,g},t=-4..4,title='f_and_g');

Warning, the name changecoords has been redefined

f := 5*u(t+2)-5*u(t-2)

g := 2.500000000+3.183098860*cos(1/4*Pi*t)-1.061032954*cos(3/4*Pi*t)+.6366197722*cos(5/4*Pi*t)-.4547284088*cos(7/4*Pi*t)+.3536776512*cos(9/4*Pi*t)

[Maple Plot]

>