close all






  
if 1==1
  slen=1024;
  fft_pts=512;%342;%256;%slen/2;
  
  
  %reconstruction
  figure
  
  offset=20;  
  pd=20;
  if 1==0 
    %cosine
    x=cos([0:slen*2]*2*pi /pd);
  elseif 1==0
    %square
    x=square([0:slen*2]*2*pi/pd);
  
  else
    x=cos([0:slen*2]*2*pi /pd);
    x=x+cos([0:slen*2]*2*pi /(pd*3));
  end
  
  
  x=x(1+offset:offset+slen);  
  y=fft(x,fft_pts);
  

 
  subplot(511)
  z=[-fft_pts/2:fft_pts/2-1] / fft_pts;% / fft_pts;
  %z=1./z;
  plot(z,abs(fftshift(y)))
  %plot(abs(y))
  xlabel('freq (mag)');
  
  subplot(512)  
  plot(fq,fftshift(angle(y)));
  xlabel('phase');

  subplot(513)
  size(y)
  plot(ifft(y))
  xlabel('reconstruct')
  
  reconstruct=-ones(1,slen).* abs(y(1)) / length(y)/2;
  %reconstruct=zeros(1,slen);
  t=[0:length(x)-1];

  

  length(y)
  for i=2:length(y)/2
    frq= (i-1) / length(y);%(slen/fft_pts) / length(y);
    ang=angle(y(i));
    this_sinus=cos(t .* 2 * pi * frq  + ang);
   
    
    this_sinus=this_sinus .* abs(y(i)) / (length(y)/2);

    %abs(y(i))    
    %plot(this_sinus)
    %pause
    
    reconstruct=reconstruct + this_sinus;
    subplot(513)
    plot(this_sinus)
    hold on;
  end
  xlabel('component sinusoids')
 
  subplot(514)    
  plot(reconstruct);
  hold on
  plot(x,'r');
  xlabel('reconstructed signal using component sinusoids vs original')

  
  subplot(515)
  xx=ifft(y,length(y));
  plot(xx);
  hold on;
  plot(x,'r')
  plot(xx);
  xlabel('reconstructed signal using ifft vs original')
  
  
  
end


