Parameterized Gaussian distribution function (no toolboxes needed)
This anonymous function produces a normal probability density curve at the values in x with a mean of mu and a standard deviation of sigma.
gausDist = @(x,mu,sig)(1./(sig.*sqrt(2*pi))).*exp((-(x-mu).^2)./(2.*sig.^2));
- x is an array of x-values.
- mu is the mean
- sig is the standard deviation
The anonymous function is equivalent to normpdf(x,mu,sig) (Statistics and Machine Learning Toolbox) except that normpdf returns NaN values when the standard deviation (sig) is negative whereas the anonymous function will evaluate the negative value. Comparison between normpdf and the anonymous function:
plot(x,y,'b-','LineWidth',3,'DisplayName','Anonymous fcn')
plot(x,y2,'r--','LineWidth',3,'DisplayName','normpdf')
title(sprintf('Guassian with \\mu=%.1f \\sigma=%.1f', mu, sig))
Fully parameterized Gaussian function (no toolboxes needed)
This anonymous function creates a Gaussian curve that includes parameters for amplitude and vertical offset.
gaus = @(x,mu,sig,amp,vo)amp*exp(-(((x-mu).^2)/(2*sig.^2)))+vo;
- x is an array of x-values.
- mu is the mean
- sig is the standard deviation
- amp is the (positive or negative)
- vo is the vertical offset from baseline (positive or negative)
To add noise along the y-axis of the guassian,
y = gaus(___);
yh = y + randn(size(y))*amp*.10;
Demo:
y = gaus(x,mu,sig,amp,vo);
plot(x, y, 'b-', 'LineWidth',3)
yh = y + randn(size(y))*amp*.10;
plot(x, yh, 'ro','markersize', 4)
title(sprintf('Guassian with \\mu=%.1f \\sigma=%.1f amp=%.1f vo=%.1f', ...
normpdf from the Statistics and Machine Learning Toolbox
y = normpdf(x,mu,sigma) produces a normal probability density curve at the values in x with a mean of mu and a standard deviation of sigma. Unlike the parameterized anonymous function above, the output to normpdf carries a specifc meaning. Therefore, amplitude and vertical offset are not specified in normpdf. This demo uses the same argument values as above.
plot(x,y,'-b','LineWidth',3)
gaussmf from the Fuzzy Logic Toolbox
y = gaussmf(x,[sigma,mu]) produces a Gaussian membership function which differs from the probability density function produced by normpdf. A key difference is that the Gaussian membership function always has a maximum value of 1. Given a sufficiently wide range of x values, the curve ranges from near 0 to 1 along the y axis. Thus, amplitude (amp) and vertical offset (vo) can be applied to the curve by, y = amp * gaussmf(x,[sigma,mu]) + vo;
This demo uses the same argument values as above and compares gausmf (red) and the parameterized anonymous function (blue).
y = gaus(x,mu,sig,amp,vo);
y2 = amp*gaussmf(x,[sig,mu])+vo;
plot(x, y, 'b-', 'LineWidth',3, 'DisplayName','Parameterized fcn')
plot(x, y2, 'r--', 'LineWidth',4, 'DisplayName','gaussmf()')
title(sprintf('Guassian with \\mu=%.1f \\sigma=%.1f amp=%.1f vo=%.1f', ...