What's wrong with this Whittaker-Shannon-Kotel’nikov interpolation implementation? I tried to implement Whittaker-Shannon-Kotel’nikov interpolation formula but I get unexpected results: the reconstructed signal lags with respect to the original.



I know that I can not expect a perfect reconstruction because I am sampling a finite time signal (just one cycle of a sine wave) and this finite time signal is not bandwidth limited and so the Whittaker-Shannon-Kotel’nikov theorem does not hold... but I do not know whether to expect a lag.

I suspect there is something wrong in my implementation (for example in a previous implementation I was erroneously using $\frac{\sin x}{x}$ instead of $\frac{\sin \pi x}{\pi x}$) but I can not find it.

f=1;
T=1/f;
mysignal = @(t) sin(2*pi*f*t);

f_s=5*f; % sampling rate
T_s=1/f_s; % sampling period
t=0:T_s:T; % times at which mysignal is sampled 
t1=0:.05*T_s:1.5*T; % times to plot mysignal "continuosly"

% plot mysgnal as a "continuos" signal
hold off;
plot(t1,mysignal(t1),'r')
hold on;

function y = Whittaker_Shannon_interpolation(samples,T_s,t)
% samples are the samples of the original signal;
% T_s is sampling period;
% t are the times at which interpolate the original signal;
% y are the interpolated values (reconstructed) of the original signal at times t.
  y = zeros(1,length(t));
  normalized_sinc = @(x) sin(pi*x)./(pi*x);
  for k = 1:1:length(samples)
    % https://en.wikipedia.org/wiki/Whittaker%E2%80%93Shannon_interpolation_formula
    % the above formula uses so called "normalized sinc", i.e. sin(pi*x)/(pi*x)
    y+=samples(k)*normalized_sinc((t-k*T_s)/T_s);
  endfor
endfunction

samples=mysignal(t);
stem(t,samples,'b')
hold on;

y=Whittaker_Shannon_interpolation(samples,T_s,t1);
plot(t1,y,'k-o')

legend('original signal','samples','reconstructed')
Replace the line

y+=samples(k)*normalized_sinc((t-k*T_s)/T_s);

by

y+=samples(k)*normalized_sinc((t-(k-1)*T_s)/T_s);

because the indices inside the sinc function must start from $0$, not from $1$. The first sinc must have its peak at $t=0$, not at $t=T_s$.