% ---------------------------------
% fft_interpolation.m
% 
% interpolating points to data using FFT
% Vesa Apaja 2012
% ---------------------------------
clear all; close all;
% data points factor; will give L*N points from N input 
L=5;
% original data:
N=16;
x=linspace(0,1,N);
y=rand(1,N);
%y = cos(x).*exp(-x*10);
%y = [0 .5 1 1.5 2 1.5 1 .5 0 -.5 -1 -1.5 -2 -1.5 -1 -.5  ] ;
N = length(y)
x = 0:L:L*N-1;

% plot original data
plot(x,y,'bx','markersize',10); hold on;

M=L*N;
xlong = 0:M-1;
ylong1 = interpft(y,M);
plot(xlong,ylong1,'mo','markersize',5); hold on;

if(mod(N,2)~=0)
    disp (['N is not power of 2']);
    break;
end

%
% how did interpft do it ?
% 
% FFT data
Fy=fft(y,N);
% zero padding
Fylong=[Fy(1:N/2) zeros(1,M-N) Fy(N/2+1:N)];
% builtin postpad(Fy,M,0) adds zeros to end until size is M, but
% this is not what we want

% inverse FFT back
ylong=M/N*ifft(Fylong,M);

plot(xlong,ylong,'g*','markersize',5);
legend('original','interpft','own fft interp')

