eDSP  0.0.1
A cross-platform DSP library written in C++.
sinusoidal.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: sine.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 27/7/2018
21  */
22 #ifndef EDSP_OSCILLATOR_SIN_HPP
23 #define EDSP_OSCILLATOR_SIN_HPP
24 
25 #include <edsp/oscillator.hpp>
26 #include <edsp/math/constant.hpp>
27 #include <edsp/math/numeric.hpp>
28 #include <cmath>
29 
30 namespace edsp { namespace oscillators {
31 
36 
47  template <typename T>
48  class oscillator {
49  public:
50  using value_type = T;
51 
60  constexpr oscillator(value_type amplitude, value_type samplerate, value_type frequency,
61  value_type phase) noexcept;
66  constexpr value_type frequency() const noexcept;
67 
72  constexpr void set_frequency(value_type frequency) noexcept;
73 
78  constexpr value_type phase() const noexcept;
79 
84  constexpr void set_phase(value_type phase) noexcept;
85 
90  constexpr value_type timestamp() const noexcept;
91 
96  constexpr void set_timestamp(value_type timestamp) noexcept;
97 
102  constexpr value_type samplerate() const noexcept;
103 
108  constexpr void set_samplerate(value_type samplerate) noexcept;
109 
115  constexpr value_type sampling_period() const noexcept;
116 
121  constexpr value_type amplitude() const noexcept;
122 
127  constexpr void set_amplitude(value_type amplitude) noexcept;
128 
132  constexpr void reset() noexcept;
133 
134  private:
135  value_type amplitude_{0.};
136  value_type timestamp_{0.};
137  value_type samplerate_{0.};
138  value_type sampling_period_{0.};
139  value_type frequency_{1.};
140  value_type phase_{0.};
141  };
142 
166  template <typename T>
167  class sin_oscillator : public oscillator<T> {
168  public:
169  using value_type = T;
170 
179  constexpr sin_oscillator(value_type amplitude, value_type samplerate, value_type frequency,
180  value_type phase) noexcept;
181 
186  constexpr value_type operator()();
187  };
188 
189  template <typename T>
190  constexpr sin_oscillator<T>::sin_oscillator(value_type amplitude, value_type samplerate, value_type frequency,
191  value_type phase) noexcept :
192  oscillator<T>(amplitude, samplerate, frequency, phase) {}
193 
194  template <typename T>
196  const value_type result =
197  std::sin(constants<value_type>::two_pi * oscillator<T>::frequency() * oscillator<T>::timestamp() +
199  this->set_timestamp(oscillator<T>::timestamp() + oscillator<T>::sampling_period());
200  return result * oscillator<T>::amplitude();
201  }
202 
203  template <typename T>
204  constexpr oscillator<T>::oscillator(value_type amplitude, value_type samplerate, value_type frequency,
205  value_type phase) noexcept :
206  amplitude_(amplitude),
207  samplerate_(samplerate),
208  sampling_period_(math::inv(samplerate)),
209  frequency_(frequency),
210  phase_(phase) {}
211 
212  template <typename T>
213  constexpr typename oscillator<T>::value_type oscillator<T>::timestamp() const noexcept {
214  return timestamp_;
215  }
216 
217  template <typename T>
219  timestamp_ = timestamp;
220  }
221 
222  template <typename T>
223  constexpr void oscillator<T>::reset() noexcept {
224  timestamp_ = 0;
225  }
226 
227  template <typename T>
228  constexpr typename oscillator<T>::value_type oscillator<T>::samplerate() const noexcept {
229  return samplerate_;
230  }
231 
232  template <typename T>
233  constexpr void oscillator<T>::set_samplerate(value_type samplerate) noexcept {
234  samplerate_ = samplerate;
235  sampling_period_ = math::inv(samplerate_);
236  }
237 
238  template <typename T>
239  constexpr typename oscillator<T>::value_type oscillator<T>::frequency() const noexcept {
240  return frequency_;
241  }
242 
243  template <typename T>
244  constexpr void oscillator<T>::set_frequency(value_type frequency) noexcept {
245  frequency_ = frequency;
246  }
247 
248  template <typename T>
249  constexpr typename oscillator<T>::value_type oscillator<T>::phase() const noexcept {
250  return phase_;
251  }
252 
253  template <typename T>
254  constexpr void oscillator<T>::set_phase(value_type phase) noexcept {
255  phase_ = phase;
256  }
257 
258  template <typename T>
259  constexpr typename oscillator<T>::value_type oscillator<T>::amplitude() const noexcept {
260  return amplitude_;
261  }
262 
263  template <typename T>
264  constexpr void oscillator<T>::set_amplitude(value_type amplitude) noexcept {
265  amplitude_ = amplitude;
266  }
267 
268  template <typename T>
269  constexpr typename oscillator<T>::value_type oscillator<T>::sampling_period() const noexcept {
270  return sampling_period_;
271  }
272 
273 }} // namespace edsp::oscillators
274 
275 #endif // EDSP_OSCILLATOR_SIN_HPP
T value_type
Definition: sinusoidal.hpp:169
constexpr void set_samplerate(value_type samplerate) noexcept
Sets the sampling frequency.
Definition: sinusoidal.hpp:233
constexpr value_type timestamp() const noexcept
Returns the current timestamp of the signal in seconds.
Definition: sinusoidal.hpp:213
constexpr oscillator(value_type amplitude, value_type samplerate, value_type frequency, value_type phase) noexcept
Creates an oscillator that generates a waveform with the configuration.
Definition: sinusoidal.hpp:204
constexpr value_type sampling_period() const noexcept
Returns the sampling period in secs.
Definition: sinusoidal.hpp:269
The oscillator class generates a periodic signal.
Definition: sinusoidal.hpp:48
std::int64_t timestamp
Definition: timestamp.hpp:29
constexpr void set_amplitude(value_type amplitude) noexcept
Set the amplitude of the periodic signal.
Definition: sinusoidal.hpp:264
constexpr sin_oscillator(value_type amplitude, value_type samplerate, value_type frequency, value_type phase) noexcept
Creates a sinusoidal oscillator that generates a waveform with the configuration. ...
Definition: sinusoidal.hpp:190
T value_type
Definition: sinusoidal.hpp:50
constexpr value_type operator()()
Generates one step.
Definition: sinusoidal.hpp:195
constexpr value_type frequency() const noexcept
Returns the fundamental frequency in Hz.
Definition: sinusoidal.hpp:239
constexpr value_type samplerate() const noexcept
Returns the sampling frequency in Hz.
Definition: sinusoidal.hpp:228
constexpr T inv(T x)
Computes the inverse value of the input number.
Definition: numeric.hpp:208
constexpr value_type amplitude() const noexcept
Returns the amplitude of the periodic signal.
Definition: sinusoidal.hpp:259
constexpr value_type phase() const noexcept
Returns the phase shift in radians.
Definition: sinusoidal.hpp:249
constexpr void set_frequency(value_type frequency) noexcept
Sets the fundamental frequency of the periodic signal.
Definition: sinusoidal.hpp:244
constexpr void reset() noexcept
Reset the oscillator to the original state.
Definition: sinusoidal.hpp:223
constexpr void set_phase(value_type phase) noexcept
Sets the phase shift of the periodic signal.
Definition: sinusoidal.hpp:254
constexpr void set_timestamp(value_type timestamp) noexcept
Sets the current timestamp of the periodic signal.
Definition: sinusoidal.hpp:218
The class sin_oscillator generates a sinusoidal signal.
Definition: sinusoidal.hpp:167
constexpr T phase(const std::complex< T > &z)
Computes the phase of the complex number z.
Definition: complex.hpp:50
logger & reset(logger &stream)
Resets the logger to default configuration.
Definition: logger.hpp:416
Definition: amplifier.hpp:29
OscillatorType
the OscillatorType enum represents the different waveforms generated by tha available oscillators...
Definition: sinusoidal.hpp:35