Matlab编码说明 [英] Explanation of the Matlab coding

查看:143
本文介绍了Matlab编码说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上找到了这个Matlab代码.它实际上是整个代码的一部分,可以在此处找到.有人可以逐行解释发生了什么.我真的很绝望...

I found this Matlab code on the internet. Its actually a part of the whole code which can be found here. Can someone explain line by line what's happening. Im really desperate...

% ------------- % 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

推荐答案

此函数尝试返回高斯的梯度-当您将其与一组o数据点进行卷积时,它将检测数据中的边缘,同时平滑数据中的点.平均水平没有突然变化的区域.这是N = 15时的输出:

This function attempts to return the gradient of a Gaussian - when you convolve that with a set o data points it will detect edges in the data while smoothing points in a region where there isn't a sudden shift in mean level. Here is the output when N = 15:

但是,如果将N增加到20,则会发生疯狂的事情,因为代码坦率地说是错误的(以及丑陋的).最终,您得到的不是一条漂亮的曲线,而是一条直线-一个可怕的过滤器.这是因为尝试为alpha计算新值是可怕的.

However, if you increase N to 20, something crazy happens because the code is frankly buggy (as well as ugly). Instead of a nice curve, you end up with something of a straight line - a terrible filter. This is because the attempt to compute a new value for alpha is horrible.

Robert P已经提供了代码工作的逐步说明.让我向您展示正确的方式"编写此功能(您将看到我使用了Robert提到的一些技术,以及其他一些,我会解释)...

A step by step description of what the code is doing was already provided by Robert P. Let me show you "the right way" to write this function (you will see that I use some of the techniques that Robert mentions, plus some others that I will explain)...

function myFilter = gaussfilt2(N, alpha)
% myFilter = gaussfilt(N, alpha)
% returns an N point normalized array of filter coefficients
% corresponding to the gradient of a Gaussian over the interval [-1 1]
% with a standard deviation of 1/alpha 
% in other words, the higher alpha, the sharper the filter
% default value for alpha is 3

if ~exist('alpha', 'var')
  alpha = 3; 
end 

x = linspace( -1, 1, N); % create a vector of N values between -1 and 1 inclusive
sigma = 1.0 / alpha; % convert from alpha to sigma as used in Gaussian formula

% compute first derivative, but leave constants out
% we will normalize later by summing over the coefficients
myFilter = -x .* exp( -(x.^2)/(2*sigma.^2)); % using .* for element-by-element operation

% normalize:
myFilter = myFilter / sum( abs( myFilter ) ); % absolute sum of coefficients is now one

使用N=150alpha = 3运行此功能时,曲线如下所示:

When you run this function with N=150 and alpha = 3, the curve looks like this:

一些需要解释的技术:

文档,当您在功能声明下方开始注释框(用%表示)时,当用户请求此功能的帮助时,该框内的所有内容都会显示给用户(例如,通过在另一个脚本中突出显示功能名称并按F1).永远是个好主意

documentation when you start a comment block (indicated with %) right below the function declaration, everything in that block will be shown to the user when he/she requests help for this function (for example, by highlighting the function name in another script and pressing F1). Always a good idea

向量化 Matlab在执行显式循环"方面非常糟糕,并且在隐式循环"方面非常出色.只要有可能,您都希望一次对一堆值进行相同的计算".在这种情况下,我计算出向量x c4 linspace(x1,x2,n)c5 n n c6 x 1 c7 x2(含).现在,我可以用一个语句来计算整个函数

vectorization Matlab is terrible at doing "explicit loops" and really good at "implicit loops". Whenever possible you want to do "the same calculation" on a whole bunch of values at once. In this case, I compute a vector xusing thelinspace( x1, x2, n )function. This returns an equally spaced array ofnvalues fromx1tox2` inclusive. Now I can compute the entire function with a single statement

逐元素乘法 Matlab实际上是用于矩阵处理的(在"Matlab"中是"Mat"-它代表矩阵,而不是数学).如果您有两个向量ab,并且想要将它们逐个元素相乘(因此结果为[a(1)8b(1) a(2)*b(2) ... a(n)*b(n)]),则可以使用.*运算符.

element by element multiplication Matlab is really intended for matrix manipulation (that's the "Mat" in "Matlab" - it stands for matrix, not mathematics). If you have two vectors a and b, and you want to multiply them element-by-element (so the result is [a(1)8b(1) a(2)*b(2) ... a(n)*b(n)]), you use the .* operator.

灵活性代替硬接线" alpha值,使其成为第二个参数使您可以重新使用相同的功能并更改滤镜的清晰度.允许用户忽略该变量并提供默认值意味着该函数仍可被该函数仅具有单个参数时编写的程序使用.当存在名称为'alpha'的变量('var')时,exist('alpha', 'var')调用将返回true.在函数前添加~会否定结果-就像其他语言中的If Not一样.

flexibility Instead of "hard wiring" the value for alpha, making it a second parameter allows you to re-use the same function and change the sharpness of the filter. Allowing the user to omit the variable and supplying a default means it can still be used by programs that were written when the function only had a single parameter. The exist('alpha', 'var') call returns true when a variable ('var') with the name 'alpha' exists. Adding the ~ in front of the function negates the result - it is like If Not in some other languages.

这篇关于Matlab编码说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆