如何计算和显示标准差?

时间:2013-06-04 18:04:26

标签: c# winforms microsoft-chart-controls standard-deviation

我正在尝试显示一个频谱,它的偏差是上下偏差。因此,我有一个Class,“SpectrumClass”,其中cooridantes存储在DataTable(dtCoords)中。

我展示的光谱是其他几种光谱的平均值。我用这些光谱计算标准偏差如下(所有光谱具有相同数量的数据点):

DataTable dt = new DataTable();
        DataColumn columnX = new DataColumn("X");
        DataColumn columnY = new DataColumn("Y");

        dt.Columns.Add(columnX);
        dt.Columns.Add(columnY);

        SpectrumClass stdSpectrum = new SpectrumClass(0,"Standard deviation",dt);

        //Iterate through each Intensity value
        for (int i = 0; i < specs[0].dtCoords.Rows.Count; i++)
        {
            double rShift = 0;
            double IntensitySum = 0;

            //Calculate std-Points for each Intensity value
            foreach(SpectrumClass spec in specs)
            {
                IntensitySum += Convert.ToDouble(spec.dtCoords.Rows[i][1], System.Globalization.CultureInfo.InvariantCulture);
                rShift = Convert.ToDouble(spec.dtCoords.Rows[i ][0], System.Globalization.CultureInfo.InvariantCulture);
            }

            //Averge-value (Mittelwert)
            IntensitySum /= specs.Count;

            //Varianz
            double variance = 0;
            foreach (SpectrumClass spec in specs)
            {
                //Here IntensitySum is the average value
                variance += ((Convert.ToDouble(spec.dtCoords.Rows[i][1], System.Globalization.CultureInfo.InvariantCulture) - IntensitySum) * (Convert.ToDouble(spec.dtCoords.Rows[i][1], System.Globalization.CultureInfo.InvariantCulture) - IntensitySum));
            }

            //Std-Point
            double stdValue = variance /(specs.Count-1);

            stdValue = Math.Sqrt(stdValue);

            stdSpectrum.dtCoords.Rows.Add(rShift.ToString(System.Globalization.CultureInfo.InvariantCulture), stdValue.ToString(System.Globalization.CultureInfo.InvariantCulture));

        }

        return stdSpectrum;

后来谈到视觉部分我创建了两个新的系列,它应该包含上下偏差。在这里,我只是添加或减去平均光谱相应点的Y值。

for (int i = dt.Rows.Count; i > 0;i--)
            {
                //Obere STD grenze
                if (expMeas.Count > 1)
                {

                    ExpChart.Series["STD+"].Points.AddXY(double.Parse(stdSpec.dtCoords.Rows[i - 1][0].ToString(), System.Globalization.CultureInfo.InvariantCulture)
                 , double.Parse(dt.Rows[i - 1][1].ToString(), System.Globalization.CultureInfo.InvariantCulture) + double.Parse(stdSpec.dtCoords.Rows[i - 1][1].ToString(), System.Globalization.CultureInfo.InvariantCulture));

                    ExpChart.Series["STD-"].Points.AddXY(double.Parse(stdSpec.dtCoords.Rows[i - 1][0].ToString(), System.Globalization.CultureInfo.InvariantCulture)
              , double.Parse(dt.Rows[i - 1][1].ToString(), System.Globalization.CultureInfo.InvariantCulture) - double.Parse(stdSpec.dtCoords.Rows[i - 1][1].ToString(), System.Globalization.CultureInfo.InvariantCulture));
                }

                ExpChart.Series["Spectrum"].Points.AddXY(double.Parse(stdSpec.dtCoords.Rows[i - 1][0].ToString(), System.Globalization.CultureInfo.InvariantCulture)
                 , double.Parse(dt.Rows[i - 1][1].ToString(), System.Globalization.CultureInfo.InvariantCulture));
            }

这是我的问题:

I think the standard-deviation spectra (in gray) should look the same?

我认为标准偏差光谱(灰色)应该看起来一样吗? 我随机检查了标准偏差点,它们似乎是正确的。在我的一个循环中,这可能是一个错误吗? 我希望有人经历过同样的事情,通过千分点进行检查并不好玩

提前致谢。

1 个答案:

答案 0 :(得分:0)

我想知道评论

下是否有错误

// Obere STD grenze

在中间区块?

当你想要调用stdSpec时,你是在调用dt吗?

double.Parse(dt.Rows [i - 1] [0] .ToString()

double.Parse(stdSpec.dtCoords.Rows [i - 1] [0]