eDSP  0.0.1
A cross-platform DSP library written in C++.
square.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: square.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 27/7/2018
21  */
22 #ifndef EDSP_OSCILLATOR_SQUARE_HPP
23 #define EDSP_OSCILLATOR_SQUARE_HPP
24 
26 #include <edsp/math/constant.hpp>
27 
28 namespace edsp { namespace oscillators {
29 
48  template <typename T>
49  class square_oscillator : public oscillator<T> {
50  public:
51  using value_type = T;
52 
62  value_type duty) noexcept;
63 
68  constexpr value_type operator()();
69 
78  constexpr void set_duty(value_type duty) noexcept;
79 
84  constexpr value_type duty() const noexcept;
85 
86  private:
87  value_type duty_{0};
88  };
89 
90  template <typename T>
92  value_type duty) noexcept :
94  duty_(duty) {}
95 
96  template <typename T>
97  constexpr void square_oscillator<T>::set_duty(value_type duty) noexcept {
98  duty_ = duty / oscillator<T>::frequency();
99  }
100 
101  template <typename T>
102  constexpr typename square_oscillator<T>::value_type square_oscillator<T>::duty() const noexcept {
103  return duty_ * oscillator<T>::frequency();
104  }
105 
106  template <typename T>
108  const auto t = oscillator<T>::timestamp();
109  const value_type result = (t >= duty_) ? -1 : 1;
110  const value_type increased = t + oscillator<T>::sampling_period();
111  this->set_timestamp((increased > math::inv(oscillator<T>::frequency())) ? 0 : increased);
112  return result * oscillator<T>::amplitude();
113  }
114 
115 }} // namespace edsp::oscillators
116 
117 #endif // EDSP_OSCILLATOR_SQUARE_HPP
T value_type
Definition: square.hpp:51
constexpr value_type timestamp() const noexcept
Returns the current timestamp of the signal in seconds.
Definition: sinusoidal.hpp:213
constexpr value_type sampling_period() const noexcept
Returns the sampling period in secs.
Definition: sinusoidal.hpp:269
constexpr value_type duty() const noexcept
Returns the current duty cycle.
Definition: square.hpp:102
The oscillator class generates a periodic signal.
Definition: sinusoidal.hpp:48
T value_type
Definition: sinusoidal.hpp:50
constexpr value_type frequency() const noexcept
Returns the fundamental frequency in Hz.
Definition: sinusoidal.hpp:239
The class square_oscillator generates a square signal.
Definition: square.hpp:49
constexpr value_type samplerate() const noexcept
Returns the sampling frequency in Hz.
Definition: sinusoidal.hpp:228
constexpr void set_duty(value_type duty) noexcept
Set the duty cycle of the oscillator.
Definition: square.hpp:97
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 void set_timestamp(value_type timestamp) noexcept
Sets the current timestamp of the periodic signal.
Definition: sinusoidal.hpp:218
constexpr value_type operator()()
Generates one step.
Definition: square.hpp:107
constexpr square_oscillator(value_type amplitude, value_type samplerate, value_type frequency, value_type duty) noexcept
Creates a square oscillator that generates a waveform with the configuration.
Definition: square.hpp:91
Definition: amplifier.hpp:29