source: ntrip/trunk/BNC/src/PPP/pppFilter.cpp@ 6107

Last change on this file since 6107 was 6107, checked in by mervart, 8 years ago
File size: 13.0 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: t_pppFilter
6 *
7 * Purpose: Filter Adjustment
8 *
9 * Author: L. Mervart
10 *
11 * Created: 29-Jul-2014
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <iostream>
18#include <iomanip>
19#include <cmath>
20#include <newmat.h>
21#include <newmatio.h>
22#include <newmatap.h>
23
24#include "pppFilter.h"
25#include "bncutils.h"
26#include "pppParlist.h"
27#include "pppObsPool.h"
28#include "pppStation.h"
29#include "pppClient.h"
30
31using namespace BNC_PPP;
32using namespace std;
33
34// Constructor
35////////////////////////////////////////////////////////////////////////////
36t_pppFilter::t_pppFilter() {
37 _parlist = 0;
38}
39
40// Destructor
41////////////////////////////////////////////////////////////////////////////
42t_pppFilter::~t_pppFilter() {
43 delete _parlist;
44}
45
46// Process Single Epoch
47////////////////////////////////////////////////////////////////////////////
48t_irc t_pppFilter::processEpoch(t_pppObsPool* obsPool) {
49
50 _numSat = 0;
51
52 if (!_parlist) {
53 _parlist = new t_pppParlist();
54 }
55
56 // Vector of all Observations
57 // --------------------------
58 t_pppObsPool::t_epoch* epoch = obsPool->lastEpoch();
59 if (!epoch) {
60 return failure;
61 }
62 vector<t_pppSatObs*>& allObs = epoch->obsVector();
63
64 // Time of the Epoch
65 // -----------------
66 _epoTime = epoch->epoTime();
67
68 if (!_firstEpoTime.valid()) {
69 _firstEpoTime = _epoTime;
70 }
71
72 // Set Parameters
73 // --------------
74 _parlist->set(_epoTime, allObs);
75 const vector<t_pppParam*>& params = _parlist->params();
76
77 // Status Vector, Variance-Covariance Matrix
78 // -----------------------------------------
79 ColumnVector xFltOld = _xFlt;
80 SymmetricMatrix QFltOld = _QFlt;
81
82 _QFlt.ReSize(_parlist->nPar()); _QFlt = 0.0;
83 _xFlt.ReSize(_parlist->nPar()); _xFlt = 0.0;
84 _x0.ReSize(_parlist->nPar()); _x0 = 0.0;
85
86 for (unsigned ii = 0; ii < params.size(); ii++) {
87 const t_pppParam* par1 = params[ii];
88
89 _x0[ii] = par1->x0();
90
91 int iOld = par1->indexOld();
92 if (iOld < 0) {
93 _QFlt[ii][ii] = par1->sigma0() * par1->sigma0(); // new parameter
94 }
95 else {
96 _QFlt[ii][ii] = QFltOld[iOld][iOld] + par1->noise() * par1->noise();
97 _xFlt[ii] = xFltOld[iOld];
98 for (unsigned jj = 0; jj < ii; jj++) {
99 const t_pppParam* par2 = params[jj];
100 int jOld = par2->indexOld();
101 if (jOld >= 0) {
102 _QFlt[ii][jj] = QFltOld(iOld+1,jOld+1);
103 }
104 }
105 }
106 }
107
108 predictCovCrdPart(QFltOld);
109
110 // Process Satellite Systems separately
111 // ------------------------------------
112 for (unsigned iSys = 0; iSys < OPT->systems().size(); iSys++) {
113 char system = OPT->systems()[iSys];
114 vector<t_pppSatObs*> obsVector;
115 for (unsigned jj = 0; jj < allObs.size(); jj++) {
116 if (allObs[jj]->prn().system() == system) {
117 obsVector.push_back(allObs[jj]);
118 }
119 }
120 if ( processSystem(OPT->LCs(system), obsVector) != success ) {
121 return failure;
122 }
123 }
124
125 cmpDOP(allObs);
126
127 _parlist->printResult(_epoTime, _QFlt, _xFlt);
128
129 return success;
130}
131
132// Process Selected LCs
133////////////////////////////////////////////////////////////////////////////
134t_irc t_pppFilter::processSystem(const vector<t_lc::type>& LCs,
135 const vector<t_pppSatObs*>& obsVector) {
136
137 LOG.setf(ios::fixed);
138
139 // Detect Cycle Slips
140 // ------------------
141 if (detectCycleSlips(LCs, obsVector) != success) {
142 return failure;
143 }
144
145 ColumnVector xSav = _xFlt;
146 SymmetricMatrix QSav = _QFlt;
147 string epoTimeStr = string(_epoTime);
148 const vector<t_pppParam*>& params = _parlist->params();
149 unsigned maxObs = obsVector.size() * LCs.size();
150
151 // Outlier Detection Loop
152 // ----------------------
153 for (unsigned iOutlier = 0; iOutlier < maxObs; iOutlier++) {
154
155 if (iOutlier > 0) {
156 _xFlt = xSav;
157 _QFlt = QSav;
158 }
159
160 // First-Design Matrix, Terms Observed-Computed, Weight Matrix
161 // -----------------------------------------------------------
162 Matrix AA(maxObs, _parlist->nPar());
163 ColumnVector ll(maxObs);
164 DiagonalMatrix PP(maxObs); PP = 0.0;
165
166 int iObs = -1;
167 vector<t_pppSatObs*> usedObs;
168 vector<t_lc::type> usedTypes;
169 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
170 t_pppSatObs* obs = obsVector[ii];
171 if (!obs->outlier()) {
172 for (unsigned jj = 0; jj < LCs.size(); jj++) {
173 const t_lc::type tLC = LCs[jj];
174 ++iObs;
175 usedObs.push_back(obs);
176 usedTypes.push_back(tLC);
177 for (unsigned iPar = 0; iPar < params.size(); iPar++) {
178 const t_pppParam* par = params[iPar];
179 AA[iObs][iPar] = par->partial(_epoTime, obs, tLC);
180 }
181 ll[iObs] = obs->obsValue(tLC) - obs->cmpValue(tLC) - DotProduct(_x0, AA.Row(iObs+1));
182 PP[iObs] = 1.0 / (obs->sigma(tLC) * obs->sigma(tLC));
183 }
184 }
185 }
186
187 // Check number of observations, truncate matrices
188 // -----------------------------------------------
189 if (iObs == -1) {
190 return failure;
191 }
192 AA = AA.Rows(1, iObs+1);
193 ll = ll.Rows(1, iObs+1);
194 PP = PP.SymSubMatrix(1, iObs+1);
195
196 // Kalman update step
197 // ------------------
198 kalman(AA, ll, PP, _QFlt, _xFlt);
199
200 // Check Residuals
201 // ---------------
202 ColumnVector vv = AA * _xFlt - ll;
203 double maxOutlier = 0.0;
204 int maxOutlierIndex = -1;
205 t_lc::type maxOutlierLC = t_lc::dummy;
206 for (unsigned ii = 0; ii < usedObs.size(); ii++) {
207 const t_lc::type tLC = usedTypes[ii];
208 double res = fabs(vv[ii]);
209 if (res > usedObs[ii]->maxRes(tLC)) {
210 if (res > fabs(maxOutlier)) {
211 maxOutlier = vv[ii];
212 maxOutlierIndex = ii;
213 maxOutlierLC = tLC;
214 }
215 }
216 }
217
218 // Mark outlier or break outlier detection loop
219 // --------------------------------------------
220 if (maxOutlierIndex > -1) {
221 t_pppSatObs* obs = usedObs[maxOutlierIndex];
222 t_pppParam* par = 0;
223 LOG << epoTimeStr << " Outlier " << t_lc::toString(maxOutlierLC) << ' '
224 << obs->prn().toString() << ' '
225 << setw(8) << setprecision(4) << maxOutlier << endl;
226 for (unsigned iPar = 0; iPar < params.size(); iPar++) {
227 t_pppParam* hlp = params[iPar];
228 if (hlp->type() == t_pppParam::amb && hlp->prn() == obs->prn() &&
229 hlp->tLC() == usedTypes[maxOutlierIndex]) {
230 par = hlp;
231 }
232 }
233 if (par) {
234 if (par->ambResetCandidate()) {
235 resetAmb(par->prn(), obsVector, &QSav, &xSav);
236 }
237 else {
238 par->setAmbResetCandidate();
239 obs->setOutlier();
240 }
241 }
242 else {
243 obs->setOutlier();
244 }
245 }
246
247 // Print Residuals
248 // ---------------
249 else {
250 for (unsigned jj = 0; jj < LCs.size(); jj++) {
251 for (unsigned ii = 0; ii < usedObs.size(); ii++) {
252 const t_lc::type tLC = usedTypes[ii];
253 t_pppSatObs* obs = usedObs[ii];
254 if (tLC == LCs[jj]) {
255 obs->setRes(tLC, vv[ii]);
256 LOG << epoTimeStr << " RES "
257 << left << setw(3) << t_lc::toString(tLC) << right << ' '
258 << obs->prn().toString() << ' '
259 << setw(8) << setprecision(4) << vv[ii] << endl;
260 }
261 }
262 }
263 break;
264 }
265 }
266
267 return success;
268}
269
270// Cycle-Slip Detection
271////////////////////////////////////////////////////////////////////////////
272t_irc t_pppFilter::detectCycleSlips(const vector<t_lc::type>& LCs,
273 const vector<t_pppSatObs*>& obsVector) {
274
275 const double SLIP = 20.0; // slip threshold
276 string epoTimeStr = string(_epoTime);
277 const vector<t_pppParam*>& params = _parlist->params();
278
279 for (unsigned ii = 0; ii < LCs.size(); ii++) {
280 const t_lc::type& tLC = LCs[ii];
281 if (t_lc::includesPhase(tLC)) {
282 for (unsigned iObs = 0; iObs < obsVector.size(); iObs++) {
283 const t_pppSatObs* obs = obsVector[iObs];
284
285 // Check set Slips and Jump Counters
286 // ---------------------------------
287 bool slip = false;
288
289 if (obs->slip()) {
290 LOG << "cycle slip set (obs)" << endl;;
291 slip = true;
292 }
293
294 if (_slips[obs->prn()]._obsSlipCounter != -1 &&
295 _slips[obs->prn()]._obsSlipCounter != obs->slipCounter()) {
296 LOG << "cycle slip set (obsSlipCounter)" << endl;
297 slip = true;
298 }
299 _slips[obs->prn()]._obsSlipCounter = obs->slipCounter();
300
301 if (_slips[obs->prn()]._biasJumpCounter != -1 &&
302 _slips[obs->prn()]._biasJumpCounter != obs->biasJumpCounter()) {
303 LOG << "cycle slip set (biasJumpCounter)" << endl;
304 slip = true;
305 }
306 _slips[obs->prn()]._biasJumpCounter = obs->biasJumpCounter();
307
308 // Slip Set
309 // --------
310 if (slip) {
311 resetAmb(obs->prn(), obsVector);
312 }
313
314 // Check Pre-Fit Residuals
315 // -----------------------
316 else {
317 ColumnVector AA(params.size());
318 for (unsigned iPar = 0; iPar < params.size(); iPar++) {
319 const t_pppParam* par = params[iPar];
320 AA[iPar] = par->partial(_epoTime, obs, tLC);
321 }
322
323 double ll = obs->obsValue(tLC) - obs->cmpValue(tLC) - DotProduct(_x0, AA);
324 double vv = DotProduct(AA, _xFlt) - ll;
325
326 if (fabs(vv) > SLIP) {
327 LOG << epoTimeStr << " cycle slip detected " << t_lc::toString(tLC) << ' '
328 << obs->prn().toString() << ' ' << setw(8) << setprecision(4) << vv << endl;
329 resetAmb(obs->prn(), obsVector);
330 }
331 }
332 }
333 }
334 }
335
336 return success;
337}
338
339// Reset Ambiguity Parameter (cycle slip)
340////////////////////////////////////////////////////////////////////////////
341t_irc t_pppFilter::resetAmb(t_prn prn, const vector<t_pppSatObs*>& obsVector,
342 SymmetricMatrix* QSav, ColumnVector* xSav) {
343 t_irc irc = failure;
344 vector<t_pppParam*>& params = _parlist->params();
345 for (unsigned iPar = 0; iPar < params.size(); iPar++) {
346 t_pppParam* par = params[iPar];
347 if (par->type() == t_pppParam::amb && par->prn() == prn) {
348 int ind = par->indexNew();
349 t_lc::type tLC = par->tLC();
350 LOG << string(_epoTime) << " RESET " << par->toString() << endl;
351 delete par; par = new t_pppParam(t_pppParam::amb, prn, tLC, &obsVector);
352 par->setIndex(ind);
353 params[iPar] = par;
354 for (unsigned ii = 1; ii <= params.size(); ii++) {
355 _QFlt(ii, ind+1) = 0.0;
356 if (QSav) {
357 (*QSav)(ii, ind+1) = 0.0;
358 }
359 }
360 _QFlt(ind+1,ind+1) = par->sigma0() * par->sigma0();
361 if (QSav) {
362 (*QSav)(ind+1,ind+1) = _QFlt(ind+1,ind+1);
363 }
364 _xFlt[ind] = 0.0;
365 if (xSav) {
366 (*xSav)[ind] = _xFlt[ind];
367 }
368 _x0[ind] = par->x0();
369 irc = success;
370 }
371 }
372
373 return irc;
374}
375
376// Compute various DOP Values
377////////////////////////////////////////////////////////////////////////////
378void t_pppFilter::cmpDOP(const vector<t_pppSatObs*>& obsVector) {
379
380 _dop.reset();
381
382 try {
383 const unsigned numPar = 4;
384 Matrix AA(obsVector.size(), numPar);
385 _numSat = 0;
386 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
387 t_pppSatObs* obs = obsVector[ii];
388 if (obs->isValid() && !obs->outlier()) {
389 ++_numSat;
390 for (unsigned iPar = 0; iPar < numPar; iPar++) {
391 const t_pppParam* par = _parlist->params()[iPar];
392 AA[_numSat-1][iPar] = par->partial(_epoTime, obs, t_lc::c1);
393 }
394 }
395 }
396 if (_numSat < 4) {
397 return;
398 }
399 AA = AA.Rows(1, _numSat);
400 SymmetricMatrix NN; NN << AA.t() * AA;
401 SymmetricMatrix QQ = NN.i();
402
403 _dop.P = sqrt(QQ(1,1) + QQ(2,2) + QQ(3,3));
404 _dop.T = sqrt(QQ(4,4));
405 _dop.G = sqrt(QQ(1,1) + QQ(2,2) + QQ(3,3) + QQ(4,4));
406 }
407 catch (...) {
408 }
409}
410
411// Compute various DOP Values
412////////////////////////////////////////////////////////////////////////////
413void t_pppFilter::predictCovCrdPart(const SymmetricMatrix& QFltOld) {
414
415 const vector<t_pppParam*>& params = _parlist->params();
416 if (params.size() < 3) {
417 return;
418 }
419
420 bool first = (params[0]->indexOld() < 0);
421
422 SymmetricMatrix Qneu(3); Qneu = 0.0;
423 for (unsigned ii = 0; ii < 3; ii++) {
424 const t_pppParam* par = params[ii];
425 if (first) {
426 Qneu[ii][ii] = par->sigma0() * par->sigma0();
427 }
428 else {
429 Qneu[ii][ii] = par->noise() * par->noise();
430 }
431 }
432
433 const t_pppStation* sta = PPP_CLIENT->staRover();
434 SymmetricMatrix Qxyz(3);
435 covariNEU_XYZ(Qneu, sta->ellApr().data(), Qxyz);
436
437 if (first) {
438 _QFlt.SymSubMatrix(1,3) = Qxyz;
439 }
440 else {
441 double dt = _epoTime - _firstEpoTime;
442 if (dt < OPT->_seedingTime) {
443 _QFlt.SymSubMatrix(1,3) = QFltOld.SymSubMatrix(1,3);
444 }
445 else {
446 _QFlt.SymSubMatrix(1,3) = QFltOld.SymSubMatrix(1,3) + Qxyz;
447 }
448 }
449}
Note: See TracBrowser for help on using the repository browser.