eDSP  0.0.1
A cross-platform DSP library written in C++.
snr.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 * Filename: snr.hpp
19 * Author: Mohammed Boujemaoui
20 * Date: 10/10/18
21 */
22 
23 #ifndef EDSP_SNR_HPP
24 #define EDSP_SNR_HPP
25 
26 #include <edsp/meta/iterator.hpp>
29 #include <vector>
30 
31 namespace edsp { namespace statistics {
32 
51  template <typename InputIt, typename Allocator = std::allocator<meta::value_type_t<InputIt>>>
52  constexpr meta::value_type_t<InputIt> snr(InputIt first1, InputIt last1, InputIt first2) {
53  using value_type = meta::value_type_t<InputIt>;
54  const auto N = std::distance(first1, last1);
55  std::vector<value_type, Allocator> noise(N);
56  std::transform(first1, last1, first2, std::begin(noise),
57  [](const value_type left, const value_type right) { return left - right; });
58 
59  const auto var_ref = statistics::variance(first1, first2);
60  const auto var_noise = statistics::variance(std::cbegin(noise), std::cend(noise));
61  return converter::pow2db(var_ref / var_noise);
62  }
63 
64 }} // namespace edsp::statistics
65 #endif //EDSP_SNR_HPP
constexpr meta::value_type_t< ForwardIt > variance(ForwardIt first, ForwardIt last)
Computes the variance of the range [first, last)
Definition: variance.hpp:44
constexpr meta::value_type_t< InputIt > snr(InputIt first1, InputIt last1, InputIt first2)
Compute the SNR of the signals in the range [first1, last1) and the signal starting in first2...
Definition: snr.hpp:52
constexpr T distance(T x, T y) noexcept
Computes the distance between x and y.
Definition: numeric.hpp:328
Definition: amplifier.hpp:29
constexpr T pow2db(T power) noexcept
Convert power to decibels.
Definition: pow2db.hpp:41