Code covered by the BSD License  

Highlights from
FFT accelerated surface analysis tools package

image thumbnail
from FFT accelerated surface analysis tools package by Andreas Almqvist
FFT accelerated functions for analysing 1D and 2D signals such surface profiles, surfaces and images

myxcorr2(a,b)
function c = myxcorr2(a,b)
%MYXCORR2 Two-dimensional FFT accelerated periodic cross-correlation of 
%   the matrices A and B representing periodic functions.
%   MYXCORR2(A,B) computes the periodic crosscorrelation of matrices A and
%   B.
%   MYXCORR2(A) is the periodic autocorrelation function.
%
%   See also CONV, CONV2, XCORR, MYCONV, MYCONV2, MYXCORR
%
%   clear all;
% 
%   % Create a "measurement" domain
%   Lx = 1e-3;            Ly = 1e-3;
%   Nx = 2^7;             Ny = 2^7;
%   x  = Lx*(0:Nx-1)/Nx;  y  = Ly*(0:Ny-1)/Ny;
%   dx = x(2)-x(1);       dy = y(2)-y(1);
%   [X,Y] = meshgrid(x,y);
% 
%   % "Sample" the (periodic) signal
%   z  = cos(2*pi*2*X/Lx).*cos(2*pi*1*Y/Ly)+...
%     0.5*cos(2*pi*5*(X-0.14*Lx)/Lx).*cos(2*pi*7*(Y-0.02)/Ly)+...
%     0.25*cos(2*pi*16*(X-0.3*Lx)/Lx).*cos(2*pi*10*(Y+0.32)/Ly)+...
%     0.25*cos(2*pi*18*(X-0.7*Lx)/Lx).*cos(2*pi*22*(Y-0.02)/Ly);
% 
%   % z  = [zo(:,end-Nx/2+1:end),zo,zo(:,1:Nx/2)]; % Periodic ext. removing edge-effects
%   % z  = [z(end-Ny/2+1:end,:);z;z(1:Ny/2,:)];
%   [Nzy,Nzx] = size(z);
% 
%   % Filter weighting function
%   lcx = Lx/2;  % Cut-off acc to ISO 11562 Gaussian Profile Filter Lx/5
%   lcy = Ly/2;
%   Xf  = [X,X(:,end)+dx]-Lx/2; % Symmetric and ensure that Nx+Nh-1 = 2^k
%   Xf  = [Xf;Xf(1,:)];
% 
%   Yf = [Y;Y(end,:)+dy]-Ly/2;
%   Yf = [Yf,Yf(:,1)];
% 
%   variancex = 1/(pi*sqrt(2/log(2)))*lcx;
%   variancey = 1/(pi*sqrt(2/log(2)))*lcy;
%   hx = dx/(sqrt(2*pi)*variancex)*...
%     exp(-((Xf/variancex).^2)/2);
%   hy = dy/(sqrt(2*pi)*variancey)*...
%     exp(-((Yf/variancey).^2)/2);
%   [Nhy,Nhx]  = size(hx); %=size(hy)
% 
%   % Total length of the padded arrays needed for the fft operations
%   N = Nzx+Nhx-1;
%   M = Nzy+Nhy-1;
% 
%   % Cross correlation - built in function
%   tic;
%   xc = xcorr2(z,z);
%   toc;
% 
%   % Cross correlation - A. Almqvists fft accelerated version
%   tic;
%   xf = myxcorr2(z,z);
%   toc;
% 
%   % Plotting the results
%   plot(...
%     1:N-1, xc(1,:)    , 'k.' ,...Filtered - spatial domain
%     1:N-1, xf(1,:)    , 'r--'...Filtered - freq. domain
%     );

%   Author(s): A. Almqvist
%   Copyright 2009- Andreas Almqvist
%   $Revision: 1$  $Date: 2009/11$
%   Homemade function

if nargin == 1
	b = a;
end
c = myconv2(a,rot90(conj(b),2));

Contact us at files@mathworks.com