Commit b8f187d466e9f626ab310069fac1ba1a5595a5e0
1 parent
2e05aaf7
Exists in
master
and in
69 other branches
2d in progress
Showing
1 changed file
with
180 additions
and
7 deletions
Show diff stats
src/ExternLib/DataFiltering/DataFiltering.hh
... | ... | @@ -49,8 +49,13 @@ namespace AMDA { |
49 | 49 | template <typename ElemType> |
50 | 50 | class FilteringContainer1D { |
51 | 51 | public: |
52 | - FilteringContainer1D(){ | |
53 | - } | |
52 | + | |
53 | + FilteringContainer1D() { | |
54 | + } | |
55 | + | |
56 | + virtual ~FilteringContainer1D() { | |
57 | + } | |
58 | + | |
54 | 59 | void clean() { |
55 | 60 | _size.clear(); |
56 | 61 | _nFilteredPoints.clear(); |
... | ... | @@ -84,6 +89,19 @@ namespace AMDA { |
84 | 89 | }; |
85 | 90 | |
86 | 91 | template <typename ElemType> |
92 | + class FilteringContainer2D : public FilteringContainer1D<ElemType> { | |
93 | + public: | |
94 | + | |
95 | + FilteringContainer2D() { | |
96 | + } | |
97 | + | |
98 | + virtual ~FilteringContainer2D() { | |
99 | + } | |
100 | + std::list<Tab2DData<ElemType> > _2Dvalues; | |
101 | + | |
102 | + }; | |
103 | + | |
104 | + template <typename ElemType> | |
87 | 105 | class DataFiltering : public Operation { |
88 | 106 | public: |
89 | 107 | // constructor |
... | ... | @@ -219,7 +237,7 @@ namespace AMDA { |
219 | 237 | _paramOutput(new ParamDataSpec<std::vector<ElemType> > ()), |
220 | 238 | _factor(factor), |
221 | 239 | _nPoints(nPoints), |
222 | - _container(new FilteringContainer1D<ElemType>()){ | |
240 | + _container(new FilteringContainer1D<ElemType>()) { | |
223 | 241 | _paramDataOutput = _paramOutput; |
224 | 242 | } |
225 | 243 | |
... | ... | @@ -237,10 +255,6 @@ namespace AMDA { |
237 | 255 | double crtTime = _paramInput.getTime(_index); |
238 | 256 | std::vector<ElemType> inputElt = _paramInput.get(_index); |
239 | 257 | |
240 | - _paramOutput->pushTime(crtTime); | |
241 | - _paramOutput->getDataList().push_back(inputElt); | |
242 | - | |
243 | - | |
244 | 258 | // initialiser et remplir container |
245 | 259 | if (*std::min_element(std::begin(_container->_size), std::end(_container->_size)) < _nPoints) { |
246 | 260 | |
... | ... | @@ -358,6 +372,165 @@ namespace AMDA { |
358 | 372 | */ |
359 | 373 | FilteringContainer1D<ElemType>* _container; |
360 | 374 | }; |
375 | + | |
376 | + template <typename ElemType> | |
377 | + class DataFiltering2D : public Operation { | |
378 | + public: | |
379 | + | |
380 | + DataFiltering2D(Process& pProcess, ParamDataSpec<Tab2DData<ElemType> >& paramInput, double factor = Default::_factor, int nPoints = Default::_nPoints) : | |
381 | + Operation(pProcess), | |
382 | + _paramInput(paramInput), | |
383 | + _paramOutput(new ParamDataSpec<Tab2DData<ElemType> > ()), | |
384 | + _factor(factor), | |
385 | + _nPoints(nPoints), | |
386 | + _container(new FilteringContainer2D<ElemType>()) { | |
387 | + _paramDataOutput = _paramOutput; | |
388 | + } | |
389 | + | |
390 | + virtual ~DataFiltering2D() { | |
391 | + } | |
392 | + | |
393 | + void write(ParamDataIndexInfo &pParamDataIndexInfo) { | |
394 | + // init container | |
395 | + int size1 = _paramInput.get(pParamDataIndexInfo._startIndex).getDim1Size(); | |
396 | + int size2 = _paramInput.get(pParamDataIndexInfo._startIndex).getDim2Size(); | |
397 | + int size = size1*size2; | |
398 | + _container->init(size); | |
399 | + | |
400 | + for (unsigned int _index = pParamDataIndexInfo._startIndex; | |
401 | + _index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess; | |
402 | + ++_index) { | |
403 | + double crtTime = _paramInput.getTime(_index); | |
404 | + Tab2DData<ElemType> inputElt = _paramInput.get(_index); | |
405 | + | |
406 | + // initialiser et remplir container | |
407 | + if (*std::min_element(std::begin(_container->_size), std::end(_container->_size)) < _nPoints) { | |
408 | + | |
409 | + _container->_values.push_back(inputElt); | |
410 | + _container->_times.push_back(crtTime); | |
411 | + for (int i = 0; i < size; i++) { | |
412 | + if (!isNAN(inputElt[i])) { | |
413 | + _container->_sum[i] += inputElt[i]; | |
414 | + _container->_sumsq[i] += inputElt[i] * inputElt[i]; | |
415 | + _container->_size[i] += 1; | |
416 | + } | |
417 | + } | |
418 | + } else { | |
419 | + for (int i = 0; i < size; i++) { | |
420 | + _container->_ave[i] = (ElemType) _container->_sum[i] / _container->_size[i]; | |
421 | + _container->_sig[i] = std::sqrt((_container->_sumsq[i] - _container->_sum[i] * _container->_sum[i] / _container->_size[i]) / _container->_size[i]); | |
422 | + } | |
423 | + | |
424 | + // filter Elements | |
425 | + for (auto elt2D = _container->_values.begin(); elt2D != _container->_values.end(); ++elt2D) { | |
426 | + int j = 0; | |
427 | + for (auto elt = elt2D->begin(); elt != elt2D->end(); ++elt) { | |
428 | + j = elt - elt2D->begin(); | |
429 | + int i = 0; | |
430 | + for (auto it = elt->begin(); it != elt->end(); ++it) { | |
431 | + i = (it - elt->begin()) * j; | |
432 | + ElemType crt_val = *it; | |
433 | + if (isNAN(crt_val)) { | |
434 | + continue; | |
435 | + } | |
436 | + | |
437 | + if (std::abs(crt_val - _container->_ave[i]) > _factor * _container->_sig[i]) { | |
438 | + _container->_sum[i] -= crt_val; | |
439 | + _container->_sumsq[i] -= crt_val*crt_val; | |
440 | + _container->_size[i] -= 1; | |
441 | + _container->_nFilteredPoints[i] += 1; | |
442 | + *it << NotANumber(); | |
443 | + } | |
444 | + } | |
445 | + } | |
446 | + } | |
447 | + _paramOutput->pushTime(_container->_times.front()); | |
448 | + _paramOutput->getDataList().push_back(_container->_values.front()); | |
449 | + _container->_values.pop_front(); | |
450 | + _container->_times.pop_front(); | |
451 | + for (int i = 0; i < size1; i++) { | |
452 | + for (int j = 0; j < size2; j++) { | |
453 | + if (!isNAN(_container->_values.front()[i][j])) { | |
454 | + _container->_size[i * j] -= 1; | |
455 | + _container->_sum[i * j] -= _container->_values.front()[i][j]; | |
456 | + _container->_sumsq[i * j] -= _container->_values.front()[i][j] * _container->_values.front()[i][j]; | |
457 | + } | |
458 | + if (!isNAN(inputElt[i][j])) { | |
459 | + _container->_sum[i * j] += inputElt[i * j]; | |
460 | + _container->_sumsq[i * j] += inputElt[i][j] * inputElt[i][j]; | |
461 | + _container->_size[i * j] += 1; | |
462 | + } | |
463 | + } | |
464 | + } | |
465 | + _container->_values.push_back(inputElt); | |
466 | + _container->_times.push_back(crtTime); | |
467 | + } | |
468 | + // last value filter and write | |
469 | + if (_index == pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess - 1) { | |
470 | + for (int i = 0; i < size; i++) { | |
471 | + _container->_ave[i] = (ElemType) _container->_sum[i] / _container->_size[i]; | |
472 | + _container->_sig[i] = std::sqrt((_container->_sumsq[i] - _container->_sum[i] * _container->_sum[i] / _container->_size[i]) / _container->_size[i]); | |
473 | + } | |
474 | + for (auto elt = _container->_values.begin(); elt != _container->_values.end(); ++elt) { | |
475 | + int i; | |
476 | + for (auto it = elt->begin(); it != elt->end(); ++it) { | |
477 | + i = it - elt->begin(); | |
478 | + ElemType crt_val = *it; | |
479 | + if (isNAN(crt_val)) { | |
480 | + continue; | |
481 | + } | |
482 | + | |
483 | + if (std::abs(crt_val - _container->_ave[i]) > _factor * _container->_sig[i]) { | |
484 | + _container->_sum[i] -= crt_val; | |
485 | + _container->_sumsq[i] -= crt_val*crt_val; | |
486 | + _container->_size[i] -= 1; | |
487 | + _container->_nFilteredPoints[i] += 1; | |
488 | + *it << NotANumber(); | |
489 | + } | |
490 | + } | |
491 | + } | |
492 | + | |
493 | + std::list<double>::iterator itTimes = _container->_times.begin(); | |
494 | + for (auto elt : _container->_values) { | |
495 | + _paramOutput->pushTime(*itTimes); | |
496 | + _paramOutput->getDataList().push_back(elt); | |
497 | + ++itTimes; | |
498 | + } | |
499 | + } | |
500 | + | |
501 | + } | |
502 | + } | |
503 | + private: | |
504 | + /* | |
505 | + * input data | |
506 | + */ | |
507 | + | |
508 | + | |
509 | + ParamDataSpec<std::vector<ElemType > >& _paramInput; | |
510 | + | |
511 | + /* | |
512 | + * oupout filtered data | |
513 | + */ | |
514 | + ParamDataSpec<std::vector<ElemType> >* _paramOutput; | |
515 | + | |
516 | + /* | |
517 | + * factor in term of sigma used in the filtering | |
518 | + */ | |
519 | + double _factor; | |
520 | + | |
521 | + /* | |
522 | + Number of point used in the fitering algorithm | |
523 | + */ | |
524 | + int _nPoints; | |
525 | + | |
526 | + /* | |
527 | + * Data container used during filtering | |
528 | + */ | |
529 | + FilteringContainer2D<ElemType>* _container; | |
530 | + }; | |
531 | + | |
532 | + | |
533 | + | |
361 | 534 | } |
362 | 535 | } |
363 | 536 | } | ... | ... |