public void InitialAverage()
        {
            var statistic = new RunningStatistics(span);

            double[] data = new[] {
                1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
            };

            for (int i = 0; i < span; i++)
            {
                statistic.Add(data[i]);

                double mean     = 0;
                double variance = 0;

                for (int j = 0; j <= i; j++)
                {
                    mean += data[j];
                }
                mean /= i + 1;

                Assert.That(statistic.Mean, Is.EqualTo(mean).Within(0.00001), "Initial mean i = " + i);
                if (i != 0)
                {
                    for (int j = 0; j <= i; j++)
                    {
                        double term = data[j] - mean;
                        variance += (term * term);
                    }
                    variance /= (i + 1);
                }
                Assert.That(statistic.Variance, Is.EqualTo(variance).Within(0.00001), "Initial variance i = " + i);
            }

            for (int i = span; i < data.Length; i++)
            {
                statistic.Add(data[i]);

                double mean     = 0.0;
                double variance = 0;

                for (int j = i - span + 1; j <= i; j++)
                {
                    mean += data[j];
                }
                mean /= span;
                Assert.That(statistic.Mean, Is.EqualTo(mean).Within(0.00001), "Running mean i = " + i);

                for (int j = i - span + 1; j <= i; j++)
                {
                    double term = data[j] - mean;
                    variance += (term * term);
                }
                variance /= (span);
                Assert.That(statistic.Variance, Is.EqualTo(variance).Within(0.00001), "Running variance i = " + i);
            }
        }
        /// <summary>
        /// Analyze the scanline Scanline around point XCenter
        /// </summary>
        public void Analyze()
        {
            if (!XCenter.HasValue && !ScanLine.HasValue)
            {
                throw new Exception();
            }

            int    x       = (int)XCenter;
            int    y       = (int)ScanLine;
            double start   = Image[x, y];
            double meanMax = 0.0;

            var stats = new RunningStatistics(5);

            stats.Add(Image[x, y]);
            stats.Add(Image[x - 1, y]);

            int    dx = x - 1;
            double stdDev;
            double mean;

            do
            {
                stats.Add(Image[--dx, y]);
                stdDev = Math.Sqrt(stats.Variance);
                mean   = stats.Mean;
                if (mean > meanMax)
                {
                    meanMax = mean;
                }
            } while (Math.Abs(mean - LeftBackground) > 3 * LeftBackgroundDeviation);

            LeftBackgroundPosition  = dx + 1;
            LeftBackground          = mean;
            LeftBackgroundDeviation = stdDev;

            stats.Clear(5);
            stats.Add(Image[x, y]);
            stats.Add(Image[x - 1, y]);

            dx = x + 1;
            do
            {
                stats.Add(Image[++dx, y]);
                stdDev = Math.Sqrt(stats.Variance);
                mean   = stats.Mean;
                if (mean > meanMax)
                {
                    meanMax = mean;
                }
            } while (Math.Abs(mean - RightBackground) > 3 * RightBackgroundDeviation);

            RightBackgroundPosition  = dx - 1;
            RightBackground          = mean;
            RightBackgroundDeviation = stdDev;

            IsBackgroundLine = (Math.Abs(meanMax - LeftBackground) < 3 * LeftBackgroundDeviation) &&
                               (Math.Abs(meanMax - RightBackground) < 3 * RightBackgroundDeviation);
        }
        public bool FindBackground()
        {
            if (!XCenter.HasValue && !ScanLine.HasValue)
            {
                throw new Exception();
            }

            int    x     = (int)XCenter;
            int    y     = (int)ScanLine;
            double start = Image[x, y];

            var stats = new RunningStatistics(5);

            stats.Add(Image[x, y]);
            stats.Add(Image[x - 1, y]);

            int    dx = x - 1;
            double stdDev;
            double mean;
            double lastVariance;

            do
            {
                lastVariance = stats.Variance;
                stats.Add(Image[--dx, y]);
                stdDev = Math.Sqrt(stats.Variance);
                mean   = stats.Mean;
            } while (stats.Variance < lastVariance || Math.Abs(mean - start) < 3 * stdDev);

            LeftBackgroundPosition  = dx + 1;
            LeftBackground          = mean;
            LeftBackgroundDeviation = stdDev;

            stats.Clear(5);
            stats.Add(Image[x, y]);
            stats.Add(Image[x - 1, y]);

            dx = x + 1;
            do
            {
                lastVariance = stats.Variance;
                stats.Add(Image[++dx, y]);
                stdDev = Math.Sqrt(stats.Variance);
                mean   = stats.Mean;
            } while (stats.Variance < lastVariance || Math.Abs(mean - start) < 3 * stdDev);

            RightBackgroundPosition  = dx - 1;
            RightBackground          = mean;
            RightBackgroundDeviation = stdDev;

            IsBackgroundLine = false;

            return(true);
        }
        public void InitialAverage()
        {
            var statistic = new RunningStatistics(span);

            double[] data = new[] {
                1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
            };

            for (int i = 0; i < span; i++)
            {
                statistic.Add(data[i]);

                double mean = 0;
                double variance = 0;

                for (int j = 0; j <= i; j++)
                {
                    mean += data[j];
                }
                mean /= i + 1;

                Assert.That(statistic.Mean, Is.EqualTo(mean).Within(0.00001), "Initial mean i = " + i);
                if (i != 0)
                {
                    for (int j = 0; j <= i; j++)
                    {
                        double term = data[j] - mean;
                        variance += (term * term);
                    }
                    variance /= (i + 1);
                }
                Assert.That(statistic.Variance, Is.EqualTo(variance).Within(0.00001), "Initial variance i = " + i);
            }

            for (int i = span; i < data.Length; i++)
            {
                statistic.Add(data[i]);

                double mean = 0.0;
                double variance = 0;

                for (int j = i - span + 1; j <= i; j++)
                {
                    mean += data[j];
                }
                mean /= span;
                Assert.That(statistic.Mean, Is.EqualTo(mean).Within(0.00001), "Running mean i = " + i);

                for (int j = i - span + 1; j <= i; j++)
                {
                    double term = data[j] - mean;
                    variance += (term * term);
                }
                variance /= (span);
                Assert.That(statistic.Variance, Is.EqualTo(variance).Within(0.00001), "Running variance i = " + i);
            }
        }
        public bool FindBackground()
        {
            if (!XCenter.HasValue && !ScanLine.HasValue)
            {
                throw new Exception();
            }

            int x = (int)XCenter;
            int y = (int)ScanLine;
            double start = Image[x, y];

            var stats = new RunningStatistics(5);

            stats.Add(Image[x, y]);
            stats.Add(Image[x - 1, y]);

            int dx = x- 1;
            double stdDev;
            double mean;
            double lastVariance;
            do
            {
                lastVariance = stats.Variance;
                stats.Add(Image[--dx, y]);
                stdDev = Math.Sqrt(stats.Variance);
                mean = stats.Mean;
            } while (stats.Variance < lastVariance || Math.Abs(mean - start) < 3 * stdDev);

            LeftBackgroundPosition = dx + 1;
            LeftBackground = mean;
            LeftBackgroundDeviation = stdDev;

            stats.Clear(5);
            stats.Add(Image[x, y]);
            stats.Add(Image[x - 1, y]);

            dx = x + 1;
            do
            {
                lastVariance = stats.Variance;
                stats.Add(Image[++dx, y]);
                stdDev = Math.Sqrt(stats.Variance);
                mean = stats.Mean;
            } while (stats.Variance < lastVariance || Math.Abs(mean - start) < 3 * stdDev);

            RightBackgroundPosition = dx - 1;
            RightBackground = mean;
            RightBackgroundDeviation = stdDev;

            IsBackgroundLine = false;

            return true;
        }
        /// <summary>
        /// Analyze the scanline Scanline around point XCenter
        /// </summary>
        public void Analyze()
        {
            if (!XCenter.HasValue && !ScanLine.HasValue)
            {
                throw new Exception();
            }

            int x = (int)XCenter;
            int y = (int)ScanLine;
            double start = Image[x, y];
            double meanMax = 0.0;

            var stats = new RunningStatistics(5);

            stats.Add(Image[x, y]);
            stats.Add(Image[x - 1, y]);

            int dx = x - 1;
            double stdDev;
            double mean;
            do
            {
                stats.Add(Image[--dx, y]);
                stdDev = Math.Sqrt(stats.Variance);
                mean = stats.Mean;
                if (mean > meanMax)
                {
                    meanMax = mean;
                }
            } while (Math.Abs(mean - LeftBackground) > 3 * LeftBackgroundDeviation);

            LeftBackgroundPosition = dx + 1;
            LeftBackground = mean;
            LeftBackgroundDeviation = stdDev;

            stats.Clear(5);
            stats.Add(Image[x, y]);
            stats.Add(Image[x - 1, y]);

            dx = x + 1;
            do
            {
                stats.Add(Image[++dx, y]);
                stdDev = Math.Sqrt(stats.Variance);
                mean = stats.Mean;
                if (mean > meanMax)
                {
                    meanMax = mean;
                }
            } while (Math.Abs(mean - RightBackground) > 3 * RightBackgroundDeviation);

            RightBackgroundPosition = dx - 1;
            RightBackground = mean;
            RightBackgroundDeviation = stdDev;

            IsBackgroundLine = (Math.Abs(meanMax - LeftBackground) < 3 * LeftBackgroundDeviation) &&
                               (Math.Abs(meanMax - RightBackground) < 3 * RightBackgroundDeviation);
        }