我每秒都有GPS数据进入我电脑的串口。我已经成功地处理了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;
}发布于 2013-04-02 00:55:36
以下是工作原理:
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, 抱歉,所有的编辑,我正在努力确保该方法与其他测试用例一起工作。
编辑:添加了对负例的测试。
发布于 2013-04-02 01:51:02
我会从信号处理的角度来阐述这个问题。因此,如果您有一个信号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持续时间。
另一个好处是,如果你想要一个非线性运动模型,你也可以很容易地扩展到它。在我上面给出的例子中,我使用了一个盒子过滤器来进行平滑,但你可以使用其他东西来考虑你正在跟踪的任何东西的加速/减速的物理限制。一个形状更像高斯曲线的滤波器就可以做到这一点。
https://stackoverflow.com/questions/15747241
复制相似问题