signal_statistics.hpp 12 KB

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