% ftfft script approximates Fourier transform
% of x(t) based on samples x(t1),...,x(t2),
% where samples are uniformly spaced by 1/fs.

% EXAMPLE:  x(t) = exp(-abs(t))

subplot(2,1,1)             % Plot waveform x(t)
t = linspace(-3,3,200);
plot(t,exp(-abs(t)))
title('\itx\rm(\itt\rm)=exp(-abs(\itt\rm))')
grid on

fs = 21; % Select sampling rate.  Since x(t) is NOT bandlimited, will be
         % some aliasing.  To minimize it, take fs "large enough."

t1 = -10;                % Sample x(t) for t1<=t<=t2 ...
t2 = 10;
n1 = ceil(fs*t1);        % ... using time values of the form n/fs
n2 = floor(fs*t2);
nvec = [n1:n2];
t = nvec/fs;             % Note that we are redefining t

x = exp(-abs(t));        % Compute function values

[y,f] = dtftfft(x,n1,0); % Compute DTFT on [-1/2,1/2]
f = f*fs;                % Convert freq. to [-fs/2,fs/2]
y = y/fs;                % Scale DTFT

subplot(2,1,2)
k = find(abs(f)<=1);     % Isolate part of spectrum of interest
ff = f(k);
yy = y(k);
plot(ff,abs(yy),'o',ff,abs(2./(1+(2*pi*ff).^2)))
title('Approximate Fourier transform (o), exact transform (solid line)')
grid on


