%
% BoxCar2        Mitch Withers 20070404
% Example matlab script to demonstrate 2d FT on a boxcar.
%
% close any old figures and clear any old variables
clear all
close all

% number of points in matrix
N=2^9;

% set matrix to zero
x=zeros(N,N);

% boxcar halfwidth
dx=2^2;

% set the center of the box car equal to one
% this will have an odd number of points in each dimension
x(N/2-dx:N/2+dx,N/2-dx:N/2+dx)=1;

% normalize by the number of ones to make the sinc max height 1
x=x/sum(sum(x));

% get the 2d fft and shift it to center the response
y=fftshift(fft2(x));

% get a normalized vector for the frequency (and space (or time) in this case)
% axes
%x1=0:1/N:1-1/N;
x1=-0.5:1/N:0.5-1/N;

disp(' ')
disp('Plotting the space (or time domain) 2-d Boxcar function')
% plot the space (or time) domain in 3-d
mesh(x1,x1,x)
xlabel('normalized distance')
ylabel('normalized distance')
zlabel('amplitude')
orient tall
%print -depsc BoxCar2length.ps
pause

disp(' ')
disp('Plotting the 2-D FFT of the boxcar function calculated using the fft2 command')
disp('note: fftshift was also used to transform spectrum from 0-1 to +-0.5')
figure
% plot the frequency domain in 3-d
% use x1,x1 for the axes again knowing their normalized so that there's no
% difference between the space and frequency vectors.
mesh(x1,x1,abs(y))
xlabel('normalized frequency')
ylabel('normalized frequency')
zlabel('amplitude')
orient tall
%print -dpsc BoxCar2frequency.ps

