Beispiel #1
0
        static SenParas LinearRegression(List <Point> parray)
        {
            SenParas para = new SenParas();

            if (parray.Count < 2)
            {
                System.Console.WriteLine("点个数不能少于2");
                return(null);
            }
            //求 X,Y平均值
            double averagex = 0; double averagey = 0;

            foreach (Point item in parray)
            {
                averagex += item.X;
                averagey += item.Y;
            }
            averagex /= parray.Count;
            averagey /= parray.Count;
            //经验回归系数的分子与分母
            double numerator   = 0;
            double denominator = 0;

            foreach (Point p in parray)
            {
                numerator   += (p.X - averagex) * (p.Y - averagey);
                denominator += (p.X - averagex) * (p.X - averagex);
            }
            //回归系数b(Regression Coefficient)
            para.RCB = numerator / denominator;
            //回归系数a
            para.RCA = averagey - para.RCB * averagex;
            //剩余平方和与回归平方和
            double residualSS   = 0; //(Residual Sum of Squares)
            double regressionSS = 0; //(Regression Sum of Squares)

            foreach (Point p in parray)
            {
                residualSS +=
                    (p.Y - para.RCA - para.RCB * p.X) *
                    (p.Y - para.RCA - para.RCB * p.X);
                regressionSS +=
                    (para.RCA + para.RCB * p.X - averagey) *
                    (para.RCA + para.RCB * p.X - averagey);
            }
            //计算R^2的值
            numerator = 0; denominator = 0;
            foreach (var item in parray)
            {
                denominator += Math.Pow((item.Y - averagey), 2.0);
                numerator   += Math.Pow((item.Y - (item.X * para.RCB + para.RCA)), 2.0);
            }
            para.ResidualSS   = residualSS;
            para.RegressionSS = regressionSS;
            para.RSquare      = 1 - numerator / denominator;
            //拟合误差越接近1则表示越准确
            return(para);
        }
Beispiel #2
0
        static void GetSensitivity()
        {
            StringBuilder sb        = new StringBuilder();
            List <Point>  listPoint = new List <Point>();

            try
            {
                //测量数量 衰减初始值 步进
                int    testCount    = 10;
                double initialValue = 26;
                double interval     = 0.5;

                Point point = new Point();
                for (int i = 0; i < testCount; i++)
                {
                    //Hp8156.Open();
                    Hp8156.SetAtt(initialValue.ToString());
                    point.X       = initialValue;
                    initialValue += interval;
                    //sb.Append(point.X.ToString());
                    Thread.Sleep(1000);
                    string errorRate = Mp.GetErrorRate();
                    string str       = errorRate.Substring(2);
                    //Error rate
                    double.TryParse(str.Trim(), out double result);
                    //判定当结果不为0时
                    if (result > 0)
                    {
                        //取对数
                        point.Y = Math.Log10(result);
                        listPoint.Add(point);
                    }
                    Console.WriteLine("X:{0},Y:{1}", point.X, point.Y);
                }
                Console.WriteLine();
                //噪声过滤
                while (IsCountChanged && listPoint.Count > 5)
                {
                    listPoint = PointFilter(listPoint);
                }
                Console.WriteLine();
                foreach (var item in listPoint)
                {
                    Console.WriteLine("X:{0},Y:{1}", item.X, item.Y);
                }
                //计算灵敏度
                SenPara = LinearRegression(listPoint);
                //灵敏度计算条件:Error rate@E-3
                SenPara.Sensitive = (-3 - SenPara.RCA) / SenPara.RCB;
            }
            catch
            {
            }
        }