close all; clear all;

t_start = -4; t_end = 4;
% For ease of analysis, use t_start and t_end that are multiples of Ts. 

%m = @(t) exp(-(t.^2)); Ts = 0.5; %Try Ts = 1 first
m = @(t) cos(2*pi*2*t); Ts = 0.4; % f_max = 2. Need Ts < 1/4 = 0.25. 
                                                %Try Ts = 0.4 first
%m = @(t) sin(2*pi*2*(t)); Ts = 0.4; % f_max = 2. Need Ts < 1/4 = 0.25. 
                                                %Try Ts = 0.4 first
%m = @(t) (2-abs(t)).*(abs(t)<=2); Ts = 0.5; % Triangular
%m = @(t) abs(t)<= 2; Ts = 0.5; % Rectangular
%m = @(t) t.^2; Ts = 0.5; % Parabola %Try Ts = 0.5 first
%m = @(t) (t.^2).*(abs(t)<=2); Ts = 0.01; % Parabola %Try Ts = 0.5 first
%m = @(t) (4-t.^2).*(abs(t)<=2); Ts = 0.1; % Parabola %Try Ts = 0.5 first
%m = @(t) cos(2*pi*2*t) + 3*cos(2*pi*3*t); Ts = 0.5;

% Ts = sampling interval
fs = 1/Ts; % sampling frequency

sampling_t = t_start:Ts:t_end;  % sampling instances
N = length(sampling_t);         

m_n = m(sampling_t);            % m[n]

if length(m_n) < 30 % Use thick circles only if there are few points
    plot(sampling_t,m_n,'ko','LineWidth',1.5)
else
    plot(sampling_t,m_n,'ko')
end
hold on

t = linspace(t_start,t_end,1000);
plot(t,m(t),'b--','LineWidth',1.5)
xlabel('t')

%% Reconstruction
s=0;
for k = 1:N
        y = m_n(k)*sinc(fs*(t-sampling_t(k)));
        if length(m_n) < 30 % Show the individual sinc functions if there 
                            % are not toom any of them.
            plot(t,y,'g');
        end
        s = s+y;
end 
plot(t,s,'r','LineWidth',1.5)
grid on
xlim([-4,4])



