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

Last change on this file since 10249 was 10249, checked in by stuerze, 3 months ago

minor changes

  • Property svn:keywords set to Author Date Id Rev URL;svn:eol-style=native
  • Property svn:mime-type set to text/plain
File size: 16.4 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 const double maxSolGap = 60.0;
52 bool setNeuNoiseToZero = false;
53
54 if (!_parlist) {
55 _parlist = new t_pppParlist();
56 }
57
58 // Vector of all Observations
59 // --------------------------
60 t_pppObsPool::t_epoch *epoch = obsPool->lastEpoch();
61 if (!epoch) {
62 return failure;
63 }
64 vector<t_pppSatObs*> &allObs = epoch->obsVector();
65
66 // Time of the Epoch
67 // -----------------
68 _epoTime = epoch->epoTime();
69
70 if (!_firstEpoTime.valid() ||
71 !_lastEpoTimeOK.valid()||
72 (maxSolGap > 0.0 && _epoTime - _lastEpoTimeOK > maxSolGap)) {
73 _firstEpoTime = _epoTime;
74 }
75
76 string epoTimeStr = string(_epoTime);
77
78 const QList<char> &usedSystems = _parlist->usedSystems();
79
80 // Set Parameters
81 // --------------
82 if (_parlist->set(_epoTime, allObs) != success) {
83 return failure;
84 }
85
86 // Status Vector, Variance-Covariance Matrix
87 // -----------------------------------------
88 _xFltOld = _xFlt;
89 _QFltOld = _QFlt;
90 setStateVectorAndVarCovMatrix(setNeuNoiseToZero);
91
92 // Process Satellite Systems separately
93 // ------------------------------------
94 for (int iSys = 0; iSys < usedSystems.size(); iSys++) {
95 char sys = usedSystems[iSys];
96 unsigned int num = 0;
97 vector<t_pppSatObs*> obsVector;
98 for (unsigned jj = 0; jj < allObs.size(); jj++) {
99 if (allObs[jj]->prn().system() == sys) {
100 obsVector.push_back(allObs[jj]);
101 num++;
102 }
103 }
104 LOG << epoTimeStr << " SATNUM " << sys << ' ' << right << setw(2) << num << endl;
105 if (processSystem(OPT->LCs(sys), obsVector, epoch->pseudoObsIono()) != success) {
106 LOG << "t_pppFilter::processSystem() != success: " << sys << endl;
107 return failure;
108 }
109 }
110
111 // close epoch processing
112 // ----------------------
113 cmpDOP(allObs);
114 _parlist->printResult(_epoTime, _QFlt, _xFlt);
115 _lastEpoTimeOK = _epoTime; // remember time of last successful epoch processing
116 return success;
117}
118
119// Process Selected LCs
120////////////////////////////////////////////////////////////////////////////
121t_irc t_pppFilter::processSystem(const vector<t_lc::type> &LCs,
122 const vector<t_pppSatObs*> &obsVector,
123 bool pseudoObsIonoAvailable) {
124 LOG.setf(ios::fixed);
125
126 // Detect Cycle Slips
127 // ------------------
128 if (detectCycleSlips(LCs, obsVector) != success) {
129 return failure;
130 }
131
132 ColumnVector xSav = _xFlt;
133 SymmetricMatrix QSav = _QFlt;
134 string epoTimeStr = string(_epoTime);
135 const vector<t_pppParam*> &params = _parlist->params();
136 unsigned nPar = _parlist->nPar();
137
138 unsigned usedLCs = LCs.size();
139 if (OPT->_pseudoObsIono && !pseudoObsIonoAvailable) {
140 usedLCs -= 1; // GIM not used
141 }
142 // max Obs
143 unsigned maxObs = obsVector.size() * usedLCs;
144
145 // Outlier Detection Loop
146 // ----------------------
147 for (unsigned iOutlier = 0; iOutlier < maxObs; iOutlier++) {
148
149 if (iOutlier > 0) {
150 _xFlt = xSav;
151 _QFlt = QSav;
152 }
153
154 // First-Design Matrix, Terms Observed-Computed, Weight Matrix
155 // -----------------------------------------------------------
156 Matrix AA(maxObs, nPar);
157 ColumnVector ll(maxObs); ll = 0.0;
158 DiagonalMatrix PP(maxObs); PP = 0.0;
159
160 int iObs = -1;
161 vector<t_pppSatObs*> usedObs;
162 vector<t_lc::type> usedTypes;
163
164 // Real Observations
165 // =================
166 int nSat = 0;
167 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
168 t_pppSatObs *obs = obsVector[ii];
169 if (iOutlier == 0) {
170 obs->resetOutlier();
171 }
172 if (!obs->outlier()) {
173 nSat++;
174 for (unsigned jj = 0; jj < usedLCs; jj++) {
175 const t_lc::type tLC = LCs[jj];
176 if (tLC == t_lc::GIM) {
177 continue;
178 }
179 ++iObs;
180 usedObs.push_back(obs);
181 usedTypes.push_back(tLC);
182 for (unsigned iPar = 0; iPar < nPar; iPar++) {
183 const t_pppParam *par = params[iPar];
184 AA[iObs][iPar] = par->partial(_epoTime, obs, tLC);
185 }
186 ll[iObs] = obs->obsValue(tLC) - obs->cmpValue(tLC) - DotProduct(_x0, AA.Row(iObs + 1));
187 PP[iObs] = 1.0 / (obs->sigma(tLC) * obs->sigma(tLC));
188 }
189 }
190 }
191
192 // Check number of observations
193 // ----------------------------
194 if (!nSat) {
195 return failure;
196 }
197
198 // Pseudo Obs Iono
199 // ================
200 if (OPT->_pseudoObsIono && pseudoObsIonoAvailable) {
201 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
202 t_pppSatObs *obs = obsVector[ii];
203 if (!obs->outlier()) {
204 for (unsigned jj = 0; jj < usedLCs; jj++) {
205 const t_lc::type tLC = LCs[jj];
206 if (tLC == t_lc::GIM) {
207 ++iObs;
208 } else {
209 continue;
210 }
211 usedObs.push_back(obs);
212 usedTypes.push_back(tLC);
213 for (unsigned iPar = 0; iPar < nPar; iPar++) {
214 const t_pppParam *par = params[iPar];
215 AA[iObs][iPar] = par->partial(_epoTime, obs, tLC);
216 }
217 ll[iObs] = obs->obsValue(tLC) - obs->cmpValue(tLC) - DotProduct(_x0, AA.Row(iObs + 1));
218 PP[iObs] = 1.0 / (obs->sigma(tLC) * obs->sigma(tLC));
219 }
220 }
221 }
222 }
223
224 // Truncate matrices
225 // -----------------
226 AA = AA.Rows(1, iObs + 1);
227 ll = ll.Rows(1, iObs + 1);
228 PP = PP.SymSubMatrix(1, iObs + 1);
229
230 // Kalman update step
231 // ------------------
232 kalman(AA, ll, PP, _QFlt, _xFlt);
233
234 // Check Residuals
235 // ---------------
236 ColumnVector vv = AA * _xFlt - ll;
237 double maxOutlier = 0.0;
238 int maxOutlierIndex = -1;
239 t_lc::type maxOutlierLC = t_lc::dummy;
240 for (unsigned ii = 0; ii < usedObs.size(); ii++) {
241 const t_lc::type tLC = usedTypes[ii];
242 double res = fabs(vv[ii]);
243 if (res > usedObs[ii]->maxRes(tLC)) {
244 if (res > fabs(maxOutlier)) {
245 maxOutlier = vv[ii];
246 maxOutlierIndex = ii;
247 maxOutlierLC = tLC;
248 }
249 }
250 }
251
252 // Mark outlier or break outlier detection loop
253 // --------------------------------------------
254 if (maxOutlierIndex > -1) {
255 t_pppSatObs *obs = usedObs[maxOutlierIndex];
256 t_pppParam *par = 0;
257 LOG << epoTimeStr << " Outlier " << t_lc::toString(maxOutlierLC) << ' '
258 << obs->prn().toString() << ' ' << setw(8) << setprecision(4)
259 << maxOutlier << endl;
260 for (unsigned iPar = 0; iPar < nPar; iPar++) {
261 t_pppParam *hlp = params[iPar];
262 if (hlp->type() == t_pppParam::amb &&
263 hlp->prn() == obs->prn() &&
264 hlp->tLC() == usedTypes[maxOutlierIndex]) {
265 par = hlp;
266 }
267 }
268 if (par) {
269 //if (par->ambResetCandidate()) {
270 resetAmb(obs->prn(), obsVector, maxOutlierLC, &QSav, &xSav);
271 //}
272 //else {
273 // par->setAmbResetCandidate();
274 // obs->setOutlier();
275 //}
276 }
277 else {
278 obs->setOutlier();
279 }
280 }
281 // Print Residuals
282 // ---------------
283 else {
284 for (unsigned jj = 0; jj < LCs.size(); jj++) {
285 for (unsigned ii = 0; ii < usedObs.size(); ii++) {
286 const t_lc::type tLC = usedTypes[ii];
287 t_pppSatObs *obs = usedObs[ii];
288 if (tLC == LCs[jj]) {
289 obs->setRes(tLC, vv[ii]);
290 LOG << epoTimeStr << " RES " << left << setw(3)
291 << t_lc::toString(tLC) << right << ' '
292 << obs->prn().toString() << ' '
293 << setw(8) << setprecision(4) << vv[ii] << endl;
294 }
295 }
296 }
297 break;
298 }
299 }
300 return success;
301}
302
303// Cycle-Slip Detection
304////////////////////////////////////////////////////////////////////////////
305t_irc t_pppFilter::detectCycleSlips(const vector<t_lc::type> &LCs,
306 const vector<t_pppSatObs*> &obsVector) {
307
308 const double SLIP = 20.0;
309 string epoTimeStr = string(_epoTime);
310 const vector<t_pppParam*> &params = _parlist->params();
311
312 for (unsigned ii = 0; ii < LCs.size(); ii++) {
313 const t_lc::type &tLC = LCs[ii];
314 if (t_lc::includesPhase(tLC)) {
315 for (unsigned iObs = 0; iObs < obsVector.size(); iObs++) {
316 const t_pppSatObs *obs = obsVector[iObs];
317
318 // Check set Slips and Jump Counters
319 // ---------------------------------
320 bool slip = false;
321
322 if (obs->slip()) {
323 LOG << epoTimeStr << "cycle slip set (obs) " << obs->prn().toString() << endl;
324 slip = true;
325 }
326
327 if (_slips[obs->prn()]._obsSlipCounter != -1 &&
328 _slips[obs->prn()]._obsSlipCounter != obs->slipCounter()) {
329 LOG << epoTimeStr << "cycle slip set (obsSlipCounter) " << obs->prn().toString() << endl;
330 slip = true;
331 }
332 _slips[obs->prn()]._obsSlipCounter = obs->slipCounter();
333
334 if (_slips[obs->prn()]._biasJumpCounter != -1 &&
335 _slips[obs->prn()]._biasJumpCounter != obs->biasJumpCounter()) {
336 LOG << epoTimeStr << "cycle slip set (biasJumpCounter) " << obs->prn().toString() << endl;
337 slip = true;
338 }
339 _slips[obs->prn()]._biasJumpCounter = obs->biasJumpCounter();
340
341 // Slip Set
342 // --------
343 if (slip) {
344 resetAmb(obs->prn(), obsVector, tLC);
345 }
346
347 // Check Pre-Fit Residuals
348 // -----------------------
349 else {
350 ColumnVector AA(params.size());
351 for (unsigned iPar = 0; iPar < params.size(); iPar++) {
352 const t_pppParam* par = params[iPar];
353 AA[iPar] = par->partial(_epoTime, obs, tLC);
354 }
355
356 double ll = obs->obsValue(tLC) - obs->cmpValue(tLC) - DotProduct(_x0, AA);
357 double vv = DotProduct(AA, _xFlt) - ll;
358
359 if (fabs(vv) > SLIP) {
360 LOG << epoTimeStr << " cycle slip detected " << t_lc::toString(tLC) << ' '
361 << obs->prn().toString() << ' ' << setw(8) << setprecision(4) << vv << endl;
362 resetAmb(obs->prn(), obsVector, tLC);
363 }
364 }
365 }
366 }
367 }
368
369 return success;
370}
371
372// Reset Ambiguity Parameter (cycle slip)
373////////////////////////////////////////////////////////////////////////////
374t_irc t_pppFilter::resetAmb(const t_prn prn, const vector<t_pppSatObs*> &obsVector, t_lc::type lc,
375 SymmetricMatrix *QSav, ColumnVector *xSav) {
376
377 t_irc irc = failure;
378 vector<t_pppParam*>& params = _parlist->params();
379 for (unsigned iPar = 0; iPar < params.size(); iPar++) {
380 t_pppParam *par = params[iPar];
381 if (par->type() == t_pppParam::amb && par->prn() == prn) {
382 int ind = par->indexNew();
383 double eleSat = par->ambEleSat();
384 bncTime firstObsTime;
385 bncTime lastObsTime = par->lastObsTime();
386 (par->firstObsTime().undef()) ?
387 firstObsTime = lastObsTime : firstObsTime = par->firstObsTime();
388 t_lc::type tLC = par->tLC();
389 if (tLC != lc) {continue;}
390 LOG << string(_epoTime) << " RESET " << par->toString() << endl;
391 delete par; par = new t_pppParam(t_pppParam::amb, prn, tLC, &obsVector);
392 par->setIndex(ind);
393 par->setFirstObsTime(firstObsTime);
394 par->setLastObsTime(lastObsTime);
395 par->setAmbEleSat(eleSat);
396 params[iPar] = par;
397 for (unsigned ii = 1; ii <= params.size(); ii++) {
398 _QFlt(ii, ind + 1) = 0.0;
399 if (QSav) {
400 (*QSav)(ii, ind + 1) = 0.0;
401 }
402 }
403 _QFlt(ind + 1, ind + 1) = par->sigma0() * par->sigma0();
404 if (QSav) {
405 (*QSav)(ind + 1, ind + 1) = _QFlt(ind + 1, ind + 1);
406 }
407 _xFlt[ind] = 0.0;
408 if (xSav) {
409 (*xSav)[ind] = _xFlt[ind];
410 }
411 _x0[ind] = par->x0();
412 return success;
413 }
414 }
415
416 return irc;
417}
418
419// Adjust process noise of individual parameters
420////////////////////////////////////////////////////////////////////////////
421t_irc t_pppFilter::adjustNoise(t_pppParam::e_type parType, t_prn prn, double noise,
422 SymmetricMatrix *QSav) {
423
424 t_irc irc = failure;
425 vector<t_pppParam*>& params = _parlist->params();
426 for (unsigned iPar = 0; iPar < params.size(); iPar++) {
427 t_pppParam *par = params[iPar];
428 if (par->type() == parType && par->prn() == prn) {
429 int iOld = par->indexOld();
430 int iNew = par->indexNew();
431 LOG << string(_epoTime) << " ADJUSTNOISE " << prn.toString() << endl;
432 _QFlt[iNew][iNew] = _QFltOld[iOld][iOld] + noise * noise;
433 if (QSav) {
434 (*QSav)(iNew + 1, iNew + 1) = _QFlt(iNew + 1, iNew + 1);
435 }
436 return success;
437 }
438 }
439 return irc;
440}
441
442// Compute various DOP Values
443////////////////////////////////////////////////////////////////////////////
444void t_pppFilter::cmpDOP(const vector<t_pppSatObs*> &obsVector) {
445
446 _dop.reset();
447
448 try {
449 const unsigned numPar = 4;
450 Matrix AA(obsVector.size(), numPar);
451 _numSat = 0;
452 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
453 t_pppSatObs *obs = obsVector[ii];
454 if (obs->isValid() && !obs->outlier()) {
455 ++_numSat;
456 for (unsigned iPar = 0; iPar < numPar; iPar++) {
457 const t_pppParam* par = _parlist->params()[iPar];
458 AA[_numSat - 1][iPar] = par->partial(_epoTime, obs, t_lc::c1);
459 }
460 }
461 }
462 if (_numSat < 4) {
463 return;
464 }
465 AA = AA.Rows(1, _numSat);
466 SymmetricMatrix NN; NN << AA.t() * AA;
467 SymmetricMatrix QQ = NN.i();
468
469 _dop.H = sqrt(QQ(1, 1) + QQ(2, 2));
470 _dop.V = sqrt(QQ(3, 3));
471 _dop.P = sqrt(QQ(1, 1) + QQ(2, 2) + QQ(3, 3));
472 _dop.T = sqrt(QQ(4, 4));
473 _dop.G = sqrt(QQ(1, 1) + QQ(2, 2) + QQ(3, 3) + QQ(4, 4));
474 }
475 catch (...) {
476 }
477}
478
479//
480////////////////////////////////////////////////////////////////////////////
481void t_pppFilter::predictCovCrdPart(bool setNeuNoiseToZero) {
482
483 const vector<t_pppParam*>& params = _parlist->params();
484
485 if (params.size() < 3) {
486 return;
487 }
488
489 bool first = (params[0]->indexOld() < 0);
490
491 SymmetricMatrix Qneu(3); Qneu = 0.0;
492 for (unsigned ii = 0; ii < 3; ii++) {
493 const t_pppParam *par = params[ii];
494 if (first) {
495 Qneu[ii][ii] = par->sigma0() * par->sigma0();
496 }
497 else {
498 Qneu[ii][ii] = par->noise() * par->noise();
499 }
500 }
501
502 const t_pppStation *sta = PPP_CLIENT->staRover();
503 SymmetricMatrix Qxyz(3);
504 covariNEU_XYZ(Qneu, sta->ellApr().data(), Qxyz);
505
506 if (first) {
507 _QFlt.SymSubMatrix(1, 3) = Qxyz;
508 }
509 else {
510 double dt = _epoTime - _firstEpoTime;
511 if (dt < OPT->_seedingTime || setNeuNoiseToZero) {
512 _QFlt.SymSubMatrix(1, 3) = _QFltOld.SymSubMatrix(1, 3);
513 }
514 else {
515 _QFlt.SymSubMatrix(1, 3) = _QFltOld.SymSubMatrix(1, 3) + Qxyz;
516 }
517 }
518}
519
520//
521////////////////////////////////////////////////////////////////////////////
522void t_pppFilter::setStateVectorAndVarCovMatrix(bool setNeuNoiseToZero) {
523
524 const vector<t_pppParam*>& params = _parlist->params();
525 unsigned nPar = params.size();
526
527 _QFlt.ReSize(nPar); _QFlt = 0.0;
528 _xFlt.ReSize(nPar); _xFlt = 0.0;
529 _x0.ReSize(nPar); _x0 = 0.0;
530
531 for (unsigned ii = 0; ii < nPar; ii++) {
532 t_pppParam *par1 = params[ii];
533 if (_QFltOld.size() == 0) {
534 par1->resetIndex();
535 }
536 _x0[ii] = par1->x0();
537 int iOld = par1->indexOld();
538 if (iOld < 0) {
539 _QFlt[ii][ii] = par1->sigma0() * par1->sigma0(); // new parameter
540 } else {
541 _QFlt[ii][ii] = _QFltOld[iOld][iOld] + par1->noise() * par1->noise();
542 _xFlt[ii] = _xFltOld[iOld];
543 for (unsigned jj = 0; jj < ii; jj++) {
544 t_pppParam *par2 = params[jj];
545 int jOld = par2->indexOld();
546 if (jOld >= 0) {
547 _QFlt[ii][jj] = _QFltOld(iOld + 1, jOld + 1);
548 }
549 }
550 }
551 }
552 predictCovCrdPart(setNeuNoiseToZero);
553}
554
Note: See TracBrowser for help on using the repository browser.