Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Interference_Simple
DSinGenerator.cpp
- Committer:
- aktk
- Date:
- 2019-11-27
- Revision:
- 0:6400e338266f
File content as of revision 0:6400e338266f:
#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);
}