% =================================================================
% Funcion de muestreo (comb) y su TDF.
% 
% En la  Figura 1 la funcion  de muestreo comb(t) es  truncada por una
% ventana rectangular. En  el dominio de la frecuencia  se observan la
% serie de spikes convolucionados con el sinc asociado a la ventana de
% observacion.
%
% En la Figura 2 se muestra el efecto en frecuencia de tomar un numero
% finito de muestras sobre una funcion sinusoidal.
%
% 
% =================================================================

clear all;close all


% ---------------------
% comb(t_n) discreto en tiempo,  y comb2(t) "teorico"
% ---------------------

N = 10
dt=0.1 % s
fs=1/dt
fN=fs/2
T=N*dt;

t=(0:dt:T);
Nt=length(t);
comb(1:Nt)=1;

tt=(0:dt:3*T);
Ntt=length(tt);
comb2(1:Ntt)=1;


% ---------------------
% TDF COMB(f) 
% ---------------------

k = Nt; NN=Nt*k; % zero-padding

COMB=fft(comb,NN); dw=2*pi/NN ;w=(0:NN-1)*dw;
df=fs/NN;f=(0:NN-1)*df;

COMB2(1:NN)=0;
ndw=floor((2*pi/dt)*k);
COMB2(1:ndw:NN)=1;

COMBB =[COMB COMB COMB COMB COMB COMB]; % repetir para graficar
COMB22=[COMB2 COMB2 COMB2 COMB2 COMB2 COMB2]; % repetir para graficar
NP=length(COMBB);
ff=(0:NP-1)*df;


 % EJEMPLO CON UNA FUNCION SINUSOIDAL

 % senal muestreada
 w0 = pi/4 ;  T  = 2*pi/w0;
 n = (0:1:6*T);  s = sin(w0*n);  Ns  = length(s);
 % senal "continua"
 nn = (0:0.25:8*T);  ss  = sin(w0*nn);  Nss  = length(ss);
 

 % Hago zero padding y calculo el espectro de la senal muestreada (S) y
 % de la senal continua (SS).

 Npad    = 1024;
 S       = abs(fft(s,Npad));
 wpad    = (0:2*pi/Npad:2*pi*(1-1/Npad));   
 
 SS(Npad)=0;
 for j=1:Npad
     if (wpad(j)==w0 || wpad(j)==2*pi-w0)
         SS(j)=1;
     end
 end



% ---------------------
% GRAFICOS
% ---------------------

clf;


figure(1);

subplot(3,1,1)
 hold on
  stem(tt,comb2,"color","g","marker",'^',"markersize",8)
  stem(tt(Nt:2*Nt-1),comb,"color","b","marker",'^',"markersize",8,"filled")
 hold off
  axis([tt(1) +tt(Ntt) 0 1.4])
  xlabel('t')  
  ylabel('p(t)')   
subplot(3,1,2)
 hold on
 stem(ff,COMB22,"color","g","markersize",3,"filled","linewidth",3)
 plot(ff,(1/N)*abs(COMBB),'r')
 hold off
 xlabel('f')  
 ylabel('|P(f)|')   
subplot(3,1,3)
 plot(w,(1/N)*abs(COMB),'r')
 xlabel('{/Symbol w}')  
 ylabel('|P({/Symbol w})|')   
 xt      = 0:pi/2:2*pi;
 xtlabel = ["0";"{/Symbol p}/2";"{/Symbol p}";"3/2{/Symbol p}";"{/Symbol 2p}"];
 set(gca,"xtick",xt,"xticklabel",xtlabel);
 axis([0,2*pi,0, max( (1/N)*abs(COMB))]);
 
 
 
 figure(2);
 
 
 subplot(2,1,1)
 hold on;
 plot(nn,ss,"g","linewidth",2)
 plot(n+T,s,"ob","markersize", 5)
 hold off
 xlabel('t')  
 ylabel('s(t)')  
 axis([ nn(1) +nn(Nss) -1.2 +1.2])
 
 subplot(2,1,2)
 hold on
 stem(wpad,SS,"color","g","markersize", 3,"filled","linewidth",5)
 plot(wpad,(2/Ns)*S,"r","linewidth",2)
 xlabel('{/Symbol w}')  
 ylabel('|S({/Symbol w})|')    
  xt      = 0:pi/2:2*pi;
 xtlabel = ["0";"{/Symbol p}/2";"{/Symbol p}";"3/2{/Symbol p}";"{/Symbol 2p}"];
 set(gca,"xtick",xt,"xticklabel",xtlabel);
 axis([0,2*pi,0, 1.1]);
 