eDSP  0.0.1
A cross-platform DSP library written in C++.
centroid.hpp
Go to the documentation of this file.
1 /*
2  * eDSP, A cross-platform Digital Signal Processing library written in modern C++.
3  * Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi, All rights reserved.
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation, either version 3 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along width
16  * this program. If not, see <http://www.gnu.org/licenses/>
17  *
18  * File: centroid.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 17/6/2018
21  */
22 #ifndef EDSP_STATISTICAL_CENTROID_HPP
23 #define EDSP_STATISTICAL_CENTROID_HPP
24 
25 #include <edsp/meta/iterator.hpp>
26 
27 namespace edsp { namespace statistics {
28 
42  template <typename ForwardIt>
43  constexpr meta::value_type_t<ForwardIt> centroid(ForwardIt first, ForwardIt last) {
44  using input_t = meta::value_type_t<ForwardIt>;
45  auto weighted_sum = static_cast<input_t>(0);
46  auto unweighted_sum = static_cast<input_t>(0);
47  for (input_t i = 0; first != last; ++first, ++i) {
48  weighted_sum += i * (*first);
49  unweighted_sum += *first;
50  }
51  return weighted_sum / unweighted_sum;
52  }
53 
68  template <typename ForwardIt>
69  constexpr meta::value_type_t<ForwardIt> centroid(ForwardIt first1, ForwardIt last1, ForwardIt first2) {
70  using input_t = meta::value_type_t<ForwardIt>;
71  auto weighted_sum = static_cast<input_t>(0);
72  auto unweighted_sum = static_cast<input_t>(0);
73  for (; first1 != last1; ++first1, ++first2) {
74  weighted_sum += (*first2) * (*first1);
75  unweighted_sum += *first1;
76  }
77  return weighted_sum / unweighted_sum;
78  }
79 
80 }} // namespace edsp::statistics
81 
82 #endif // EDSP_STATISTICAL_CENTROID_HPP
Definition: amplifier.hpp:29
constexpr meta::value_type_t< ForwardIt > centroid(ForwardIt first, ForwardIt last)
Computes the centroid value of the range [first, last)
Definition: centroid.hpp:43