MATLAB: Explanation of the matlab code

filter

Hi I foun this 'extract' of a matlab code on the internet. Can someone explain to me line by line whats happening? Im REALLY in need of help.
*
% ------------- % This is code to make the edge detecting filter % ----%
function filter=gaussfilt(N)
% calculate alpha so the filter fills all N points
alpha=N;
first=-(1-N/2)*exp(-(1-N/2)^2/alpha);
count=0;
while first<.1*(-(1530/4000*N-N/2)*exp(-(1530/4000*N-N/2)^2/alpha))
count=count+1;
alpha=N*500*count;
first=-(1-N/2)*exp(-(1-N/2)^2/alpha);
end
for n=1:N
filter(n)=-(n-N/2)*exp(-(n-N/2)^2/alpha); % d/dt of a gaussian
end
filter=filter/sum(abs(filter)); % normalization
return

Best Answer

Your code calculates a filter kernel. I haven't run the code, but, it seems to me a kind of Gaussian filter, with some normalizations.
It could be rewritten like,
function filter=gaussfilt(N)
% calculate alpha so the filter fills all N points
alpha=N;
first=-(1-N/2)*exp(-(1-N/2)^2/alpha);
count=0;
% find the standard deviation for the Gaussian
while first<.1*(-(1530/4000*N-N/2)*exp(-(1530/4000*N-N/2)^2/alpha))
count=count+1;
alpha=N*500*count;
first=-(1-N/2)*exp(-(1-N/2)^2/alpha);
end
% calculate the filter kernel
n=1:N
filter =-(n-N/2).*exp(-(n-N/2).^2/alpha);
%normalize kernel & return
HTH
Related Question