Mathematica中文论坛-非官方

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 22030|回复: 1
打印 上一主题 下一主题

豆粑粑 fit Gaussian matlab

[复制链接]

529

主题

598

帖子

3014

积分

论坛元老

Rank: 8Rank: 8

积分
3014
跳转到指定楼层
楼主
发表于 2019-1-25 11:31:12 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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 iterative



fun_fit_gau.m
  1. function [x_new,y_new,b]=fun_fit_gau(x,y)

  2. b(1)=max(y);
  3. b(2)= std(y);
  4. b(3)=mean(y);

  5. fx=@(b,x)b(1)*exp(-b(2)*(x-b(3)).^2);
  6. for l=1:3
  7. b=lsqcurvefit(fx,b,x,y);
  8. b=nlinfit(x,y,fx,b);
  9. end


  10. x_new=interp1([0:length(x)-1],x,[0:0.01:length(x)-1]);
  11. y_new = b(1)*exp(-b(2)*(x_new-b(3)).^2);
复制代码


main.m
  1. clear all
  2. clc
  3. clf



  4. %% outline
  5. %

  6. %% main
  7. dat1 = load('../data/deltE_QM1.dat');

  8. hi_bi = linspace(-20,5,26);

  9. [hi_va1]=hist(dat1(:,2),hi_bi);
  10. hi_va1=hi_va1./sum(hi_va1);

  11. [hi_bi1_new,hi_va1_new,b1]=fun_fit_gau(hi_bi,hi_va1);

  12. pl_max = 1.2*max(hi_va1);

  13. subplot(2,2,1)
  14. hold on
  15. plot(dat1(:,1),dat1(:,2),'m-','linewidth',1.5)
  16. grid on
  17. box on
  18. axis([0 1000 -20 5])
  19. title('data')

  20. subplot(2,2,2)
  21. hold on
  22. barh(hi_bi,hi_va1,'m')
  23. axis([0 pl_max -20 5])
  24. grid on
  25. box on
  26. title('hist')

  27. subplot(2,2,3)
  28. hold on
  29. plot(hi_bi,hi_va1,'mo')
  30. plot(hi_bi1_new,hi_va1_new,'r-','linewidth',2)

  31. box on
  32. grid on
  33. axis([-20 5 0 pl_max])
  34. title('Gau fit')

  35. h=gcf;
  36. fi_na='../file_imgs/fig_gau_1dat';
  37. fun_work_li_035_myfig_out(h,fi_na,3);
复制代码


deltE_QM1.zip

4.48 KB, 下载次数: 0

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

529

主题

598

帖子

3014

积分

论坛元老

Rank: 8Rank: 8

积分
3014
沙发
 楼主| 发表于 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-4-20 19:54 , Processed in 0.132898 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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