# 2010-10-01
# fourier

clear all
clc
close all

t = 0:0.0001:6*pi;

f = sin(t) + 0.1*sin(50*t) + 0.2*cos(10*t) + 0.4*sin(cos(t*4)*3) + rand;

#plot(t,f);

N = length(t);

cn = fft(f)/N;

an =  2 * real(cn);
bn = -2 * imag(cn);

serie = 0;
n     = 1;
# el numero del argumento n 100w0t
armonicas = 20;

# frecuencia fundamental, 2 pi entre intervalo de tiempo
w0 =  (2 * pi )/(6*pi) 

while n <=armonicas
  serie = serie + an(n+1)*cos(n*w0*t) + bn(n+1)*sin(n*w0*t);
  n += 1;
end


serie =  serie + cn(1);




figure(1, "visible", "off");

plot(t,f,'k',t,serie,'r');
grid;
print -deps "../media/fourier.eps";
