基于平均值预测数据

本文关键字:数据 平均值 | 更新日期: 2023-09-27 18:06:45

我每秒钟都有GPS数据进入我PC上的串行端口。我已经成功地处理了GPS数据,纬度和经度以浮点数的形式存储在单独的数组中。

double[] dlat = new double[100000]; //contains the latitude data
double[] dlon = new double[100000]; //contains the longitude data

大多数情况下,经纬度数字保持不变,因为GPS位置每5米才变化一次。当数组中的纬度或经度值发生变化时,我希望程序根据变化之间存储的数据点的平均值来预测纬度或经度。例如:

假设这是latitude数组的内容:

2,2,2,2,2,17

我希望我的程序将数组中的内容更改为:

2,5,8,11,14,17   

我已经尝试解决这个问题,但我的方法不起作用:-/我是c#的新手;一定有更好的办法。下面是我的代码片段,它试图做预测(---GPS coordinate prediction---之后的位是不工作的位):

string RxString;// where the raw serial data is stored
string mag;
double[] dmag = new double[100000];//magnetic data stored here
string lat;
double[] dlat = new double[100000];//latitude data stored here
string lon; 
double[] dlon = new double[100000];//longitude data stored here
double average;//average step between change in latiude
int i; //pointer double array data;
int count;//counter for prediction code
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//activates when port is open and data in buffer
{
    RxString = serialPort1.ReadTo("'r'n");//read raw data from serial port into string
    this.Invoke(new EventHandler(DisplayText));//invoke allows it to call function diplay text*/
    if(RxString.StartsWith("G"))
    {
       lat = RxString.Split(',')[0].Substring(4);// extract latitude
       this.Invoke(new EventHandler(DisplayText1));//invoke allows it to call function diplay text
       dlat[i] = Convert.ToDouble(lat);//convert and store in double array
       this.Invoke(new EventHandler(Form1_Load));//invoke allows it to call function 
       lon = RxString.Split(',')[2];// extract longitude
       this.Invoke(new EventHandler(DisplayText2));//invoke allows it to call function diplay text
       dlon[i] = Convert.ToDouble(lon);//covert and store in double array
       this.Invoke(new EventHandler(Form1_Load));//invoke allows it to call function 
       mag = RxString.Split(',')[3].Substring(6).Trim();// extract magnetic data 
       this.Invoke(new EventHandler(DisplayText3));//invoke allows it to call function diplay text
       dmag[i] = Convert.ToDouble(mag);//convert and store in double array
       this.Invoke(new EventHandler(Form1_Load));//invoke allows it to call function 
       i++;
       RxString = null;
        /* -------------------------GPS coordinate prediction--------------------------------------------- */
       if (i > 0)
       {
           if (dlat[i] == dlat[i - 1])
           {
               count++;
           }
           if (dlat[i] != dlat[i - 1])
           {
               double average = (dlat[i] - dlat[i - 1]) / (count);//average data step beween changed values
               int firstAv = i - (count - 1);//position of first average
               int lastAv = i - 1;//position of last average
               for (int j = firstAv; j <= lastAv; i++)
               {
                   dlat[j] = dlat[j - 1] + average;
               }
               count = 0;
           }
       }
       if (i==0) count = 1;
   }

基于平均值预测数据

以下作品:

    using System;
    using System.Text;
    namespace Practice
    {
        public class Hello
        {
            static double[] ldat = {2.0,2.0,2.00,2.0,2.0,17.0};
            static double[] ldat2 = {2.0,3.0,4.00,4.0,7.0,19.0};
            static double[] ldat3 = {0.0, 0.0, -5.0, -5.0, -11.0, -11.0, -20};
            public static void Main(string[] args)
            {
                test(ldat);
                test(ldat2);
                test(ldat3);
            }
            public static void test(double[] array){
                //Use Code from here.....
                int firstEqualIndex = -1;
                for(int i = 1; i < array.Length ; i ++)
                {
                    if (i > 0)
                    {
                        if(array[i] == array[i - 1])
                        {
                            if(firstEqualIndex == -1)
                            {
                                firstEqualIndex = i - 1;
                            }
                        }
                        else //They are not equal
                        {
                            //Figure out the average.
                            if(firstEqualIndex >= 0)
                            {
                                double average = (array[i] - array[firstEqualIndex]) / (Double)((i - firstEqualIndex));
                                int k = 0;
                                for(int j = firstEqualIndex; j < i; j++)
                                {
                                    array[j] += average * k;
                                    k++;
                                }
                                firstEqualIndex = -1;
                            }
                        }
                    }
                }
                //..... to here.
                StringBuilder builder = new StringBuilder();
                foreach (double entry in array)
                {
                    // Append each int to the StringBuilder overload.
                    builder.Append(entry).Append(", ");
                }
                string result = builder.ToString();
                Console.WriteLine(result);
            }
        }
    }      

该测试输出

2, 5, 8, 11, 14, 17, 
2, 3, 4, 5.5, 7, 19,
0, -2.5, -5, -8, -11, -15.5, -20,  

对所有的编辑表示抱歉,我正试图确保该方法与额外的测试用例一起工作。

EDIT:添加一个阴性案例的测试

我会从信号处理的角度来阐述这个问题。因此,如果您有一个信号f(t)(例如,它可能是您的离散latitude数组),您希望创建一个由

定义的新信号g(t)
g(t) = E[f(z) | t-0.5*w <= z <= t+0.5*w]

其中E表示期望值(或平均值),w表示过滤器的宽度。

以这种方式建模问题的一个好处是,你有一个非常具体的方式来指定你的运动模型。也就是说,你将如何转换数据[0,0,0,0,1,1,1,1]?

应该是[0,0,0,1/3,2/3,1,1,1]吗?

或应该是[0,1/7,2/7,3/7,4/7,5/7,6/7,1]吗?

给定您知道采样之间的时间间隔,您可以选择指定您想要的模型的w持续时间。

另一个好处是,如果你想要一个非线性运动模型,你也可以很容易地扩展到它。在我上面给出的例子中,我使用了一个框过滤器来进行平滑,但你可以使用其他东西来考虑你所追踪的物体的加速/减速的物理限制。一个形状更像高斯曲线的过滤器可以做到这一点。