Mathematica中文论坛-非官方

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz

豆粑粑 fit Gaussian matlab

查看数: 24993 | 评论数: 1 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2019-1-25 11:31

正文摘要:

本帖最后由 meatball1982 于 2019-1-25 16:40 编辑 matlab fit Gaussian distribution init parameter is max, std and  mean of y. the both of the lsqcurvefit and nlinfit are applied to iterat ...

回复

meatball1982 发表于 2021-6-1 14:32:29
一个较新的版本


  1. function [Gau_fit,Gau_par,x_fit,y_fit,fx]=fun_mm_fit_1Gau(x,y)
  2. %%
  3. %
  4. % [Gau_fit,Gau_par,x_fit,y_fit]=fun_mm_fit_1Gau(x,y)
  5. %
  6. % %--------------------------------------------------------------
  7. % Inputs:
  8. % x,y
  9. % Outputs:
  10. % Gau_fit : the Gaussian fit value of x
  11. % Gau_par : the parameters in
  12. %           b(1)*exp(-b(2)*(x-b(3)).^2)+b(4)
  13. % x_fit   : more x points for a smoother line
  14. % y_fit   : more y points for a smoother line
  15. % fx      : the function to calculate the points users define
  16. %         % x_new = [ 1 3 17 30 40];
  17. %         % y_new = fx(Gau_par,x_new);
  18. %
  19. % %--------------------------------------------------------------
  20. % example
  21. % np = 1000;
  22. % p = 10* randn(1,np)+3 +10;
  23. % hi_bi = linspace(-40,60,50);
  24. % hi_va = hist(p,hi_bi);
  25. %
  26. % x = hi_bi;
  27. % y = hi_va;
  28. % [Gau_fit,Gau_par,x_fit,y_fit,fx]=fun_mm_fit_1Gau(x,y);
  29. %
  30. %
  31. % x_new = [ 1.2 3.3 17.8 30.001 40.08];
  32. % y_new = fx(Gau_par,x_new);
  33. %
  34. % hold on
  35. % stairs(hi_bi,hi_va,'b-')
  36. % plot(x,Gau_fit,'gs')
  37. % plot(x_fit,y_fit,'k-','linewidth',2)
  38. % plot(x_new,y_new,'ro','markersize',10)
  39. %
  40. % %--------------------------------------------------------------
  41. % type by : mm
  42. % contact me : meatball1982@163.com
  43. % typed on : 01-Jun-2021 14:11:21

  44. %% main
  45. % define the original function
  46. fx=@(b,x)b(1)*exp(-b(2)*(x-b(3)).^2)+b(4);

  47. % point number
  48. n_p = length(x);

  49. % smooth version of y, for find the initial b(3) with one peak
  50. y_sm = smooth(y,ceil(n_p/10)+1);
  51. [pks_va,pks_ind]=findpeaks(y_sm);

  52. % for lsq fit, there is value < 0.
  53. y(y==0)=-0.001;
  54. b  = ones(1,4);
  55. b(3)=x(pks_ind(1));
  56. b(1)=max(y);

  57. for i_ite=1:10
  58.     % Solve nonlinear curve-fitting problems in least-squares sense.
  59.     % just a guess.
  60.     b=lsqcurvefit(fx,b,x,y);
  61.     % Nonlinear regression,
  62.     b=nlinfit(x,y,fx,b);
  63. end

  64. % more points and smoother
  65. x_fit = linspace(min(x),max(x),n_p*5);
  66. y_fit = fx(b,x_fit);
  67. y_fit(y_fit<0)=0;

  68. % Fit points
  69. Gau_fit = fx(b,x);
  70. Gau_fit(Gau_fit<0)=0;

  71. % parameters
  72. Gau_par = b;


  73. %% logs
  74. % mod : 01-Jun-2021 13:24:37
  75. %
复制代码


QQ|Archiver|手机版|小黑屋|Mathematica中文论坛-非官方 ( 辽ICP备16001491号-1

GMT+8, 2024-12-27 23:53 , Processed in 0.102138 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表