


GAUSSKERNEL Creates a discretized N-dimensional Gaussian kernel. KERNEL = GAUSSKERNEL(R,S), for scalar R and S, returns a 1-D Gaussian array KERNEL with standard deviation S, discretized on [-R:R]. If R is a D-dimensional vector and S is scalar, KERNEL will be a D-dimensional isotropic Gaussian kernel with covariance matrix (S^2)*eye(D), discretized on a lattice with points [-R(k):R(k)] in the kth dimension. If R and S are both D-dimensional vectors, KERNEL will be a D-dimensional anisotropic Gaussian kernel on the lattice described above, but with standard deviation S(k) in the kth dimension. If R is scalar and S is a D-dimensional vector, R is treated as as R*ones(D,1). KERNEL is always normalized to sum to 1.


0001 function kernel = gausskernel(R,S) 0002 %GAUSSKERNEL Creates a discretized N-dimensional Gaussian kernel. 0003 % KERNEL = GAUSSKERNEL(R,S), for scalar R and S, returns a 1-D Gaussian 0004 % array KERNEL with standard deviation S, discretized on [-R:R]. 0005 % 0006 % If R is a D-dimensional vector and S is scalar, KERNEL will be a 0007 % D-dimensional isotropic Gaussian kernel with covariance matrix 0008 % (S^2)*eye(D), discretized on a lattice with points [-R(k):R(k)] in the 0009 % kth dimension. 0010 % 0011 % If R and S are both D-dimensional vectors, KERNEL will be a 0012 % D-dimensional anisotropic Gaussian kernel on the lattice described 0013 % above, but with standard deviation S(k) in the kth dimension. 0014 % 0015 % If R is scalar and S is a D-dimensional vector, R is treated as 0016 % as R*ones(D,1). 0017 % 0018 % KERNEL is always normalized to sum to 1. 0019 0020 %%%%%%%%%%%%%%%%%%%%%%%%%%%% Check Inputs %%%%%%%%%%%%%%%%%%%%%%%%%%%% 0021 D = numel(R); 0022 D2 = numel(S); 0023 if (((D > 1) && (~isvectord(R))) || ((D2> 1) && (~isvectord(S)))), 0024 error('Matrix arguments are not supported.'); 0025 end 0026 if ((D>1)&&(D2>1)&&(D~=D2)), 0027 error('R and S must have same number of elements (unless one is scalar).'); 0028 end; 0029 0030 if (D2>D), D = D2; R = R * ones(1,D); end; % force bins/sigmas 0031 if (D>D2), S = S * ones(1,D); end; % to be same length, 0032 R = R(:)'; S = S(:)'; % and force row vectors 0033 0034 S(S==0) = 1e-5; % std==0 causes problems below, 1e-5 has same effect 0035 0036 %%%%%%%%%%%%%%%%%%%%%%%%%%% Make the Kernel %%%%%%%%%%%%%%%%%%%%%%%%%% 0037 RR = 2*R + 1; 0038 for k = 1:D 0039 % Make the appropriate 1-D Gaussian 0040 grid = (-R(k):R(k))'; 0041 gauss = exp(-grid.^2./(2*S(k).^2)); 0042 gauss = gauss ./ sum(gauss); 0043 0044 % Then expand it against kernel-so-far ... 0045 if (k == 1), 0046 kernel = gauss; 0047 else 0048 Dpast = ones(1,(k-1)); 0049 expand = repmat(reshape(gauss, [Dpast RR(k)]), [RR(1:k-1) 1]); 0050 kernel = repmat(kernel, [Dpast RR(k)]) .* expand; 0051 end 0052 end 0053