clf;
% Parameters that you can change as desired:
x=1:15;
pos=7;
wid=2;
n=6; % Number of Fourier components include in reconstruction
% Create single Gaussian peak
y=exp(-((x-pos)./(0.60056120439323.*wid)) .^2);
% COmouter the FT, truncate it, and inverse transform the result
ffty=fft(y); % ffty is the fft of y
lfft=length(ffty); % Length of the FFT
ffty(n:lfft-n)=0; % All frequencies above n are set to zero, in both halves of the Fourier transform
fy=real(ifft(ffty)); % Real part of the ifft
plot(x,y,'o',x,fy)
grid
title( ['Signal reconstructed from ' num2str(n) ' Fourier components'])
xlabel('x')
ylabel('y')
legend('Original signal','Reconstructed from Fourier components')