% spectral interpolant demonstration
clc; clear; close all;

n = 16;

h = 2*pi/n;
xs = h*(1:n);

dirac = [zeros(1,n-1),1];

n2 = 8*n; 
h2 = 2*pi/n2; 
xs2 = h2*(1:n2);

SN = (h/2/pi)*sin(pi*xs2/h)./tan(xs2/2);
%SN(mod(xs2,2*pi)==0) = 1.;

figure;
plot(xs,dirac,'ro',xs2,SN,'b'); legend('dirac','S_N')

% spectral interpolant of any given function
% f = (abs(xs-pi)<pi/2);
% f = exp(-100*(xs-pi).^2);
f = max(0,1-abs(xs-pi));

SNf = zeros(1,n2);
for l=1:n,
    temp = f(l)*(h/2/pi)*sin(pi*(xs2-xs(l))/h)./tan((xs2-xs(l))/2);
    temp(mod(xs2-xs(l),2*pi)==0) = f(l);
    SNf = SNf + temp;
end

figure;
plot(xs,f,'ro',xs2,SNf,'b'); legend('f','S_N f')
