문제

I have GPS data entering a serial port on my PC every second. I have successfully processed the GPS data and the latitude and longitude are stored in separate arrays as floating point numbers.

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

Most of the time the latitude and longitude numbers remain the same as the GPS position only changes every 5 meters. When the either the latitude or longitude value in the arrays changes I want my program to predict based on averages the latitude or longitude for the the data points stored in between the changes. For example:

Let's say this is the contents of the latitude array:

2,2,2,2,2,17

I would want my program to change what is in the array to:

2,5,8,11,14,17   

I've tried tackling the problem but my method doesn't work :-/ I am new to C#; there must be a better way of doing this. Here is the snippet of my code that attempts to do the prediction (the bit after ---GPS coordinate prediction--- is the bit that doesn't work):

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;
   }
도움이 되었습니까?

해결책

The following works:

    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);
            }
        }
    }      

This test outputs

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

Sorry about all the edits I am trying to make sure that the method works with additional test cases.

EDIT: Added a test for negative case.

다른 팁

I would formulate this problem in terms of signal processing. So if you have a signal f(t) (which could be your discretized latitude array for example), you want to create a new signal g(t) defined by

g(t) = E[f(z) | t-0.5*w <= z <= t+0.5*w]

where E is denoting the expected value (or average) and w is the width of your filter.

One of the benefits of modeling the problem this way is that you have a very concrete way of specifying your movement model. That is, how are you going to transform the data [0, 0, 0, 0, 1, 1, 1, 1]?

Should it be [0, 0, 0, 1/3, 2/3, 1, 1, 1]?

Or should it be [0, 1/7, 2/7, 3/7, 4/7, 5/7, 6/7, 1]?

Given that you know how much time passes between your samples, you can choose a w duration that specifies the model you want.

Another benefit is that, if you want a nonlinear movement model, you can easily extend to that too. In the example I gave above, I used a box filter to do the smoothing, but you could use something else to take into account the physical limitations on the acceleration/deceleration of whatever it is you're tracking. A filter shaped more like a Gaussian curve could accomplish that.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top