DP3
Median.h
Go to the documentation of this file.
1 // Copyright (C) 2022 ASTRON (Netherlands Institute for Radio Astronomy)
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 
4 #ifndef COMMON_MEDIAN_H
5 #define COMMON_MEDIAN_H
6 
7 #include <algorithm>
8 #include <vector>
9 
10 namespace dp3 {
11 namespace common {
12 
16 template <class T>
17 T Median(std::vector<T>& v) {
18  if (v.empty()) {
19  return static_cast<T>(0);
20  }
21  auto target = v.begin() + v.size() / 2;
22  std::nth_element(v.begin(), target, v.end());
23  if ((v.size() % 2) == 1) {
24  return *target;
25  } else {
26  auto before = target - 1;
27  std::nth_element(v.begin(), before, v.end());
28  return (*target + *before) / static_cast<T>(2);
29  }
30 }
31 
32 } // namespace common
33 } // namespace dp3
34 
35 #endif
T Median(std::vector< T > &v)
Definition: Median.h:17
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53