eDSP  0.0.1
A cross-platform DSP library written in C++.
sawtooth.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: sawtooth.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 27/7/2018
21  */
22 #ifndef EDSP_OSCILLATOR_SAWTOOTH_HPP
23 #define EDSP_OSCILLATOR_SAWTOOTH_HPP
24 
26 #include <edsp/math/constant.hpp>
27 
28 namespace edsp { namespace oscillators {
29 
40  template <typename T>
41  class sawtooth_oscillator : public oscillator<T> {
42  public:
43  using value_type = T;
44 
54  value_type width) noexcept;
59  constexpr value_type operator()();
60 
66  constexpr void set_width(value_type width) noexcept;
67 
75  constexpr value_type width() const noexcept;
76 
77  private:
78  value_type width_{0.3};
79  };
80 
81  template <typename T>
85  width_(width) {}
86 
87  template <typename T>
89  width_ = width / oscillator<T>::frequency();
90  }
91 
92  template <typename T>
94  return width_ * oscillator<T>::frequency();
95  }
96 
97  template <typename T>
99  const auto t = oscillator<T>::timestamp();
100  const value_type result = (t >= width_) ? -2 * t / (1 - width_) + 1 : 2 * t / width_ - 1;
101  const value_type increased = t + oscillator<T>::sampling_period();
102  this->set_timestamp((increased > 1. / oscillator<T>::frequency()) ? 0 : increased);
103  return result * oscillator<T>::amplitude();
104  }
105 
106 }} // namespace edsp::oscillators
107 
108 #endif // EDSP_OSCILLATOR_SAWTOOTH_HPP
The class sawtooth_oscillator generates a sawtooth signal.
Definition: sawtooth.hpp:41
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
The oscillator class generates a periodic signal.
Definition: sinusoidal.hpp:48
constexpr sawtooth_oscillator(value_type amplitude, value_type samplerate, value_type frequency, value_type width) noexcept
Creates a sawtooth oscillator that generates a waveform with the configuration.
Definition: sawtooth.hpp:82
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 value_type amplitude() const noexcept
Returns the amplitude of the periodic signal.
Definition: sinusoidal.hpp:259
T value_type
Definition: sawtooth.hpp:43
constexpr void set_timestamp(value_type timestamp) noexcept
Sets the current timestamp of the periodic signal.
Definition: sinusoidal.hpp:218
constexpr value_type width() const noexcept
Returns the width of the periodic signal.
Definition: sawtooth.hpp:93
constexpr void set_width(value_type width) noexcept
Set the width of the periodic signal.
Definition: sawtooth.hpp:88
Definition: amplifier.hpp:29
constexpr value_type operator()()
Generates one step.
Definition: sawtooth.hpp:98