Sinc Interpolation without using predefined function

43 views (last 30 days)
I need to write a code that does linear interpolation. (without using interp1) My function has 3 inputs (n,x,n2). Vector n contains the sample points, and x contains the corresponding values, x(n). Vector n2 contains the coordinates of the query points. My code is this:
function y = sinc_interp(n,x,n2)
y = zeros(length(n2),1);
for i=1:length(n2)
y(i) = sum(x.*sinc(n2(i)- n));
end
end
I think the formula is ok (It is the Shannon formula). The problem is that for various input functions, it doesn't look good. For example:
a = 0.7;
N = 10;
n = 0:N;
x = n.*a.^n + 1;
n2 = linspace(0,10,1000);
figure(1)
plot(n2,n2.*a.^n2 + 1)
y3 = sinc_interp(n,x,n2);
figure(2)
plot(n2,y3)

Accepted Answer

Sam Chak
Sam Chak on 25 Oct 2023
Not an interpolation expert, but I believe your code didn't work correctly because the sampling frequency (at 1 Hz) is relatively low. Additionally, there appears to be a slight error in the implementation of the Whittaker–Shannon interpolation formula, as indicated on this Wikipedia page. Nevertheless, the code has been fixed, and you can adjust the sampling frequency to observe the interpolation effect.
a = 0.7; % parameter in continuous function
tFinal = 10;
Fs = 20; % sampling frequency
T = 1/Fs;
t = 0:0.01:tFinal; % for continuous function
ts = 0:T:tFinal; % for sinc-interpolated
x = @(t) t.*(a.^t) + 1; % the continuous function
xn = x(ts); % the discrete sequence for sinc-interpolated
% call Whittaker–Shannon interpolation
y = sinc_interp(t, T, ts, xn);
plot(t, x(t), 'linewidth', 4, 'Color', '#0e6ea1'),
hold on, grid on
plot(t, y, '.', 'MarkerSize', 3, 'Color', '#ed9c5a');
xlabel('t')
legend('x(t)', 'sinc')
%% Whittaker–Shannon interpolation Algorithm
function y = sinc_interp(t, T, ts, xn)
y = zeros(length(ts), 1);
for n = 1:length(ts)
y = y + xn(n)*sinc((t - (n-1)*T)/T);
end
end

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!