Code covered by the BSD License  

Highlights from
fast Fouriertransform

fast Fouriertransform

by

 

test input output

test_FFT(flag)
function test_FFT(flag)
% Test GUI for playing with FFTs:
% Please do not pass any parameter when calling this program.

% Variables that should be kept in memory from call to call:
persistent lst width offset x k; 

if(nargin > 0)
    % Obtain user selected values:
    func = get(lst,'Value')
    w    = str2double(get(width,'String'));
    x0   = str2double(get(offset,'String')); 
    % Produce the correct function, according to user input:
    switch func
        case 1 % Gaussian
            f = exp(-((x-x0)/w).^2);
        case 2 % Top hat aperture
            f = zeros(size(x));
            f(find(abs(x-x0)<w/2)) = 1;
    end
    % Obtain the modulus of the Fourier transform
    frMod = abs(ifftshift(fft(fftshift(f))));
    % Obtain the phase of the Fourier transform
    frPhase = angle(ifftshift(fft(fftshift(f))));

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Plot the data
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % The function itself:
    subplot(2,2,2);
    plot(x,f); axis tight; ylim([0 1.2*max(f)]); 
    fwhm = FWHM(x,f);
    title(sprintf('f(x) [FWHM=%.2f]',fwhm)); xlim([-10 10]); 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])
    fwhm = FWHM(k,frMod);
    title(sprintf('|FFT(f)| [FWHM=%.2f]',fwhm)); xlabel('k in 1/nm');
    % Phase of the FFT
    subplot(2,2,4);
    plot(k,frPhase); axis tight; ylim([-1.1*pi 1.1*pi]); xlim([-10 10])
    title('arg[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.
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Define real- and reciprocal space arrays
    dx = 0.001;
    x = -20:dx:20-dx;
    N = length(x);
    dk = 1/(max(x)-min(x));
    k = dk*[-N/2:N/2-1];
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Do the GUI setup  
    figure('Color',[0.8 0.8 0.8],'Name','FFT test GUI')
    funcs = {'Gaussian';'Top Hat Aperture'};
    % Listbox with function names
    lst = uicontrol('Units','normalized', ...
        'Position',[.3, .8,.19,.07],...
        'String',funcs,'Style','listbox','Callback','test_FFT(1)');
    % Edit control for function widths
    width = uicontrol('Units','normalized', ...
        'Position',[.3, .7,.19,.03],...
        'String','1','Style','edit','Callback','test_FFT(1)');
    % Edit control for function shift
    offset = uicontrol('Units','normalized', ...
        'Position',[.3, .65,.19,.03],...
        'String','0','Style','edit','Callback','test_FFT(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
    widthT = uicontrol('Units','normalized', ...
        'Position',[.1, .7,.19,.03],...
        'String','width:','Style','text','Backgroundcolor',[0.8 0.8 0.8],...
        'HorizontalAlignment','left');
    % Text for offset selection
    offsetT = uicontrol('Units','normalized', ...
        'Position',[.1, .65,.19,.03],...
        'String','x0:','Style','text','Backgroundcolor',[0.8 0.8 0.8],...
        'HorizontalAlignment','left');
    % Call this program which is now initialized
    test_FFT(1);
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A sinple function for determining the full width at half maximum
% Input: 
% x1: the array of x-values
% f1: the array of function values
% Output:
% fwhm: FWHM of f in the scale given by x
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function fwhm = FWHM(x1,f1)
f1s = f1-0.5*max(f1);
ind = find(f1s(1:end-1).*f1s(2:end) <=0);
fwhm = x1(ind(2))-x1(ind(1));

Contact us