高斯曲线拟合

一、高斯函数
在这里插入图片描述
a表示得到曲线的高度,b是指曲线在x轴的中心,c指width(与半峰全宽有关),图形如下:
在这里插入图片描述
高斯拟合(Gaussian Fitting)即使用形如:

Gi(x)=Ai*exp((x-Bi)2/Ci2)

的高斯函数对数据点集进行函数逼近的拟合方法。

其实可以跟多项式拟合类比起来,不同的是多项式拟合是用幂函数系,
而高斯拟合是用高斯函数系。

使用高斯函数来进行拟合,优点在于计算积分十分简单快捷。
在这里插入图片描述
在这里插入图片描述
二、多高斯拟合

import numpy as np
import pylab as plt
#import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy import asarray as ar,exp
 
x = ar(range(10))
y = ar([0,1,2,3,4,5,4,3,2,1])
 
 
def gaussian(x,*param):
    return param[0]*np.exp(-np.power(x - param[2], 2.) / (2 * np.power(param[4], 2.)))+\
           param[1]*np.exp(-np.power(x - param[3], 2.) / (2 * np.power(param[5], 2.)))
 
 
popt,pcov = curve_fit(gaussian,x,y,p0=[3,4,3,6,1,1])
print popt
print pcov
 
plt.plot(x,y,'b+:',label='data')
plt.plot(x,gaussian(x,*popt),'ro:',label='fit')
plt.legend()
plt.show()

运行结果:
在这里插入图片描述

[ 4.68035609  0.36961389  4.94329154  8.34528036  2.13310944  0.67913747]
[[ 0.03858998  0.02308755 -0.00386378  0.01181436 -0.0162972  -0.00306613]
 [ 0.02308755  0.22679753 -0.04375697 -0.0531039  -0.05173268  0.00435262]
 [-0.00386378 -0.04375697  0.04252537  0.11052757  0.03062613 -0.24351661]
 [ 0.01181436 -0.0531039   0.11052757  0.81824277  0.08714636 -0.87927837]
 [-0.0162972  -0.05173268  0.03062613  0.08714636  0.03972189 -0.20849818]
 [-0.00306613  0.00435262 -0.24351661 -0.87927837 -0.20849818  2.47203996]]

参考博客:
https://blog.csdn.net/zhangap123/article/details/76673689

  • 22
    点赞
  • 171
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
% This folder contains a collection of "fitting" functions. % (Some has demo options - the third section) % The GENERAL input to the functions should be samples of the distribution. % % for example, if we are to fit a normal distribution ('gaussian') with a mean "u" and varaince "sig"^2 % then the samples will distribute like: % samples = randn(1,10000)*sig + u % %fitting with Least-Squares is done on the histogram of the samples. % fitting with Maximum likelihood is done directly on the samples. % % % Contents of this folder % ======================= % 1) Maximum likelihood estimators % 2) Least squares estimators % 3) EM algorithm for estimation of multivariant gaussian distribution (mixed gaussians) % 4) added folders: Create - which create samples for the EM algorithm test % Plot - used to plot each of the distributions (parametric plot) % % % % % % Maximum likelihood estimators % ============================= % fit_ML_maxwell - fit maxwellian distribution % fit_ML_rayleigh - fit rayleigh distribution % (which is for example: sqrt(abs(randn)^2+abs(randn)^2)) % fit_ML_laplace - fit laplace distribution % fit_ML_log_normal- fit log-normal distribution % fit_ML_normal - fit normal (gaussian) distribution % % NOTE: all estimators are efficient estimators. for this reason, the distribution % might be written in a different way, for example, the "Rayleigh" distribution % is given with a parameter "s" and not "s^2". % % % least squares estimators % ========================= % fit_maxwell_pdf - fits a given curve of a maxwellian distribution % fit_rayleigh_pdf - fits a given curve of a rayleigh distribution % % NOTE: these fit function are used on a histogram output which is like a sampled % distribution function. the given curve MUST be normalized, since the estimator % is trying to fit a normalized distribution function. % % % % % Multivariant Gaussian distribution % ================================== % for demo of 1
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值