我有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;
}
感谢这工作得很好:-)但是有一个问题。因为我的数组设置是这样的:'double [] dmag = new double [100000];'在我的纬度或经度数据后面有空值,这会使平均值变差。有没有一种方法可以将数组设置为在添加新数据时变得更大,以便在数组的空位中不存在空值? –
我解决了使用ArrayList解决了问题:-) –
太棒了!对不起,我没有看到你的评论早些时候我会建议。 – nattyddubbs