signal_statistics.hpp 12 KB

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