根据平均值预测数据

时间:2013-04-01 16:27:01

标签: c# serial-port data-processing

我的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;
   }

2 个答案:

答案 0 :(得分: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,  

对于我尝试确保该方法适用于其他测试用例的所有编辑感到抱歉。

编辑:为否定案例添加了测试。

答案 1 :(得分:1)

我会在信号处理方面制定这个问题。因此,如果您有信号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持续时间来指定您想要的模型。

另一个好处是,如果你想要一个非线性运动模型,你也可以轻松扩展到它。在我上面给出的示例中,我使用了一个盒式滤镜来进行平滑处理,但是您可以使用其他东西来考虑您正在跟踪的任何加速度/减速度的物理限制。形状更像高斯曲线的滤波器可以实现这一目标。