


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) && (~isvector(R))) || ((D2> 1) && (~isvector(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 %%%%%%%%%%%%%%%%%%%%%%%%%%% Make the Kernel %%%%%%%%%%%%%%%%%%%%%%%%%% 0035 RR = 2*R + 1; 0036 for k = 1:D 0037 % Make the appropriate 1-D Gaussian 0038 grid = [-R(k):R(k)]'; 0039 gauss = exp(-grid.^2./(2*S(k).^2)); 0040 gauss = gauss ./ sum(gauss); 0041 0042 % Then expand it against kernel-so-far ... 0043 if (k == 1), 0044 kernel = gauss; 0045 else 0046 Dpast = ones(1,(k-1)); 0047 expand = repmat(reshape(gauss, [Dpast RR(k)]), [RR(1:k-1) 1]); 0048 kernel = repmat(kernel, [Dpast RR(k)]) .* expand; 0049 end 0050 end 0051