AForge.Math.ContinuousHistogram.Update C# (CSharp) Method

Update() public method

Update statistical value of the histogram.
The method recalculates statistical values of the histogram, like mean, standard deviation, etc. The method should be called only in the case if histogram values were retrieved through Values property and updated after that.
public Update ( ) : void
return void
        public void Update( )
        {
            int hits;
            int i, n = values.Length;
            int nM1 = n - 1;

            float rangeLength = range.Length;
            float rangeMin = range.Min;

            max    = 0;
            min    = n;
            mean   = 0;
            stdDev = 0;
            total  = 0;

            double sum = 0;

            // calculate mean, min, max
            for ( i = 0; i < n; i++ )
            {
                hits = values[i];

                if ( hits != 0 )
                {
                    // max
                    if ( i > max )
                        max = i;
                    // min
                    if ( i < min )
                        min = i;
                }

                // accumulate total value
                total += hits;
                // accumulate mean value
                sum += ( ( (double) i / nM1 ) * rangeLength + rangeMin ) * hits;
            }

            if ( total != 0 )
            {
                mean = (float) ( sum / total );
            }

            min = ( min / nM1 ) * rangeLength + rangeMin;
            max = ( max / nM1 ) * rangeLength + rangeMin;

            // calculate stadard deviation
            sum = 0;
            double diff;

            for ( i = 0; i < n; i++ )
            {
                hits = values[i];
                diff = ( ( (double) i / nM1 ) * rangeLength + rangeMin ) - mean;
                sum += diff * diff * hits;
            }

            if ( total != 0 )
            {
                stdDev = (float) Math.Sqrt( sum / total );
            }

            // calculate median
            int m, halfTotal = total / 2;

            for ( m = 0, hits = 0; m < n; m++ )
            {
                hits += values[m];
                if ( hits >= halfTotal )
                    break;
            }
            median = ( (float) m / nM1 ) * rangeLength + rangeMin;
        }
    }