eDSP  0.0.1
A cross-platform DSP library written in C++.
cepstrum.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: cepstrum.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 27/7/2018
21  */
22 #ifndef EDSP_CEPSTRUM_HPP
23 #define EDSP_CEPSTRUM_HPP
24 
25 #include <edsp/spectral/dft.hpp>
26 #include <vector>
27 
28 namespace edsp { inline namespace spectral {
29 
43  template <typename InputIt, typename OutputIt, typename RAllocator = std::allocator<meta::value_type_t<InputIt>>,
44  typename CAllocator = std::allocator<std::complex<meta::value_type_t<OutputIt>>>>
45  inline void cepstrum(InputIt first, InputIt last, OutputIt d_first) {
46  meta::expects(std::distance(first, last) > 0, "Not expecting empty input");
47  using value_type = meta::value_type_t<InputIt>;
48  const auto size = std::distance(first, last);
49  const auto nfft = 2 * size;
50  fft_impl<value_type> fft_(nfft);
51  fft_impl<value_type> ifft_(nfft);
52 
53  std::vector<value_type, RAllocator> temp_input(nfft, static_cast<value_type>(0)), temp_output(nfft);
54  std::copy(first, last, std::begin(temp_input));
55 
56  std::vector<std::complex<value_type>, CAllocator> fft_data_(make_fft_size(nfft));
57  fft_.dft(meta::data(temp_input), meta::data(fft_data_));
58 
59  std::transform(std::cbegin(fft_data_), std::cend(fft_data_), std::begin(fft_data_),
60  [](const std::complex<value_type>& val) -> std::complex<value_type> {
61  return std::complex<value_type>(std::log(std::abs(val)), 0);
62  });
63 
64  ifft_.idft(meta::data(fft_data_), meta::data(temp_output));
65  ifft_.idft_scale(meta::data(temp_output));
66  std::copy(std::cbegin(temp_output), std::cbegin(temp_output) + size, d_first);
67  }
68 
69 }} // namespace edsp::spectral
70 
71 #endif // EDSP_CEPSTRUM_HPP
void cepstrum(InputIt first, InputIt last, OutputIt d_first)
Computes the cepstrum of the range [first, last) and stores the result in another range...
Definition: cepstrum.hpp:45
constexpr Integer make_fft_size(Integer real_size) noexcept
Computes the expected DFT size for a real-to-complex DFT transform.
Definition: dft.hpp:35
constexpr T distance(T x, T y) noexcept
Computes the distance between x and y.
Definition: numeric.hpp:328
Definition: amplifier.hpp:29