eDSP  0.0.1
A cross-platform DSP library written in C++.
moment.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: moment.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 2018-06-13
21  */
22 #ifndef EDSP_STATISTICAL_MOMENT_H
23 #define EDSP_STATISTICAL_MOMENT_H
24 
25 #include <edsp/statistics/mean.hpp>
26 #include <edsp/meta/iterator.hpp>
27 #include <numeric>
28 
29 namespace edsp { namespace statistics {
30 
31  namespace internal {
32 
33  // TODO: update this internal
34  template <int N, class T>
35  constexpr T nthPower(T x) {
36  T ret = x;
37  for (int i = 1; i < N; ++i) {
38  ret *= x;
39  }
40  return ret;
41  }
42 
43  template <class T, int N>
44  struct SumDiffNthPower {
45  explicit SumDiffNthPower(T x) : mean_(x) {}
46  constexpr T operator()(T sum, T current) {
47  return sum + nthPower<N>(current - mean_);
48  }
49  T mean_;
50  };
51 
52  template <class T, int N, class Iter_T>
53  T nthMoment(Iter_T first, Iter_T last, T mean) {
54  const auto cnt = std::distance(first, last);
55  return std::accumulate(first, last, T(), SumDiffNthPower<T, N>(mean)) /
56  static_cast<meta::value_type_t<Iter_T>>(cnt);
57  }
58 
59  } // namespace internal
60 
69  template <std::size_t N, typename ForwardIt>
70  constexpr meta::value_type_t<ForwardIt> moment(ForwardIt first, ForwardIt last) {
71  using input_t = meta::value_type_t<ForwardIt>;
72  const auto m = mean(first, last);
73  return internal::nthMoment<input_t, N>(first, last, m);
74  }
75 
86  template <std::size_t N, typename ForwardIt>
87  constexpr meta::value_type_t<ForwardIt> moment(ForwardIt first, ForwardIt last,
88  const meta::value_type_t<ForwardIt> mean) {
89  using input_t = meta::value_type_t<ForwardIt>;
90  return internal::nthMoment<input_t, N>(first, last, mean);
91  }
92 
93 }} // namespace edsp::statistics
94 
95 #endif // EDSP_STATISTICAL_MOMMENT_H
constexpr meta::value_type_t< ForwardIt > moment(ForwardIt first, ForwardIt last)
Computes the n-th moment of the range [first, last)
Definition: moment.hpp:70
constexpr T distance(T x, T y) noexcept
Computes the distance between x and y.
Definition: numeric.hpp:328
Definition: amplifier.hpp:29
constexpr meta::value_type_t< ForwardIt > mean(ForwardIt first, ForwardIt last)
Computes the average or mean value of the range [first, last)
Definition: mean.hpp:44