计算 两个 sin 正弦波 的RMSE

 测试值的采样率 远低于测试台的采样率 。这样两个波形是否一致?我们通过采取等样抽取的方式抽取出测试台的数据,计算出最小的标准误差。

#region 计算 两个 sin 正弦波 的误差

        private double[] getValuable(double[] data)
        {
            int indexHigh = 0;int indexLow = 0; int cnt = 0; int indexS = 0;
            for (int i = 0; i < data.Length; i++)
            {
                double max = 0;double min = 0;
                if (data[i] >= max) { max = data[i]; indexHigh = i;  }
                if (data[i] <= min) { min = data[i]; indexLow = i; }
            }
            cnt = (  indexLow- indexHigh) * 2;
            indexS = indexHigh - (indexLow-indexHigh) /2;
            double[] buf = new double[cnt];

            Array.Copy(data, indexS, buf, 0,cnt);

            return buf;
        }  //获取有用的数据

        private double[] getSample(double[] b, int len,int step=0) //取样
        {
            //double b 是否需要优化  
            double[] buf = new double[len];
            List<int> indexList = sampleList(b.Length, len);            

            foreach (int index in indexList)
            {
                if (index + step >= b.Length) {
                    buf[index] = b[b.Length-1];
                }
                else
                {
                    buf[index] = b[index + step];
                }
            }


            return buf;
        }
        
        private List<int> sampleList(int inputLen, int sampleLen)  //返回的是各取样点的索引值的List
        {    
            // inputLen>= len
            double stepSize = (double)inputLen / sampleLen;//初始步长
                                                    // 兼容inputLen< len 的情况
            if (stepSize < 1)
                stepSize = 1;
            List<int> IndexList = new List<int>();//索引值的List
            for (int i = 0, n = 0; i < inputLen || n < sampleLen; n++)//i是待处理数据的索引,n是循环次数
            {
                var currentStepIndex = n * stepSize;
                var currentIndex = nearestInteger(currentStepIndex);
                if (currentIndex >= inputLen)
                {
                    break;
                }
                if (IndexList.Contains(currentIndex))
                {
                    currentIndex++;
                    if (currentIndex >= inputLen)
                        break;
                }
                IndexList.Add(currentIndex);
                i = currentIndex;
            }
            return IndexList;
        }
        private int nearestInteger(double data)  //向下取整
        {
            int ret = 0;
            double input = Math.Ceiling(data);//向上取整
            double step = Math.Floor(data);//向下取整
            if (Math.Abs(input - data) >= Math.Abs(data - step))
            {
                ret = (int)step;
            }
            else
            {
                ret = (int)input;
            }
            return ret;
        }

        private double RMSE(double[] a, double[] b,out double min,out double max)  //根据 RMSE 标准误差公式
        {
            min = a.Min(); max = a.Max();  
            double sum = 0;
            for (int i = 0; i < a.Length; i++)
            {
                sum += (a[i] - b[i]) * (a[i] - b[i]);
            }
            sum = sum / a.Length;
            sum = Math.Sqrt(sum);
            return sum;
        }
        /// <summary>
        /// 计算 两个 sin 正弦波 的误差
        /// </summary>
        /// <param name="ECU"> 测试值 </param>
        /// <param name="Tester">观察值</param>
        /// <param name="min">输出最小测量值</param>
        /// <param name="max">输入最大测量值</param>
        public void calcRMS(double[] ECU, double[] Tester, out double min, out double max)
        {
            double[] a = getValuable(ECU);
            double[]temp = getValuable(Tester);   // 需不需要优化 平顺 信号

            min = a.Min(); max = a.Max();

            int step = temp.Length / a.Length;  // step 最大值 是temp.Length/a.Length
            step = 0; //
            double minRMS = 100;

            for (int i = 0; i <= step; i++)
            {
                double[] b = getSample(temp, a.Length, step);

                double rms = RMSE(a, b, out min, out max);
                if (rms < minRMS) { minRMS = rms; }
            }

        }

        #endregion

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值