signal_statistics.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // (C) Copyright Nick Thompson 2018.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_TOOLS_SIGNAL_STATISTICS_HPP
  6. #define BOOST_MATH_TOOLS_SIGNAL_STATISTICS_HPP
  7. #include <algorithm>
  8. #include <iterator>
  9. #include <boost/math/tools/assert.hpp>
  10. #include <boost/math/tools/complex.hpp>
  11. #include <boost/math/tools/roots.hpp>
  12. #include <boost/math/statistics/univariate_statistics.hpp>
  13. #include <boost/math/tools/is_standalone.hpp>
  14. #ifndef BOOST_MATH_STANDALONE
  15. #include <boost/config.hpp>
  16. #ifdef BOOST_MATH_NO_CXX17_IF_CONSTEXPR
  17. #error "The header <boost/math/norms.hpp> can only be used in C++17 and later."
  18. #endif
  19. #endif
  20. namespace boost::math::statistics {
  21. template<class ForwardIterator>
  22. auto absolute_gini_coefficient(ForwardIterator first, ForwardIterator last)
  23. {
  24. using std::abs;
  25. using RealOrComplex = typename std::iterator_traits<ForwardIterator>::value_type;
  26. BOOST_MATH_ASSERT_MSG(first != last && std::next(first) != last, "Computation of the Gini coefficient requires at least two samples.");
  27. std::sort(first, last, [](RealOrComplex a, RealOrComplex b) { return abs(b) > abs(a); });
  28. decltype(abs(*first)) i = 1;
  29. decltype(abs(*first)) num = 0;
  30. decltype(abs(*first)) denom = 0;
  31. for (auto it = first; it != last; ++it)
  32. {
  33. decltype(abs(*first)) tmp = abs(*it);
  34. num += tmp*i;
  35. denom += tmp;
  36. ++i;
  37. }
  38. // If the l1 norm is zero, all elements are zero, so every element is the same.
  39. if (denom == 0)
  40. {
  41. decltype(abs(*first)) zero = 0;
  42. return zero;
  43. }
  44. return ((2*num)/denom - i)/(i-1);
  45. }
  46. template<class RandomAccessContainer>
  47. inline auto absolute_gini_coefficient(RandomAccessContainer & v)
  48. {
  49. return boost::math::statistics::absolute_gini_coefficient(v.begin(), v.end());
  50. }
  51. template<class ForwardIterator>
  52. auto sample_absolute_gini_coefficient(ForwardIterator first, ForwardIterator last)
  53. {
  54. size_t n = std::distance(first, last);
  55. return n*boost::math::statistics::absolute_gini_coefficient(first, last)/(n-1);
  56. }
  57. template<class RandomAccessContainer>
  58. inline auto sample_absolute_gini_coefficient(RandomAccessContainer & v)
  59. {
  60. return boost::math::statistics::sample_absolute_gini_coefficient(v.begin(), v.end());
  61. }
  62. // The Hoyer sparsity measure is defined in:
  63. // https://arxiv.org/pdf/0811.4706.pdf
  64. template<class ForwardIterator>
  65. auto hoyer_sparsity(const ForwardIterator first, const ForwardIterator last)
  66. {
  67. using T = typename std::iterator_traits<ForwardIterator>::value_type;
  68. using std::abs;
  69. using std::sqrt;
  70. BOOST_MATH_ASSERT_MSG(first != last && std::next(first) != last, "Computation of the Hoyer sparsity requires at least two samples.");
  71. if constexpr (std::is_unsigned<T>::value)
  72. {
  73. T l1 = 0;
  74. T l2 = 0;
  75. size_t n = 0;
  76. for (auto it = first; it != last; ++it)
  77. {
  78. l1 += *it;
  79. l2 += (*it)*(*it);
  80. n += 1;
  81. }
  82. double rootn = sqrt(n);
  83. return (rootn - l1/sqrt(l2) )/ (rootn - 1);
  84. }
  85. else {
  86. decltype(abs(*first)) l1 = 0;
  87. decltype(abs(*first)) l2 = 0;
  88. // We wouldn't need to count the elements if it was a random access iterator,
  89. // but our only constraint is that it's a forward iterator.
  90. size_t n = 0;
  91. for (auto it = first; it != last; ++it)
  92. {
  93. decltype(abs(*first)) tmp = abs(*it);
  94. l1 += tmp;
  95. l2 += tmp*tmp;
  96. n += 1;
  97. }
  98. if constexpr (std::is_integral<T>::value)
  99. {
  100. double rootn = sqrt(n);
  101. return (rootn - l1/sqrt(l2) )/ (rootn - 1);
  102. }
  103. else
  104. {
  105. decltype(abs(*first)) rootn = sqrt(static_cast<decltype(abs(*first))>(n));
  106. return (rootn - l1/sqrt(l2) )/ (rootn - 1);
  107. }
  108. }
  109. }
  110. template<class Container>
  111. inline auto hoyer_sparsity(Container const & v)
  112. {
  113. return boost::math::statistics::hoyer_sparsity(v.cbegin(), v.cend());
  114. }
  115. template<class Container>
  116. auto oracle_snr(Container const & signal, Container const & noisy_signal)
  117. {
  118. using Real = typename Container::value_type;
  119. BOOST_MATH_ASSERT_MSG(signal.size() == noisy_signal.size(),
  120. "Signal and noisy_signal must be have the same number of elements.");
  121. if constexpr (std::is_integral<Real>::value)
  122. {
  123. double numerator = 0;
  124. double denominator = 0;
  125. for (size_t i = 0; i < signal.size(); ++i)
  126. {
  127. numerator += signal[i]*signal[i];
  128. denominator += (noisy_signal[i] - signal[i])*(noisy_signal[i] - signal[i]);
  129. }
  130. if (numerator == 0 && denominator == 0)
  131. {
  132. return std::numeric_limits<double>::quiet_NaN();
  133. }
  134. if (denominator == 0)
  135. {
  136. return std::numeric_limits<double>::infinity();
  137. }
  138. return numerator/denominator;
  139. }
  140. else if constexpr (boost::math::tools::is_complex_type<Real>::value)
  141. {
  142. using std::norm;
  143. typename Real::value_type numerator = 0;
  144. typename Real::value_type denominator = 0;
  145. for (size_t i = 0; i < signal.size(); ++i)
  146. {
  147. numerator += norm(signal[i]);
  148. denominator += norm(noisy_signal[i] - signal[i]);
  149. }
  150. if (numerator == 0 && denominator == 0)
  151. {
  152. return std::numeric_limits<typename Real::value_type>::quiet_NaN();
  153. }
  154. if (denominator == 0)
  155. {
  156. return std::numeric_limits<typename Real::value_type>::infinity();
  157. }
  158. return numerator/denominator;
  159. }
  160. else
  161. {
  162. Real numerator = 0;
  163. Real denominator = 0;
  164. for (size_t i = 0; i < signal.size(); ++i)
  165. {
  166. numerator += signal[i]*signal[i];
  167. denominator += (signal[i] - noisy_signal[i])*(signal[i] - noisy_signal[i]);
  168. }
  169. if (numerator == 0 && denominator == 0)
  170. {
  171. return std::numeric_limits<Real>::quiet_NaN();
  172. }
  173. if (denominator == 0)
  174. {
  175. return std::numeric_limits<Real>::infinity();
  176. }
  177. return numerator/denominator;
  178. }
  179. }
  180. template<class Container>
  181. auto mean_invariant_oracle_snr(Container const & signal, Container const & noisy_signal)
  182. {
  183. using Real = typename Container::value_type;
  184. BOOST_MATH_ASSERT_MSG(signal.size() == noisy_signal.size(), "Signal and noisy signal must be have the same number of elements.");
  185. Real mu = boost::math::statistics::mean(signal);
  186. Real numerator = 0;
  187. Real denominator = 0;
  188. for (size_t i = 0; i < signal.size(); ++i)
  189. {
  190. Real tmp = signal[i] - mu;
  191. numerator += tmp*tmp;
  192. denominator += (signal[i] - noisy_signal[i])*(signal[i] - noisy_signal[i]);
  193. }
  194. if (numerator == 0 && denominator == 0)
  195. {
  196. return std::numeric_limits<Real>::quiet_NaN();
  197. }
  198. if (denominator == 0)
  199. {
  200. return std::numeric_limits<Real>::infinity();
  201. }
  202. return numerator/denominator;
  203. }
  204. template<class Container>
  205. auto mean_invariant_oracle_snr_db(Container const & signal, Container const & noisy_signal)
  206. {
  207. using std::log10;
  208. return 10*log10(boost::math::statistics::mean_invariant_oracle_snr(signal, noisy_signal));
  209. }
  210. // Follows the definition of SNR given in Mallat, A Wavelet Tour of Signal Processing, equation 11.16.
  211. template<class Container>
  212. auto oracle_snr_db(Container const & signal, Container const & noisy_signal)
  213. {
  214. using std::log10;
  215. return 10*log10(boost::math::statistics::oracle_snr(signal, noisy_signal));
  216. }
  217. // A good reference on the M2M4 estimator:
  218. // D. R. Pauluzzi and N. C. Beaulieu, "A comparison of SNR estimation techniques for the AWGN channel," IEEE Trans. Communications, Vol. 48, No. 10, pp. 1681-1691, 2000.
  219. // A nice python implementation:
  220. // https://github.com/gnuradio/gnuradio/blob/master/gr-digital/examples/snr_estimators.py
  221. template<class ForwardIterator>
  222. auto m2m4_snr_estimator(ForwardIterator first, ForwardIterator last, decltype(*first) estimated_signal_kurtosis=1, decltype(*first) estimated_noise_kurtosis=3)
  223. {
  224. BOOST_MATH_ASSERT_MSG(estimated_signal_kurtosis > 0, "The estimated signal kurtosis must be positive");
  225. BOOST_MATH_ASSERT_MSG(estimated_noise_kurtosis > 0, "The estimated noise kurtosis must be positive.");
  226. using Real = typename std::iterator_traits<ForwardIterator>::value_type;
  227. using std::sqrt;
  228. if constexpr (std::is_floating_point<Real>::value || std::numeric_limits<Real>::max_exponent)
  229. {
  230. // If we first eliminate N, we obtain the quadratic equation:
  231. // (ka+kw-6)S^2 + 2M2(3-kw)S + kw*M2^2 - M4 = 0 =: a*S^2 + bs*N + cs = 0
  232. // If we first eliminate S, we obtain the quadratic equation:
  233. // (ka+kw-6)N^2 + 2M2(3-ka)N + ka*M2^2 - M4 = 0 =: a*N^2 + bn*N + cn = 0
  234. // I believe these equations are totally independent quadratics;
  235. // if one has a complex solution it is not necessarily the case that the other must also.
  236. // However, I can't prove that, so there is a chance that this does unnecessary work.
  237. // Future improvements: There are algorithms which can solve quadratics much more effectively than the naive implementation found here.
  238. // See: https://stackoverflow.com/questions/48979861/numerically-stable-method-for-solving-quadratic-equations/50065711#50065711
  239. auto [M1, M2, M3, M4] = boost::math::statistics::first_four_moments(first, last);
  240. if (M4 == 0)
  241. {
  242. // The signal is constant. There is no noise:
  243. return std::numeric_limits<Real>::infinity();
  244. }
  245. // Change to notation in Pauluzzi, equation 41:
  246. auto kw = estimated_noise_kurtosis;
  247. auto ka = estimated_signal_kurtosis;
  248. // A common case, since it's the default:
  249. Real a = (ka+kw-6);
  250. Real bs = 2*M2*(3-kw);
  251. Real cs = kw*M2*M2 - M4;
  252. Real bn = 2*M2*(3-ka);
  253. Real cn = ka*M2*M2 - M4;
  254. auto [S0, S1] = boost::math::tools::quadratic_roots(a, bs, cs);
  255. if (S1 > 0)
  256. {
  257. auto N = M2 - S1;
  258. if (N > 0)
  259. {
  260. return S1/N;
  261. }
  262. if (S0 > 0)
  263. {
  264. N = M2 - S0;
  265. if (N > 0)
  266. {
  267. return S0/N;
  268. }
  269. }
  270. }
  271. auto [N0, N1] = boost::math::tools::quadratic_roots(a, bn, cn);
  272. if (N1 > 0)
  273. {
  274. auto S = M2 - N1;
  275. if (S > 0)
  276. {
  277. return S/N1;
  278. }
  279. if (N0 > 0)
  280. {
  281. S = M2 - N0;
  282. if (S > 0)
  283. {
  284. return S/N0;
  285. }
  286. }
  287. }
  288. // This happens distressingly often. It's a limitation of the method.
  289. return std::numeric_limits<Real>::quiet_NaN();
  290. }
  291. else
  292. {
  293. BOOST_MATH_ASSERT_MSG(false, "The M2M4 estimator has not been implemented for this type.");
  294. return std::numeric_limits<Real>::quiet_NaN();
  295. }
  296. }
  297. template<class Container>
  298. inline auto m2m4_snr_estimator(Container const & noisy_signal, typename Container::value_type estimated_signal_kurtosis=1, typename Container::value_type estimated_noise_kurtosis=3)
  299. {
  300. return m2m4_snr_estimator(noisy_signal.cbegin(), noisy_signal.cend(), estimated_signal_kurtosis, estimated_noise_kurtosis);
  301. }
  302. template<class ForwardIterator>
  303. inline auto m2m4_snr_estimator_db(ForwardIterator first, ForwardIterator last, decltype(*first) estimated_signal_kurtosis=1, decltype(*first) estimated_noise_kurtosis=3)
  304. {
  305. using std::log10;
  306. return 10*log10(m2m4_snr_estimator(first, last, estimated_signal_kurtosis, estimated_noise_kurtosis));
  307. }
  308. template<class Container>
  309. inline auto m2m4_snr_estimator_db(Container const & noisy_signal, typename Container::value_type estimated_signal_kurtosis=1, typename Container::value_type estimated_noise_kurtosis=3)
  310. {
  311. using std::log10;
  312. return 10*log10(m2m4_snr_estimator(noisy_signal, estimated_signal_kurtosis, estimated_noise_kurtosis));
  313. }
  314. }
  315. #endif