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

test_XtalFFT.txt

function test_XtalFFT(flag)
% Test GUI for playing with FFTs:
% 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 cellPar cellCount;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Constants, selected to make the display look best:
NcellMax = 16; % number of unit cells used for simulation
pixPerCell = 64; % 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 = str2double(get(cellPar,'String'));
if acell < 0.1, acell = 0.1; end
% number of unit cells in specimen
Ncell = str2double(get(cellCount,'String'));
if Ncell < 1, Ncell = 1; end
if Ncell > NcellMax, Ncell = NcellMax; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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
k = (max(x)-min(x))^(-1)*[-N/2:N/2-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 % Delta function
fcell = zeros(size(xCell));
fcell(round(pixPerCell/2)) = 1;
case 4 % saw tooth
fcell = abs(xCell-cellCenter);
fcell = max(fcell)-fcell;
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 divide the
% FFT by the number of unit cells:
% fr = ifftshift(fft(fftshift(f/Ncell)));
fr = ifftshift(fft(fftshift(f/sum(f))));
% Obtain the modulus of the Fourier transform
frMod = abs(fr);
% Obtain the phase of the Fourier transform
frReal = real(fr);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot the data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The function itself:
subplot(2,2,2);
plot(x,f); axis tight; ylim([0 1.2*max(f)]);
title(sprintf('f(x)')); xlabel('x in nm');
% Modulus of the FFT
subplot(2,2,3);
plot(k,frMod); axis tight; ylim([0 1.2*max(frMod)]); xlim([-10 10])
title(sprintf('|FFT(f)|')); xlabel('k in 1/nm');
% Phase of the FFT
subplot(2,2,4);
plot(k,frReal); axis tight; xlim([-10 10])
title('Real[FFT(f)]'); xlabel('k in 1/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','1D crystal FFT test GUI')
funcs = {'Gaussian';'Top Hat Aperture'; 'Delta function'; 'saw tooth'};
% Listbox with function names
lst = uicontrol('Units','normalized', ...
'Position',[.3, .77,.19,.1],...
'String',funcs,'Style','listbox','Callback','test_XtalFFT(1)');
% Edit control for function widths
cellPar = uicontrol('Units','normalized', ...
'Position',[.3, .7,.19,.03],...
'String','0.4','Style','edit','Callback','test_XtalFFT(1)');
% Edit control for function shift
cellCount = uicontrol('Units','normalized', ...
'Position',[.3, .65,.19,.03],...
'String','1','Style','edit','Callback','test_XtalFFT(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 Width selection
cellParT = uicontrol('Units','normalized', ...
'Position',[.1, .7,.19,.03],...
'String','cell size (nm):','Style','text','Backgroundcolor',[0.8 0.8 0.8],...
'HorizontalAlignment','left');
% Text for offset selection
cellCountT = uicontrol('Units','normalized', ...
'Position',[.1, .65,.19,.03],...
'String','Nr. of unit cells:','Style','text','Backgroundcolor',[0.8 0.8 0.8],...
'HorizontalAlignment','left');
% Call this program which is now initialized
test_XtalFFT(1);
end