Vm=1;
w0=120*pi;
T=2*pi/w0;
Narmonicos=10;
R=100;
L=100e-3;

a0=Vm/pi;
a1=Vm/2;
n=0:Narmonicos;
an=Vm*real((1+(-1).^n).*((-1).^(n/2)))./(1-n.^2)/pi;
an(1)=a0;
an(2)=a1;
bn=zeros(size(an));
cn=abs(an+j*bn);
phin=angle(an+j*bn); %atan2(bn,an);
figure(1)
stem(n,cn)
figure(2)
stem(n,phin)
t=linspace(0,2*T,100);
reconstruida=a0+a1*cos(120*pi*t);
figure(3);
plot(t,a1*cos(120*pi*t),t,reconstruida);
title('n=1');

iL=zeros(size(cn));
iL(1)=an(1)/R;
iL(2)=(an(2)+j*bn(2))/(R+j*w0*L);
figure(4);
reconstruidaL=iL(1)+abs(iL(2))*cos(120*pi*t-angle(iL(2)));
plot(t,abs(iL(2))*cos(120*pi*t-angle(iL(2))),t,reconstruidaL);
title('iL, n=1');
pause;

for indice=2:Narmonicos
    figure(3)
    armonico=an(indice+1)*cos(indice*120*pi*t);
    reconstruida=reconstruida+armonico;
    plot(t,armonico,t,reconstruida)
    title(sprintf('n=%d',indice));
    iL(indice+1)=(an(indice+1)+j*bn(indice+1))/(R+j*indice*w0*L);
    armonicoL=abs(iL(indice+1))*cos(indice*120*pi*t-angle(iL(indice+1)));
    reconstruidaL=reconstruidaL+armonicoL;
    figure(4)
    plot(t,armonicoL,t,reconstruidaL)
    title(sprintf('iL, n=%d',indice))
    pause(1);
end
figure(5);
stem(n,abs(iL));
figure(6);
stem(n,angle(iL));   
