Next  |  Prev  |  Up  |  Top  |  Index  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

Fitting a Gaussian to Data

When fitting a single Gaussian to data, one can take a log and fit a parabola. In matlab, this can be carried out as in the following example:

x = -1:0.1:1;
sigma = 0.01;
y = exp(-x.*x) + sigma*randn(size(x)); % test data:
[p,s] = polyfit(x,log(y),2); % fit parabola to log
yh = exp(polyval(p,x)); % data model
norm(y-yh) % ans =  1.9230e-16 when sigma=0
In practice, it is good to avoid zeros in the data. For example, one can fit only to the middle third or so of a measured peak, restricting consideration to measured samples that are positive and ``look Gaussian'' to a reasonable extent. An improved estimate can be obtained using weighted least squares for the fit: Assuming the noise is uncorrelated at each point, the optimal weighting for each squared error can be shown to be the inverse of the variance of the noise at that point (search for ``generalized least squares''). The weighting y has been found to work well in practice [#!Wang2018!#].

Next  |  Prev  |  Up  |  Top  |  Index  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

[How to cite this work]  [Order a printed hardcopy]  [Comment on this page via email]

``Spectral Audio Signal Processing'', by Julius O. Smith III, W3K Publishing, 2011, ISBN 978-0-9745607-3-1.
Copyright © 2019-10-29 by Julius O. Smith III
Center for Computer Research in Music and Acoustics (CCRMA),   Stanford University