%% Fourier Interpolation
% Interpolate 1-D data using the FFT method and visualize the result.
% Generate some sample points in the interval [0, 3 \pi] for the function f(x) = \sin^2(x)\cos(x). Use a spacing interval dx to ensure the data is evenly spaced. Plot the sample points.

dx = 3*pi/30;
x = 0:dx:3*pi;
f = sin(x).^2 .* cos(x);
plot(x,f,'o')

%
% Use FFT interpolation to find the function value at 200 query points.

N = 200;
y = interpft(f,N);

%
% Calculate the spacing of the interpolated data from the spacing of the sample points with dy = dx*length(x)/N, where N is the number of interpolation points. Truncate the data in y to match the sampling density of x2.

dy = dx*length(x)/N;
x2 = 0:dy:3*pi;
y = y(1:length(x2));

%
% Plot the results.

hold on
plot(x2,y,'.')
title('FFT Interpolation of Periodic Function')

% Copyright 2012 The MathWorks, Inc.