C#. How do I Apply a Gaussian Bell Curve to my Peak?

Viewed 433

I'm having an inordinate amount of trouble understanding how to fit a gaussian curve to my peak. There are some similar questions but even after looking at these I'm still not sure. Statistics/probability theory etc were never my strong point but I think what confuses me is the gaussian bell curve is calculated simply using the mean and standard deviation. The actual raw values in the peak are not used unlike other curve fitting methods.

Ive implemented the gaussian function thus;

public static float getGaussian(float x,  float mean, float stdDev)
{
            float v1 = 1F / (stdDev * (float)Math.Sqrt(2 * Math.PI));
            float v2 = ((x - mean) * (x - mean)) / (2 * (stdDev * stdDev));
            return (v1 * (float)Math.Exp(-v2));
}

I think this is correct but my "curve" ends up looking like a flat plain so Im obviously doing something wrong.

Here's a sample peakenter image description here

The raw data ie the (x,y) points are

(9,0) (10,1) (11,2) (12,3) (13,4) (14,10) (15,12) (16,13) (17,23) (18,26) (19,23) (20,20) (21,17) (22,12) (23,5) (24,3) (25,3) (26,4) (27,2) (28,2) (29,1)

Can anyone explain how the gaussian curve should be calculated from this data and what the curve will look like?

2 Answers

It seems that you have only integer numbers. Assuming that this is a histogram coming from normal distributed data you would just have:

xs = sum_i x_i * y_i
ns = sum_i y_i
mean = xs / ns

ss = sum_i  y_i * ( x_i - mean) * ( x_i - mean)
sigma = sqrt( ss / ( ns - 1 ) )

Finally, if g( x, x0, s ) is a normalised gaussian with mean x0 and standard deviation s then

ns * g( x, mean, sigma )

should approximate your data.

Related