% ============================================================
% INTERPOLACIONES LINEAL Y SINC
% ============================================================

clear all
clear all
close all

x=[1 2 1];
%x=load('../datos/wavelet'); L=5

L=10    % Numero de muestras  a interpolar: (L-1) puntos nuevos entre
       % las muestras originales.

N=length(x);


% Nota: Las sumatorias  con rango n=0:L*N-1 extrapola L  muestras a la
% derecha. Aqui  se decide dar  por resultado vectores que  empiezan y
% terminan    en   las    muestras   originales,    es   decir,    sin
% extrapolacion. Por ello la sumatoria tiene rango: n=0:L*N-L.


    % Interpolacion lineal:

for n=0:L*N-L   % n=0:L*N-L para no extrapolar a la derecha
    xil(n+1)=0.;
    for k=0:N-1
        if (abs(n-k*L)<=L)
            xil(n+1)=xil(n+1)+x(k+1)*(1-abs(k-n/L));
        end 
    end
end

    % Interpolacion sinc:

    for n=0:L*N-L   % n=0:L*N-L para no extrapolar a la derecha
        xis(n+1)=0.;
        for k=0:N-1
            %          if (k*L==n)
            %    xi(n+1)=x(k+1); break
            %else
                   xis(n+1)=xis(n+1)+x(k+1)*sinc(k-n/L);
               % xi(n+1)=xi(n+1)+x(k+1)*sin(pi*(k-n/L))/(pi*(k-n/L));
               %end 
        end
    end

    % verificacion con funciones externas    
    %  xiis  = sinc_interp(x,L-1);
    % xiil = lin_interp(x,L-1);


% ============================================================
% GRAFICOS
% ============================================================

% Vector de muestras con nuevo muestreo:

xe(L*N-L)=0.;
xe(1:L:L*N)=x;

xx=(1:(N*L-L+1))/L; %  "indices" de valores interpolados

% ------------------
% Interpolacion lineal
% ------------------

figure(1)
subplot(1,2,1)
%stem(x,'color','b',"markersize",7,"markeredgecolor",'b','filled','b')
stem(x,'color','b',"markersize",7,'filled','b')
axis([0 N+1 -1.2*min(x) 1.2*max(x)])
title('Original')
subplot(1,2,2)
hold on
%stem(xx,xil,'color','r',"markersize",8,"markeredgecolor",'r','filled','r')
stem(xx,xil,'color','r',"markersize",8,'filled','r')
%stem(xx,xiil,'b') verificacion con funcion externa
%stem(xx,xe,'color','b',"markersize",7,"markeredgecolor",'b','filled','b')
stem(xx,xe,'color','b',"markersize",7,'filled','b')
axis([-0.5 xx(end)+0.5 -1.2*min(x) 1.2*max(x)])
title('Interpolado linealmente')
hold off

% ------------------
% Interpolacion sinc
% ------------------


figure(2)
subplot(1,2,1)
%stem(x,'color','b',"markersize",7,"markeredgecolor",'b','filled','b')
stem(x,'color','b',"markersize",7,'filled','b')
axis([0 N+1 -1.2*min(x) 1.2*max(x)])
title('Original')
subplot(1,2,2)
hold on
%stem(xx,xis,'color','r',"markersize",8,"markeredgecolor",'r','filled','r')
stem(xx,xis,'color','r',"markersize",8,'filled','r')
% stem(xx,xiis,'b') % verificacion con funcion externa
%stem(xx,xe,'color','b',"markersize",7,"markeredgecolor",'b','filled','b')
stem(xx,xe,'color','b',"markersize",7,'filled','b')
axis([-0.5 xx(end)+0.5 -1.2*min(x) 1.2*max(x)])
title('Interpolado sinc')
hold off





