Commit 2e05aaf70f15af9896caa7e46486eca685a7f642
1 parent
79c1e322
Exists in
master
and in
69 other branches
param 1d ok
Showing
2 changed files
with
174 additions
and
7 deletions
Show diff stats
src/ExternLib/DataFiltering/DataFiltering.hh
... | ... | @@ -35,7 +35,6 @@ namespace AMDA { |
35 | 35 | |
36 | 36 | template <typename ElemType> |
37 | 37 | struct FilteringContainer { |
38 | - ~FilteringContainer(){} | |
39 | 38 | // size of real points(not nan and not spike) |
40 | 39 | int _size = 0; |
41 | 40 | int _nFilteredPoints = 0; |
... | ... | @@ -48,10 +47,30 @@ namespace AMDA { |
48 | 47 | }; |
49 | 48 | |
50 | 49 | template <typename ElemType> |
51 | - struct FilteringContainer1D { | |
52 | - FilteringContainer1D(){} | |
53 | - ~FilteringContainer1D(){ | |
54 | - delete &_size; | |
50 | + class FilteringContainer1D { | |
51 | + public: | |
52 | + FilteringContainer1D(){ | |
53 | + } | |
54 | + void clean() { | |
55 | + _size.clear(); | |
56 | + _nFilteredPoints.clear(); | |
57 | + _sum.clear(); | |
58 | + _sumsq.clear(); | |
59 | + _ave.clear(); | |
60 | + _sig.clear(); | |
61 | + _values.clear(); | |
62 | + _times.clear(); | |
63 | + } | |
64 | + | |
65 | + void init(int size) { | |
66 | + std::vector<int> intVect(size, 0); | |
67 | + std::vector<ElemType> eltVect(size, 0); | |
68 | + _size = intVect; | |
69 | + _nFilteredPoints = intVect; | |
70 | + _sum = eltVect; | |
71 | + _sumsq = eltVect; | |
72 | + _ave = eltVect; | |
73 | + _sig = eltVect; | |
55 | 74 | } |
56 | 75 | // size of real points(not nan and not spike) |
57 | 76 | std::vector<int> _size; |
... | ... | @@ -190,7 +209,155 @@ namespace AMDA { |
190 | 209 | |
191 | 210 | }; |
192 | 211 | |
193 | - | |
212 | + template <typename ElemType> | |
213 | + class DataFiltering1D : public Operation { | |
214 | + public: | |
215 | + | |
216 | + DataFiltering1D(Process& pProcess, ParamDataSpec<std::vector<ElemType> >& paramInput, double factor = Default::_factor, int nPoints = Default::_nPoints) : | |
217 | + Operation(pProcess), | |
218 | + _paramInput(paramInput), | |
219 | + _paramOutput(new ParamDataSpec<std::vector<ElemType> > ()), | |
220 | + _factor(factor), | |
221 | + _nPoints(nPoints), | |
222 | + _container(new FilteringContainer1D<ElemType>()){ | |
223 | + _paramDataOutput = _paramOutput; | |
224 | + } | |
225 | + | |
226 | + virtual ~DataFiltering1D() { | |
227 | + } | |
228 | + | |
229 | + void write(ParamDataIndexInfo &pParamDataIndexInfo) { | |
230 | + // init container | |
231 | + int size = _paramInput.get(pParamDataIndexInfo._startIndex).size(); | |
232 | + _container->init(size); | |
233 | + | |
234 | + for (unsigned int _index = pParamDataIndexInfo._startIndex; | |
235 | + _index < pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess; | |
236 | + ++_index) { | |
237 | + double crtTime = _paramInput.getTime(_index); | |
238 | + std::vector<ElemType> inputElt = _paramInput.get(_index); | |
239 | + | |
240 | + _paramOutput->pushTime(crtTime); | |
241 | + _paramOutput->getDataList().push_back(inputElt); | |
242 | + | |
243 | + | |
244 | + // initialiser et remplir container | |
245 | + if (*std::min_element(std::begin(_container->_size), std::end(_container->_size)) < _nPoints) { | |
246 | + | |
247 | + _container->_values.push_back(inputElt); | |
248 | + _container->_times.push_back(crtTime); | |
249 | + for (int i = 0; i < size; i++) { | |
250 | + if (!isNAN(inputElt[i])) { | |
251 | + _container->_sum[i] += inputElt[i]; | |
252 | + _container->_sumsq[i] += inputElt[i] * inputElt[i]; | |
253 | + _container->_size[i] += 1; | |
254 | + } | |
255 | + } | |
256 | + } else { | |
257 | + for (int i = 0; i < size; i++) { | |
258 | + _container->_ave[i] = (ElemType) _container->_sum[i] / _container->_size[i]; | |
259 | + _container->_sig[i] = std::sqrt((_container->_sumsq[i] - _container->_sum[i] * _container->_sum[i] / _container->_size[i]) / _container->_size[i]); | |
260 | + } | |
261 | + | |
262 | + // filter Elements | |
263 | + for (auto elt = _container->_values.begin(); elt != _container->_values.end(); ++elt) { | |
264 | + int i; | |
265 | + for (auto it = elt->begin(); it != elt->end(); ++it) { | |
266 | + i = it - elt->begin(); | |
267 | + ElemType crt_val = *it; | |
268 | + if (isNAN(crt_val)) { | |
269 | + continue; | |
270 | + } | |
271 | + | |
272 | + if (std::abs(crt_val - _container->_ave[i]) > _factor * _container->_sig[i]) { | |
273 | + _container->_sum[i] -= crt_val; | |
274 | + _container->_sumsq[i] -= crt_val*crt_val; | |
275 | + _container->_size[i] -= 1; | |
276 | + _container->_nFilteredPoints[i] += 1; | |
277 | + *it << NotANumber(); | |
278 | + } | |
279 | + } | |
280 | + } | |
281 | + _paramOutput->pushTime(_container->_times.front()); | |
282 | + _paramOutput->getDataList().push_back(_container->_values.front()); | |
283 | + _container->_values.pop_front(); | |
284 | + _container->_times.pop_front(); | |
285 | + for (int i = 0; i < size; i++) { | |
286 | + if (!isNAN(_container->_values.front()[i])) { | |
287 | + _container->_size[i] -= 1; | |
288 | + _container->_sum[i] -= _container->_values.front()[i]; | |
289 | + _container->_sumsq[i] -= _container->_values.front()[i] * _container->_values.front()[i]; | |
290 | + } | |
291 | + if (!isNAN(inputElt[i])) { | |
292 | + _container->_sum[i] += inputElt[i]; | |
293 | + _container->_sumsq[i] += inputElt[i] * inputElt[i]; | |
294 | + _container->_size[i] += 1; | |
295 | + } | |
296 | + } | |
297 | + _container->_values.push_back(inputElt); | |
298 | + _container->_times.push_back(crtTime); | |
299 | + } | |
300 | + // last value filter and write | |
301 | + if (_index == pParamDataIndexInfo._startIndex + pParamDataIndexInfo._nbDataToProcess - 1) { | |
302 | + for (int i = 0; i < size; i++) { | |
303 | + _container->_ave[i] = (ElemType) _container->_sum[i] / _container->_size[i]; | |
304 | + _container->_sig[i] = std::sqrt((_container->_sumsq[i] - _container->_sum[i] * _container->_sum[i] / _container->_size[i]) / _container->_size[i]); | |
305 | + } | |
306 | + for (auto elt = _container->_values.begin(); elt != _container->_values.end(); ++elt) { | |
307 | + int i; | |
308 | + for (auto it = elt->begin(); it != elt->end(); ++it) { | |
309 | + i = it - elt->begin(); | |
310 | + ElemType crt_val = *it; | |
311 | + if (isNAN(crt_val)) { | |
312 | + continue; | |
313 | + } | |
314 | + | |
315 | + if (std::abs(crt_val - _container->_ave[i]) > _factor * _container->_sig[i]) { | |
316 | + _container->_sum[i] -= crt_val; | |
317 | + _container->_sumsq[i] -= crt_val*crt_val; | |
318 | + _container->_size[i] -= 1; | |
319 | + _container->_nFilteredPoints[i] += 1; | |
320 | + *it << NotANumber(); | |
321 | + } | |
322 | + } | |
323 | + } | |
324 | + | |
325 | + std::list<double>::iterator itTimes = _container->_times.begin(); | |
326 | + for (auto elt : _container->_values) { | |
327 | + _paramOutput->pushTime(*itTimes); | |
328 | + _paramOutput->getDataList().push_back(elt); | |
329 | + ++itTimes; | |
330 | + } | |
331 | + } | |
332 | + | |
333 | + } | |
334 | + } | |
335 | + private: | |
336 | + /** | |
337 | + * input data | |
338 | + */ | |
339 | + ParamDataSpec<std::vector<ElemType > >& _paramInput; | |
340 | + | |
341 | + /** | |
342 | + * oupout filtered data | |
343 | + */ | |
344 | + ParamDataSpec<std::vector<ElemType> >* _paramOutput; | |
345 | + | |
346 | + /** | |
347 | + * factor in term of sigma used in the filtering | |
348 | + */ | |
349 | + double _factor; | |
350 | + | |
351 | + /** | |
352 | + Number of point used in the fitering algorithm | |
353 | + */ | |
354 | + int _nPoints; | |
355 | + | |
356 | + /** | |
357 | + * Data container used during filtering | |
358 | + */ | |
359 | + FilteringContainer1D<ElemType>* _container; | |
360 | + }; | |
194 | 361 | } |
195 | 362 | } |
196 | 363 | } | ... | ... |
src/ExternLib/DataFiltering/DataFilteringCreator.hh
... | ... | @@ -173,7 +173,7 @@ namespace AMDA { |
173 | 173 | } |
174 | 174 | template <typename Type> |
175 | 175 | void create1DOperation() { |
176 | - | |
176 | + _operation = new DataFiltering::DataFiltering1D<Type>(_process, dynamic_cast<ParamDataSpec<std::vector<Type> > &> (_paramData), _factor, _nPoints); | |
177 | 177 | } |
178 | 178 | |
179 | 179 | Process &_process; | ... | ... |