eDSP  0.0.1
A cross-platform DSP library written in C++.
mu_law_compressor.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: mu_law_compressor.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 7/9/2018
21  */
22 #ifndef EDSP_MU_LAW_COMPRESSOR_HPP
23 #define EDSP_MU_LAW_COMPRESSOR_HPP
24 
25 #include <edsp/meta/expects.hpp>
26 #include <edsp/math/numeric.hpp>
27 #include <edsp/meta/iterator.hpp>
28 #include <algorithm>
29 #include <functional>
30 
31 namespace edsp { namespace quantizer {
32 
47  template <typename InputIt, typename OutputIt>
48  constexpr void mu_law_compression(InputIt first, InputIt last, OutputIt d_first,
49  meta::value_type_t<InputIt> absolute_max_value,
50  meta::diff_type_t<InputIt> compression_factor) {
51  meta::expects(absolute_max_value > 0, "Expected a maximum absolute value higher than zero");
52  meta::expects(compression_factor > 0, "The compression factor should be a positive number");
53  using value_type = meta::value_type_t<InputIt>;
54  const auto lambda = [absolute_max_value](const value_type input) -> value_type {
55  const auto ratio = std::fabs(input) / absolute_max_value;
56  return math::sign(input) * absolute_max_value *
57  std::log(static_cast<value_type>(1 + compression_factor * ratio)) /
58  std::log(static_cast<value_type>(1 + compression_factor));
59  };
60  std::transform(first, last, d_first, std::cref(lambda));
61  }
62 
79  template <typename InputIt, typename OutputIt>
80  constexpr void inverse_mu_law_compression(InputIt first, InputIt last, OutputIt d_first,
81  meta::value_type_t<InputIt> absolute_max_value,
82  meta::diff_type_t<InputIt> compression_factor) {
83  meta::expects(absolute_max_value > 0, "Expected a maximum absolute value higher than zero");
84  meta::expects(compression_factor > 0, "The compression factor should be a positive number");
85  using value_type = meta::value_type_t<InputIt>;
86  const auto lambda = [absolute_max_value](const value_type input) -> value_type {
87  const auto ratio = std::fabs(input) / absolute_max_value;
88  return math::sign(input) * absolute_max_value *
89  (std::pow(static_cast<value_type>(1 + compression_factor), ratio) - 1) /
90  std::log(static_cast<value_type>(1 + compression_factor));
91  };
92  std::transform(first, last, d_first, std::cref(lambda));
93  }
94 
95 }} // namespace edsp::quantizer
96 
97 #endif // EDSP_MU_LAW_COMPRESSOR_HPP
constexpr void inverse_mu_law_compression(InputIt first, InputIt last, OutputIt d_first, meta::value_type_t< InputIt > absolute_max_value, meta::diff_type_t< InputIt > compression_factor)
The law converts data in 8-bit µ-law format to 16-bit linear formats.
Definition: mu_law_compressor.hpp:80
constexpr T sign(T x) noexcept
Determines the sign of the input number.
Definition: numeric.hpp:155
Definition: amplifier.hpp:29
constexpr void mu_law_compression(InputIt first, InputIt last, OutputIt d_first, meta::value_type_t< InputIt > absolute_max_value, meta::diff_type_t< InputIt > compression_factor)
The law converts data in 16-bit linear formats to a 8-bit µ-law format.
Definition: mu_law_compressor.hpp:48