Matlab:最小二乘拟合2d数据集

时间:2016-02-11 22:49:25

标签: matlab image-processing 2d least-squares

我有以下问题:

模型:中心对称圆,其轮廓是高斯分布和洛伦兹分布的组合。要获得模型的图,只需将以下代码插入Matlab:

N = 501;                            %Matrix size
R = zeros(N,N);                     %Initializing matrix R
x0 = ceil(N/2); y0 = x0;            %Barycenter coordinates

for i=1:N                           %Calculation of matrix R
    for j=1:N
        R(i,j) = sqrt((x0-j)^2 + (y0-i)^2);          
    end       
end

%Model z1 and the parameters:
peak = 0.275*N;                       %Peak location
m = 0.3;
sigma = 0.1*N;
gamma = 15;
A1 = 1000;
A2 = 50;
z1 = (1-m)*A1/(sigma*sqrt(pi))*exp(-(abs(R - peak)).^2/sigma^2) + m*A2/pi * (gamma./((abs(R - peak1)).^2 + gamma^2));

figure('name','Show Model')
surf(z1,'EdgeColor','none','LineStyle','none','FaceLighting','phong');

所以这就是“理想化”的模式。为了模拟真实数据,我将随机噪声添加到z1:

z2 = z1 + random('Normal',0,1,N,N);
figure('name','Show random noise data')
surf(z2,'EdgeColor','none','LineStyle','none','FaceLighting','phong');

最后是穿过重心的交叉平面图:

figure('name','Show intersecting plane with model and random noise data')
xaxis = -floor(N/2):1:floor(N/2);
intersectionline1 = z1(ceil(N/2),:);
intersectionline2 = z2(ceil(N/2),:);
plot(xaxis,intersectionline1,xaxis,intersectionline2,'.r');

Z2可以是我测量的真实数据集。圆圈在我的图像中的任何位置。我实际上能够找到重心并得到一个平方的扇区。除此之外,我得到了半径r(变量“peak”)的良好近似值。

我现在的问题: 是否可以对我的数据集进行最小二乘拟合,以从我的模型中获取参数m,sigma,gamma,peak,A1和A2 ???我不知道Matlab是否能够这样做......

提前致谢!

1 个答案:

答案 0 :(得分:2)

您可以使用fminsearch查找给出最小最小二乘的系数集:

% peak  = K(1)^2
% m     = K(2)^2
% sigma = K(3)^2
% gamma = K(4)^2
% A1    = K(5)^2
% A2    = K(6)^2

model = @(z0, K) (1-K(2)^2)*K(5)^2/(K(3)^2*sqrt(pi))*exp(-(abs(z0 - K(1)^2)).^2/K(3)^4) + K(2)^2*K(6)^2/pi * (K(4)^2./((abs(z0 - K(1)^2)).^2 + K(4)^4));
errsq = @(K) sum(sum((z2-model(R,K)).^2));
K0 = ones(6,1);
K  = fminsearch(errsq,K0);
figure('name','Reconstructed');
surf(model(R,K),'EdgeColor','none','LineStyle','none','FaceLighting','phong');

为确保系数保持正值,模型使用平方参数化。当然,初始猜测K0对最终结果非常重要。