|
沙发
楼主 |
发表于 2021-6-1 14:32:29
|
只看该作者
一个较新的版本
- function [Gau_fit,Gau_par,x_fit,y_fit,fx]=fun_mm_fit_1Gau(x,y)
- %%
- %
- % [Gau_fit,Gau_par,x_fit,y_fit]=fun_mm_fit_1Gau(x,y)
- %
- % %--------------------------------------------------------------
- % Inputs:
- % x,y
- % Outputs:
- % Gau_fit : the Gaussian fit value of x
- % Gau_par : the parameters in
- % b(1)*exp(-b(2)*(x-b(3)).^2)+b(4)
- % x_fit : more x points for a smoother line
- % y_fit : more y points for a smoother line
- % fx : the function to calculate the points users define
- % % x_new = [ 1 3 17 30 40];
- % % y_new = fx(Gau_par,x_new);
- %
- % %--------------------------------------------------------------
- % example
- % np = 1000;
- % p = 10* randn(1,np)+3 +10;
- % hi_bi = linspace(-40,60,50);
- % hi_va = hist(p,hi_bi);
- %
- % x = hi_bi;
- % y = hi_va;
- % [Gau_fit,Gau_par,x_fit,y_fit,fx]=fun_mm_fit_1Gau(x,y);
- %
- %
- % x_new = [ 1.2 3.3 17.8 30.001 40.08];
- % y_new = fx(Gau_par,x_new);
- %
- % hold on
- % stairs(hi_bi,hi_va,'b-')
- % plot(x,Gau_fit,'gs')
- % plot(x_fit,y_fit,'k-','linewidth',2)
- % plot(x_new,y_new,'ro','markersize',10)
- %
- % %--------------------------------------------------------------
- % type by : mm
- % contact me : meatball1982@163.com
- % typed on : 01-Jun-2021 14:11:21
- %% main
- % define the original function
- fx=@(b,x)b(1)*exp(-b(2)*(x-b(3)).^2)+b(4);
- % point number
- n_p = length(x);
- % smooth version of y, for find the initial b(3) with one peak
- y_sm = smooth(y,ceil(n_p/10)+1);
- [pks_va,pks_ind]=findpeaks(y_sm);
- % for lsq fit, there is value < 0.
- y(y==0)=-0.001;
- b = ones(1,4);
- b(3)=x(pks_ind(1));
- b(1)=max(y);
- for i_ite=1:10
- % Solve nonlinear curve-fitting problems in least-squares sense.
- % just a guess.
- b=lsqcurvefit(fx,b,x,y);
- % Nonlinear regression,
- b=nlinfit(x,y,fx,b);
- end
- % more points and smoother
- x_fit = linspace(min(x),max(x),n_p*5);
- y_fit = fx(b,x_fit);
- y_fit(y_fit<0)=0;
- % Fit points
- Gau_fit = fx(b,x);
- Gau_fit(Gau_fit<0)=0;
- % parameters
- Gau_par = b;
- %% logs
- % mod : 01-Jun-2021 13:24:37
- %
复制代码
|
|