make discretized sin wave, which is stored internal array var. The iterator of which array can go ahead by calling a get function.

Committer:
aktk
Date:
Mon Dec 02 22:48:37 2019 +0000
Revision:
1:b01e84ce3ae0
Parent:
DSinGenerator.cpp@0:6400e338266f
renamed lib and class; added m_ initial at private members;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aktk 1:b01e84ce3ae0 1 #include "DSin.h"
aktk 0:6400e338266f 2
aktk 1:b01e84ce3ae0 3 DSin::DSin(
aktk 0:6400e338266f 4 uint16_t const arg_resolution_ofsin
aktk 0:6400e338266f 5 ):
aktk 0:6400e338266f 6 ampl_max(10),
aktk 0:6400e338266f 7 freq_max(5000),
aktk 0:6400e338266f 8 resolution_ofsin(arg_resolution_ofsin)
aktk 0:6400e338266f 9 {
aktk 0:6400e338266f 10 init();
aktk 0:6400e338266f 11 setParam(0, 4000);
aktk 0:6400e338266f 12 }
aktk 0:6400e338266f 13
aktk 1:b01e84ce3ae0 14 DSin::DSin(
aktk 0:6400e338266f 15 float const arg_ampl,
aktk 0:6400e338266f 16 uint16_t const arg_freq,
aktk 0:6400e338266f 17 uint16_t const arg_resolution_ofsin
aktk 0:6400e338266f 18 ):
aktk 0:6400e338266f 19 ampl_max(10),
aktk 0:6400e338266f 20 freq_max(5000),
aktk 0:6400e338266f 21 resolution_ofsin(arg_resolution_ofsin)
aktk 0:6400e338266f 22 {
aktk 0:6400e338266f 23 init();
aktk 0:6400e338266f 24 setParam(arg_ampl, arg_freq);
aktk 0:6400e338266f 25 }
aktk 0:6400e338266f 26
aktk 1:b01e84ce3ae0 27 void DSin::setParam(
aktk 0:6400e338266f 28 float const arg_ampl,
aktk 0:6400e338266f 29 uint16_t const arg_freq
aktk 0:6400e338266f 30 )
aktk 0:6400e338266f 31 {
aktk 0:6400e338266f 32 setAmplitude(arg_ampl);
aktk 0:6400e338266f 33 setFrequency(arg_freq);
aktk 0:6400e338266f 34 }
aktk 0:6400e338266f 35
aktk 1:b01e84ce3ae0 36 void DSin::setAmplitude(
aktk 0:6400e338266f 37 float const arg_ampl
aktk 0:6400e338266f 38 )
aktk 0:6400e338266f 39 {
aktk 1:b01e84ce3ae0 40 m_ampl = arg_ampl;
aktk 0:6400e338266f 41 for(int i = 0; i < resolution_ofsin; i++){
aktk 1:b01e84ce3ae0 42 m_discretized_sin_p16m16[i] = static_cast<int32_t>(4095.0 / ampl_max * m_ampl * m_discretized_sin[i] );
aktk 0:6400e338266f 43 }
aktk 0:6400e338266f 44 }
aktk 0:6400e338266f 45
aktk 1:b01e84ce3ae0 46 void DSin::setFrequency(
aktk 0:6400e338266f 47 uint16_t const arg_freq
aktk 0:6400e338266f 48 )
aktk 0:6400e338266f 49 {
aktk 1:b01e84ce3ae0 50 m_freq = arg_freq;
aktk 1:b01e84ce3ae0 51 m_pwth = 1000000 / m_freq / resolution_ofsin;
aktk 0:6400e338266f 52 }
aktk 0:6400e338266f 53
aktk 1:b01e84ce3ae0 54 void DSin::init()
aktk 0:6400e338266f 55 {
aktk 1:b01e84ce3ae0 56 m_discretized_sin = new float[resolution_ofsin];
aktk 1:b01e84ce3ae0 57 m_discretized_sin_p16m16 = new int32_t[resolution_ofsin];
aktk 0:6400e338266f 58
aktk 0:6400e338266f 59 for(int i = 0; i < resolution_ofsin; i++){
aktk 1:b01e84ce3ae0 60 m_discretized_sin[i] = sin( 2.0 * M_PI * static_cast<float>(i) / static_cast<float>(resolution_ofsin));
aktk 0:6400e338266f 61 }
aktk 0:6400e338266f 62 }
aktk 0:6400e338266f 63
aktk 1:b01e84ce3ae0 64 float DSin::getValue()
aktk 0:6400e338266f 65 {
aktk 0:6400e338266f 66 static int itr = 0;
aktk 1:b01e84ce3ae0 67 return m_ampl * m_discretized_sin[itr++ % resolution_ofsin];
aktk 0:6400e338266f 68 }
aktk 0:6400e338266f 69
aktk 1:b01e84ce3ae0 70 int32_t DSin::getValue_p16m16()
aktk 0:6400e338266f 71 {
aktk 0:6400e338266f 72 static int itr = 0;
aktk 1:b01e84ce3ae0 73 return m_discretized_sin_p16m16[itr++ % resolution_ofsin];
aktk 0:6400e338266f 74 }
aktk 0:6400e338266f 75
aktk 1:b01e84ce3ae0 76 void DSin::getValueofSamplePoints(float arg_dsin[])
aktk 0:6400e338266f 77 {
aktk 1:b01e84ce3ae0 78 memcpy(arg_dsin, m_discretized_sin, sizeof(float) * resolution_ofsin);
aktk 0:6400e338266f 79 }