make discretized sin wave, which is stored internal array var. The iterator of which array can go ahead by calling a get function.
Revision 1:b01e84ce3ae0, committed 2019-12-02
- Comitter:
- aktk
- Date:
- Mon Dec 02 22:48:37 2019 +0000
- Parent:
- 0:6400e338266f
- Commit message:
- renamed lib and class; added m_ initial at private members;
Changed in this revision
diff -r 6400e338266f -r b01e84ce3ae0 DSin.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DSin.cpp Mon Dec 02 22:48:37 2019 +0000 @@ -0,0 +1,79 @@ +#include "DSin.h" + +DSin::DSin( + uint16_t const arg_resolution_ofsin +): + ampl_max(10), + freq_max(5000), + resolution_ofsin(arg_resolution_ofsin) +{ + init(); + setParam(0, 4000); +} + +DSin::DSin( + float const arg_ampl, + uint16_t const arg_freq, + uint16_t const arg_resolution_ofsin +): + ampl_max(10), + freq_max(5000), + resolution_ofsin(arg_resolution_ofsin) +{ + init(); + setParam(arg_ampl, arg_freq); +} + +void DSin::setParam( + float const arg_ampl, + uint16_t const arg_freq +) +{ + setAmplitude(arg_ampl); + setFrequency(arg_freq); +} + +void DSin::setAmplitude( + float const arg_ampl +) +{ + m_ampl = arg_ampl; + for(int i = 0; i < resolution_ofsin; i++){ + m_discretized_sin_p16m16[i] = static_cast<int32_t>(4095.0 / ampl_max * m_ampl * m_discretized_sin[i] ); + } +} + +void DSin::setFrequency( + uint16_t const arg_freq +) +{ + m_freq = arg_freq; + m_pwth = 1000000 / m_freq / resolution_ofsin; +} + +void DSin::init() +{ + m_discretized_sin = new float[resolution_ofsin]; + m_discretized_sin_p16m16 = new int32_t[resolution_ofsin]; + + for(int i = 0; i < resolution_ofsin; i++){ + m_discretized_sin[i] = sin( 2.0 * M_PI * static_cast<float>(i) / static_cast<float>(resolution_ofsin)); + } +} + +float DSin::getValue() +{ + static int itr = 0; + return m_ampl * m_discretized_sin[itr++ % resolution_ofsin]; +} + +int32_t DSin::getValue_p16m16() +{ + static int itr = 0; + return m_discretized_sin_p16m16[itr++ % resolution_ofsin]; +} + +void DSin::getValueofSamplePoints(float arg_dsin[]) +{ + memcpy(arg_dsin, m_discretized_sin, sizeof(float) * resolution_ofsin); +} \ No newline at end of file
diff -r 6400e338266f -r b01e84ce3ae0 DSin.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DSin.h Mon Dec 02 22:48:37 2019 +0000 @@ -0,0 +1,97 @@ +/** Defining Discretized Sinusoidal Wave Model + * + * \file DSin.h + * \author Akifumi Takahashi + * \date 2019/Nov/28 ver.1 publish + * \version 1.0.2019.Nov + * \version 2.0.2019.Dec + */ + +#ifndef DISCRETIZED_SINUSOIDAL_WAVE_MODEL_H +#define DISCRETIZED_SINUSOIDAL_WAVE_MODEL_H +#define _USE_MATH_DEFINES +#include <cmath> + +#ifndef M_PI +#define M_PI 3.141592 +#endif + +#include "mbed.h" +/** \Class DISCRETIZED SINUSOIDAL WAVE MODEL + * + * Model of a discretized sinusoidal wave whose value type is float, + * and the range is from (-1.0f * ampl) to (1.0f * ampl). + * + */ +class DSin +{ +private: + /// Amplitude of sinusoidal wave (mA) + float m_ampl; + + /// Frequency of the wave (Hz) + uint16_t m_freq; + + /// Pulse width fineness of dicretization (us) + /// pwth = 1000000 / freq / resolution_ofsin; + uint16_t m_pwth; + + float* m_discretized_sin; + int32_t* m_discretized_sin_p16m16; + + void init(); + +public: + float const ampl_max; + uint16_t const freq_max; + uint16_t const resolution_ofsin; // = arg_resolution_ofsin below + + DSin( + uint16_t const arg_resolution_ofsin = 20 + ); + DSin( + float const arg_ampl, + uint16_t const arg_freq, + uint16_t const arg_resolution_ofsin = 20 + ); + + void setParam( + float const arg_ampl, + uint16_t const arg_freq + ); + + void setAmplitude( + float const arg_ampl + ); + + void setFrequency( + uint16_t const arg_freq + ); + + float getValue(); + int32_t getValue_p16m16(); + + void getValueofSamplePoints(float[]); + + float getAmplitude(); //inline + uint16_t getFrequency(); //inline + uint16_t getPulseWidth(); //inline + +}; + + +inline float DSin::getAmplitude() +{ + return m_ampl; +} + +inline uint16_t DSin::getFrequency() +{ + return m_freq; +} + +inline uint16_t DSin::getPulseWidth() +{ + return m_pwth; +} +#endif \ No newline at end of file
diff -r 6400e338266f -r b01e84ce3ae0 DSinGenerator.cpp --- a/DSinGenerator.cpp Wed Nov 27 23:03:42 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,79 +0,0 @@ -#include "DSinGenerator.h" - -DSinGenerator::DSinGenerator( - uint16_t const arg_resolution_ofsin -): - ampl_max(10), - freq_max(5000), - resolution_ofsin(arg_resolution_ofsin) -{ - init(); - setParam(0, 4000); -} - -DSinGenerator::DSinGenerator( - float const arg_ampl, - uint16_t const arg_freq, - uint16_t const arg_resolution_ofsin -): - ampl_max(10), - freq_max(5000), - resolution_ofsin(arg_resolution_ofsin) -{ - init(); - setParam(arg_ampl, arg_freq); -} - -void DSinGenerator::setParam( - float const arg_ampl, - uint16_t const arg_freq -) -{ - setAmplitude(arg_ampl); - setFrequency(arg_freq); -} - -void DSinGenerator::setAmplitude( - float const arg_ampl -) -{ - ampl = arg_ampl; - for(int i = 0; i < resolution_ofsin; i++){ - discretized_sin_p16m16[i] = static_cast<int32_t>(4095.0 / ampl_max * ampl * discretized_sin[i] ); - } -} - -void DSinGenerator::setFrequency( - uint16_t const arg_freq -) -{ - freq = arg_freq; - pwth = 1000000 / freq / resolution_ofsin; -} - -void DSinGenerator::init() -{ - discretized_sin = new float[resolution_ofsin]; - discretized_sin_p16m16 = new int32_t[resolution_ofsin]; - - for(int i = 0; i < resolution_ofsin; i++){ - discretized_sin[i] = sin( 2.0 * M_PI * static_cast<float>(i) / static_cast<float>(resolution_ofsin)); - } -} - -float DSinGenerator::getValue() -{ - static int itr = 0; - return ampl * discretized_sin[itr++ % resolution_ofsin]; -} - -int32_t DSinGenerator::getValue_p16m16() -{ - static int itr = 0; - return discretized_sin_p16m16[itr++ % resolution_ofsin]; -} - -void DSinGenerator::getValueofSamplePoints(float arg_dsin[]) -{ - memcpy(arg_dsin, discretized_sin, sizeof(float) * resolution_ofsin); -} \ No newline at end of file
diff -r 6400e338266f -r b01e84ce3ae0 DSinGenerator.h --- a/DSinGenerator.h Wed Nov 27 23:03:42 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,96 +0,0 @@ -/** Defining Discretized Sinusoidal Wave Generator - * - * \file DSinGenerator.h - * \author Akifumi Takahashi - * \date 2019/Nov/28 ver.1 publish - * \version 1.0.2019.Nov - */ - -#ifndef DISCRETIZED_SINUSOIDAL_WAVE_GENERATOR_H -#define DISCRETIZED_SINUSOIDAL_WAVE_GENERATOR_H -#define _USE_MATH_DEFINES -#include <cmath> - -#ifndef M_PI -#define M_PI 3.141592 -#endif - -#include "mbed.h" -/** \Class DISCRETIZED SINUSOIDAL WAVE GENERATOR - * - * Generate a discretized sinusoidal wave whose value type is float, - * and the range is from (-1.0f * ampl) to (1.0f * ampl). - * - */ -class DSinGenerator -{ -private: - /// Amplitude of sinusoidal wave (mA) - float ampl; - - /// Frequency of the wave (Hz) - uint16_t freq; - - /// Pulse width fineness of dicretization (us) - /// pwth = 1000000 / freq / resolution_ofsin; - uint16_t pwth; - - float* discretized_sin; - int32_t* discretized_sin_p16m16; - - void init(); - -public: - float const ampl_max; - uint16_t const freq_max; - uint16_t const resolution_ofsin; // = arg_resolution_ofsin below - - DSinGenerator( - uint16_t const arg_resolution_ofsin = 20 - ); - DSinGenerator( - float const arg_ampl, - uint16_t const arg_freq, - uint16_t const arg_resolution_ofsin = 20 - ); - - void setParam( - float const arg_ampl, - uint16_t const arg_freq - ); - - void setAmplitude( - float const arg_ampl - ); - - void setFrequency( - uint16_t const arg_freq - ); - - float getValue(); - int32_t getValue_p16m16(); - - void getValueofSamplePoints(float[]); - - float getAmplitude(); //inline - uint16_t getFrequency(); //inline - uint16_t getPulseWidth(); //inline - -}; - - -inline float DSinGenerator::getAmplitude() -{ - return ampl; -} - -inline uint16_t DSinGenerator::getFrequency() -{ - return freq; -} - -inline uint16_t DSinGenerator::getPulseWidth() -{ - return pwth; -} -#endif \ No newline at end of file