Fourier Series Examples

Contents

Triangle wave (even function) cosine series

w = 0.5;
T = 1.5;
d = w/T;
M = 50;
f0 = 1/T;

t = linspace(-3,3,601);
c0 = d; % DC term
y = ones(size(t))*c0;
for k=1:M
    ck = d*sinc(k*d)^2;
    y = y + 2*ck*cos(2*pi*k*f0*t);
end
plot(t,y,'k','LineWidth',2);
xlabel('time');
ylabel('signal');

Convergence Test

t = linspace(-T/2,T/2,801);
yp = tri(t/w); % signal
y = ones(size(t))*c0;
mse = zeros(M,1);
for k=1:M
    ck = d*sinc(k*d)^2;
    y = y + 2*ck*cos(2*pi*k*f0*t);
    ydiff = y - yp;
    mse(k)=ydiff*ydiff'/length(t);
end
semilogy(1:M,mse);
xlabel('M');
ylabel('mean-square-error');

Triangle wave - complex exponentials

this should be equivalent to the cosine series above

t = linspace(-3,3,601);
y = zeros(size(t));
for k=-M:M
    ck = d*sinc(k*d)^2;
    y = y + ck*exp(j*2*pi*k*f0*t);
end
y = real(y); % eliminate small spurious imaginary component
plot(t,y,'LineWidth',2);
xlabel('time');
ylabel('signal');

Triangle wave (odd function) - sine series

t = linspace(-3,3,601);
y = zeros(size(t));
Ts = T/4; % time shift
d = 1/6;  % narrower triangle
for k=1:M
    ck = 2*d*sin(2*pi*k*f0*Ts)*sinc(k*d)^2;
    y = y + 2*ck*sin(2*pi*k*f0*t);
end
plot(t,y,'k','LineWidth',2);
axis([-3 3 -1.1 1.1]);
xlabel('time');
ylabel('signal');

Triangle wave (odd function) - complex exponentials

This should be equivalent to the sine series above

t = linspace(-3,3,601);
y = zeros(size(t));
for k=-M:M
    ck = -2*j*d*sin(2*pi*k*f0*Ts)*sinc(k*d)^2;
    y = y + ck*exp(j*2*pi*k*f0*t);
end
y = real(y); % eliminate small spurious imaginary component
plot(t,y,'LineWidth',2);
axis([-3 3 -1.1 1.1]);
xlabel('time');
ylabel('signal');

Triangle wave (no symmetry)

t = linspace(-3,3,601);
y = zeros(size(t));
for k=-M:M
    ck = d*exp(-j*2*pi*k*f0*Ts)*sinc(k*d)^2;
    y = y + ck*exp(j*2*pi*k*f0*t);
end
y = real(y); % eliminate small spurious imaginary component
plot(t,y,'LineWidth',2);
axis([-3 3 -1.1 1.1]);
xlabel('time');
ylabel('signal');

Mixed wave (really no symmetry)

t = linspace(-3,3,601);
y = zeros(size(t));
alpha = 15;
% left part is exp(-\alpha t) u(t) (flipped vertically)
% right part is tri(t/w);
% note use of time shift properties
for k=-M:M
    cleft = -exp(j*2*pi*k*f0*Ts)/(alpha+j*2*pi*k);
    cright = d*exp(-j*2*pi*k*f0*Ts)*sinc(k*d)^2;
    ck = cleft + cright;
    y = y + ck*exp(j*2*pi*k*f0*t);
end
y = real(y); % eliminate small spurious imaginary component
plot(t,y,'r','LineWidth',2);
axis([-3 3 -1.1 1.1]);
xlabel('time');
ylabel('signal');