eDSP  0.0.1
A cross-platform DSP library written in C++.
periodogram.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: periodogram.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 27/7/2018
21  */
22 #ifndef EDSP_SPECTROGRAM_HPP
23 #define EDSP_SPECTROGRAM_HPP
24 
25 #include <edsp/spectral/dft.hpp>
27 #include <edsp/math/numeric.hpp>
28 #include <vector>
29 
30 namespace edsp { inline namespace spectral {
31 
36  enum class SpectralScale {
37  Linear,
39  };
40 
41 
60  template <typename InputIt, typename OutputIt,
61  typename Allocator = std::allocator<std::complex<meta::value_type_t<OutputIt>>>>
62  inline void periodogram(InputIt first, InputIt last, OutputIt d_first, SpectralScale scale) {
63  meta::expects(std::distance(first, last) > 0, "Not expecting empty input");
64  using value_type = meta::value_type_t<InputIt>;
65  const auto size = std::distance(first, last);
66  fft_impl<value_type> fft_{size};
67  std::vector<std::complex<value_type>, Allocator> fft_data_(make_fft_size(size));
68  fft_.dft(&(*first), meta::data(fft_data_));
69  if (scale == SpectralScale::Linear) {
70  std::transform(std::cbegin(fft_data_), std::cend(fft_data_), d_first,
71  [](const std::complex<value_type>& val) -> meta::value_type_t<OutputIt> {
72  return math::square(std::abs(val));
73  });
74  } else {
75  std::transform(std::cbegin(fft_data_), std::cend(fft_data_), d_first,
76  [](const std::complex<value_type>& val) -> meta::value_type_t<OutputIt> {
77  return converter::mag2db(std::abs(val));
78  });
79  }
80  }
81 
82 }} // namespace edsp::spectral
83 
84 #endif // EDSP_SPECTROGRAM_HPP
constexpr T mag2db(T magnitude) noexcept
Convert magnitude to decibels (dB)
Definition: mag2db.hpp:41
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
SpectralScale
The SpectralScale enum represent the scale used to represent the power spectral density.
Definition: periodogram.hpp:36
void periodogram(InputIt first, InputIt last, OutputIt d_first, SpectralScale scale)
Computes the periodogram of the range [first, last) and stores the result in another range...
Definition: periodogram.hpp:62
constexpr T distance(T x, T y) noexcept
Computes the distance between x and y.
Definition: numeric.hpp:328
constexpr T square(T x)
Computes the square value of the input number.
Definition: numeric.hpp:188
Definition: amplifier.hpp:29