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:
Wed Nov 27 23:03:42 2019 +0000
Revision:
0:6400e338266f
nyaan

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aktk 0:6400e338266f 1 #include "DSinGenerator.h"
aktk 0:6400e338266f 2
aktk 0:6400e338266f 3 DSinGenerator::DSinGenerator(
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 0:6400e338266f 14 DSinGenerator::DSinGenerator(
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 0:6400e338266f 27 void DSinGenerator::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 0:6400e338266f 36 void DSinGenerator::setAmplitude(
aktk 0:6400e338266f 37 float const arg_ampl
aktk 0:6400e338266f 38 )
aktk 0:6400e338266f 39 {
aktk 0:6400e338266f 40 ampl = arg_ampl;
aktk 0:6400e338266f 41 for(int i = 0; i < resolution_ofsin; i++){
aktk 0:6400e338266f 42 discretized_sin_p16m16[i] = static_cast<int32_t>(4095.0 / ampl_max * ampl * discretized_sin[i] );
aktk 0:6400e338266f 43 }
aktk 0:6400e338266f 44 }
aktk 0:6400e338266f 45
aktk 0:6400e338266f 46 void DSinGenerator::setFrequency(
aktk 0:6400e338266f 47 uint16_t const arg_freq
aktk 0:6400e338266f 48 )
aktk 0:6400e338266f 49 {
aktk 0:6400e338266f 50 freq = arg_freq;
aktk 0:6400e338266f 51 pwth = 1000000 / freq / resolution_ofsin;
aktk 0:6400e338266f 52 }
aktk 0:6400e338266f 53
aktk 0:6400e338266f 54 void DSinGenerator::init()
aktk 0:6400e338266f 55 {
aktk 0:6400e338266f 56 discretized_sin = new float[resolution_ofsin];
aktk 0:6400e338266f 57 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 0:6400e338266f 60 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 0:6400e338266f 64 float DSinGenerator::getValue()
aktk 0:6400e338266f 65 {
aktk 0:6400e338266f 66 static int itr = 0;
aktk 0:6400e338266f 67 return ampl * discretized_sin[itr++ % resolution_ofsin];
aktk 0:6400e338266f 68 }
aktk 0:6400e338266f 69
aktk 0:6400e338266f 70 int32_t DSinGenerator::getValue_p16m16()
aktk 0:6400e338266f 71 {
aktk 0:6400e338266f 72 static int itr = 0;
aktk 0:6400e338266f 73 return discretized_sin_p16m16[itr++ % resolution_ofsin];
aktk 0:6400e338266f 74 }
aktk 0:6400e338266f 75
aktk 0:6400e338266f 76 void DSinGenerator::getValueofSamplePoints(float arg_dsin[])
aktk 0:6400e338266f 77 {
aktk 0:6400e338266f 78 memcpy(arg_dsin, discretized_sin, sizeof(float) * resolution_ofsin);
aktk 0:6400e338266f 79 }