# 2010-10-14

clear all
clc
close all

part1 = zeros(500,1);

part2 = ones(500,1);

squared = zeros( length(part1) + length(part2) );

squared(1:length(part1)) = part1;

squared(length(part1)+1:length(part1)+length(part2)) = part2;

inter = 1 / length(squared-1);

t = 0:inter:inter*length(squared-1);

T0 = max(t) - min(t);

w0 = 2*pi/T0;

cn = fft( squared ) / length( squared );

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

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

serie =  serie + cn(1);

#plot(t, serie, 'b')

n = 1;
R = 100; C = 0.0001;
serief = 0;
while n <= 50;
  H = (i * R * C * 2 * pi * n * w0 + R^2 + C^2 * (2*pi*n*w0)^2 )/ 1 + R^2 * C^2 * (2*pi*n*w0)^2;
  serief = serief + an(n+1) * abs(H) * cos(n*w0*t-angle(H)) + bn(n+1)*abs(H)*sin(n*w0*t-angle(H));
  n = n+1;
end

serief = serief + cn(1);




figure(1, "visible", "off");
plot(serief)
grid;
print -deps "../media/filtro_misterio.eps";
