source: ntrip/branches/BNC_2.12/src/PPP/pppFilter.cpp@ 9099

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