Humboldt-Universität zu Berlin - Faculty of Mathematics and Natural Sciences - Strukturforschung / Elektronenmikroskopie

test_FFTbandwidth.txt

function test_FFTbandwidth(flag)
% Test GUI for playing with FFTs:
% This program is intended to illustrate the effect that the number of
% Fourier coefficients has on the representation of a real-space function.
% Please do not pass any parameter when calling this program.

% Global variables:
% global x k f fr

% Variables that should be kept in memory from call to call:
persistent lst compCount;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Constants, selected to make the display look best:
NcellMax = 1; % number of unit cells used for simulation
pixPerCell = 65; % pixels used to sample the potential in 1 unit cell.
width = 0.4; % relative width used for Gaussian and aperture function

if(nargin > 0)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Obtain user input values:
func = get(lst,'Value');
% 1D lattice parameter of crystal:
acell = 1;
Ncomp = str2double(get(compCount,'String'));
if Ncomp < 0
Ncomp = 0;
end
% number of unit cells in specimen
Ncell = 1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define real- and reciprocal space arrays
N = NcellMax*pixPerCell; % total number of sampling points
dx = acell/pixPerCell; % x-sampling
x = -NcellMax*acell/2+dx*[0:N-1]; % x-position for whole crystal
xCell = dx*[0:pixPerCell-1]; % x-position within single unit cell
cellCenter = 0.5*(max(xCell)-min(xCell)); % center of unit cell
dk = (max(x)-min(x))^(-1);
k = dk*(-floor(N/2)+[0:N-1]);
switch func
case 1 % Gaussian
fcell = exp(-((xCell-cellCenter)/(width*cellCenter)).^2);
case 2 % Top hat aperture
fcell = zeros(size(xCell));
fcell(find(abs(xCell-cellCenter)<(width*cellCenter))) = 1;
case 3 % Sine function
fcell = 0.5*(1+sin(2*pi*xCell/acell));
case 4 % Delta function
fcell = zeros(size(xCell));
fcell(round(pixPerCell/2)) = 1;
end
% Construct a crystal with the desired number of unit cells:
f = zeros(1,N);
for j=1:Ncell
f((j-1)*pixPerCell+[1:pixPerCell]) = fcell;
end
% shift center of gravity to center of x-scale, so that it looks
% prettier:
%if sum(f) > 0
% xCenter = sum(f.*x)./sum(f);
% f = f(1+mod(round(xCenter/dx)+[0:N-1],N));
% end
% For allowing easier comparison of computed FFTs we will normalize the
% FFT:
fr = fftshift(fft(ifftshift(f/sum(f))));
% reduce the resolution:
fr(find(abs(k)/dk >= Ncomp)) = 0;
f = sum(f)*real(fftshift(ifft(ifftshift(fr))));
% Obtain the modulus of the Fourier transform
frMod = abs(fr);
% produce the array of Fourier components:
frComp = zeros(N,Ncomp);
for j=1:N
frComp(:,j) = real(fr(j)*exp(2*i*pi*k(j)*x.'));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot the data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The function itself:
subplot(2,2,2);
plot(x,f); axis tight;
if max(f)-min(f) > 0.1, ylim([1.2*min(f) 1.2*max(f)]); end
title(sprintf('f(x)')); xlabel('x in nm');
% Modulus of the FFT
subplot(2,2,3);
plot(k,frMod); axis tight; % xlim([-10 10])
if max(frMod) > 0.1, ylim([0 1.2*max(frMod)]); end
title(sprintf('|FFT(f)|')); xlabel('k in 1/nm');
% Components of the FFT
subplot(2,2,4);
plot(x,frComp); axis tight;
if max(max(frComp))-min(min(frComp)) > 0.1, ylim([1.2*min(min(frComp)) 1.2*max(max(frComp))]); end
title('FT components'); xlabel('x in nm');
else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Setup the window and graphical user interface controls,
% if this function has been called with no input.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Do the GUI setup
figure('Color',[0.8 0.8 0.8],'Name','FFT bandwidth limiting test GUI')
funcs = {'Gaussian';'Top Hat Aperture';'Sine function';'Delta function'};
% Listbox with function names
lst = uicontrol('Units','normalized', ...
'Position',[.3, .74,.19,.13],...
'String',funcs,'Style','listbox','Callback','test_FFTbandwidth(1)');
% Edit control for number of FT components
compCount = uicontrol('Units','normalized', ...
'Position',[.3, .7,.19,.03],...
'String',ceil(pixPerCell/2),'Style','edit','Callback','test_FFTbandwidth(1)');
% Text for function selection
lstT = uicontrol('Units','normalized', ...
'Position',[.1, .84,.19,.03],...
'String','function:','Style','text','Backgroundcolor',[0.8 0.8 0.8],...
'HorizontalAlignment','left');
% Text for component count selection
compCountT = uicontrol('Units','normalized', ...
'Position',[.1, .7,.19,.03],...
'String','Fourier components:','Style','text','Backgroundcolor',[0.8 0.8 0.8],...
'HorizontalAlignment','left');
% Call this program which is now initialized
test_FFTbandwidth(1);
end