% BandLimitedDemo.m illustrates band-limited interpolation
% for 3 different (not very smooth) functions
% Based on Trefethen: Spectral Methods in Matlab

clear all
clc
close all
scrnsz = get(0, 'Screensize');
figure('Position', [5 10 scrnsz(3)-5 scrnsz(4)-60])

for h = [1 .1 .01]
    tmax = 5; clf
    t = -tmax:h:tmax;                     % computational grid
    tt = -tmax-h/20:h/10:tmax+h/20;       % plotting grid
    for plt = 1:3
        subplot(3,1,plt)
        switch plt
            case 1, y = (t==0);               % delta function
                title(sprintf('Band-limited interpolation of delta function for h=%f',h))
            case 2, y = (abs(t)<=3);          % square wave
            case 3, y = max(0,1-abs(t)/3);    % hat function
        end
        plot(t,y,'.','markersize',14), grid on
        p = zeros(size(tt));
        for k = 1:length(t),
            p = p + y(k)*sin(pi*(tt-t(k))/h)./(pi*(tt-t(k))/h);
        end
        line(tt,p), axis([-tmax tmax -.5 1.5])
        set(gca,'xtick',[]), set(gca,'ytick',[0 1])
        switch plt
            case 1, title(sprintf('Band-limited interpolation of delta function for h=%f',h))
            case 2, title(sprintf('Band-limited interpolation of square wave for h=%f',h))
            case 3, title(sprintf('Band-limited interpolation of hat function for h=%f',h))
        end
    end
    pause
end