我有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