siggen.hh
#ifndef __SDR_SIGGEN_HH__
#define __SDR_SIGGEN_HH__
#include "node.hh"
namespace sdr {
template <class Scalar>
class SigGen: public Source
{
public:
SigGen(double samplerate, size_t buffersize, double tmax=-1)
: Source(), _sampleRate(samplerate), _dt(1./_sampleRate), _t(0), _tMax(tmax), _scale(1),
_bufferSize(buffersize), _buffer(buffersize)
{
switch (Config::typeId<Scalar>()) {
case Config::Type_u8:
case Config::Type_s8:
case Config::Type_cu8:
case Config::Type_cs8:
_scale = 1<<6; break;
case Config::Type_u16:
case Config::Type_s16:
case Config::Type_cu16:
case Config::Type_cs16:
_scale = 1<<14; break;
default:
_scale = 1; break;
}
this->setConfig(Config(Config::typeId<Scalar>(), samplerate, buffersize, 1));
}
virtual ~SigGen() {}
void next() {
// Stop processing once the max time elapsed
if ((_tMax>0) && (_t >= _tMax)) { Queue::get().stop(); return; }
// Assemble signal
for (size_t i=0; i<_bufferSize; i++) {
_buffer[i] = 0;
if (_signals.size() > 0) {
std::list< std::vector<double> >::iterator item = _signals.begin();
for (; item != _signals.end(); item++) {
_buffer[i] += (_scale*((*item)[1] * sin(2*M_PI*(*item)[0]*_t + (*item)[2]))/_signals.size());
}
}
_t += _dt;
}
// Send buffer
this->send(_buffer);
}
void addSine(double freq, double ampl=1, double phase=0) {
std::vector<double> tmp(3); tmp[0] = freq; tmp[1] = ampl; tmp[2] = phase;
_signals.push_back(tmp);
}
protected:
double _sampleRate;
double _dt;
double _t;
double _tMax;
double _scale;
std::list< std::vector<double> > _signals;
size_t _bufferSize;
Buffer<Scalar> _buffer;
};
template <class Scalar>
class IQSigGen: public Source
{
public:
IQSigGen(double samplerate, size_t buffersize, double tmax=-1)
: Source(), _sampleRate(samplerate), _dt(1./_sampleRate), _t(0), _tMax(tmax), _scale(1),
_bufferSize(buffersize), _buffer(buffersize)
{
switch (Config::typeId<Scalar>()) {
case Config::Type_cu8:
case Config::Type_cs8:
_scale = 1<<6; break;
case Config::Type_cu16:
case Config::Type_cs16:
_scale = 1<<14; break;
default:
_scale = 1; break;
}
this->setConfig(Config(Config::typeId< std::complex<Scalar> >(), samplerate, buffersize, 1));
}
virtual ~IQSigGen() {}
void next() {
// Stop processing once the max time elapsed
if ((_tMax>0) && (_t >= _tMax)) { Queue::get().stop(); return; }
// Assemble signal
for (size_t i=0; i<_bufferSize; i++) {
_buffer[i] = 0;
if (_signals.size() > 0) {
std::list< std::vector<double> >::iterator item = _signals.begin();
for (; item != _signals.end(); item++) {
_buffer[i] += (_scale*((*item)[1] * std::exp(std::complex<double>(0, 2*M_PI*(*item)[0]*_t + (*item)[2])))/double(_signals.size()));
}
}
_t += _dt;
}
// Send buffer
this->send(_buffer);
}
void addSine(double freq, double ampl=1, double phase=0) {
std::vector<double> tmp(3); tmp[0] = freq; tmp[1] = ampl; tmp[2] = phase;
_signals.push_back(tmp);
}
protected:
double _sampleRate;
double _dt;
double _t;
double _tMax;
double _scale;
std::list< std::vector<double> > _signals;
size_t _bufferSize;
Buffer< std::complex<Scalar> > _buffer;
};
}
#endif // __SDR_SIGGEN_HH__
sdr::SigGen::addSine
void addSine(double freq, double ampl=1, double phase=0)
Add a sine function to the function generator.
Definition: siggen.hh:61
sdr::IQSigGen::_buffer
Buffer< std::complex< Scalar > > _buffer
The output buffer.
Definition: siggen.hh:156
sdr::Config
A collection of configuration information that is send by a source to all connected sinks to properga...
Definition: node.hh:35
sdr::SigGen::_signals
std::list< std::vector< double > > _signals
A list of functions.
Definition: siggen.hh:78
sdr::SigGen::_bufferSize
size_t _bufferSize
The size of the output buffer.
Definition: siggen.hh:80
sdr::SigGen::~SigGen
virtual ~SigGen()
Destructor.
Definition: siggen.hh:39
sdr::IQSigGen::_scale
double _scale
The scaling of the signal.
Definition: siggen.hh:150
sdr::Config::Type_s16
Real signed 16b ints.
Definition: node.hh:44
sdr::SigGen::SigGen
SigGen(double samplerate, size_t buffersize, double tmax=-1)
Constructs the function generator.
Definition: siggen.hh:16
sdr::Source::send
virtual void send(const RawBuffer &buffer, bool allow_overwrite=false)
Sends the given buffer to all connected sinks.
Definition: node.cc:67
sdr::Queue::get
static Queue & get()
Get a reference to the global instance of the queue.
Definition: queue.cc:15
sdr::Config::Type_cu16
Complex (aka I/Q) type of unsigned 16b ints.
Definition: node.hh:49
sdr
Definition: autocast.hh:8
sdr::SigGen::_dt
double _dt
The sample period.
Definition: siggen.hh:70
sdr::IQSigGen::_sampleRate
double _sampleRate
The sample rate of the function generator.
Definition: siggen.hh:142
sdr::Config::Type_s8
Real signed 8b ints.
Definition: node.hh:42
sdr::Queue::stop
void stop()
Signals the queue to stop processing.
Definition: queue.cc:64
sdr::IQSigGen::addSine
void addSine(double freq, double ampl=1, double phase=0)
Add a sine function to the function generator.
Definition: siggen.hh:135
sdr::Source
Generic source class.
Definition: node.hh:213
sdr::Config::Type_u8
Real unsigned 8b ints.
Definition: node.hh:41
sdr::IQSigGen::_bufferSize
size_t _bufferSize
The size of the output buffer.
Definition: siggen.hh:154
sdr::IQSigGen::_t
double _t
The current time.
Definition: siggen.hh:146
sdr::SigGen::_scale
double _scale
The scaling of the signal.
Definition: siggen.hh:76
sdr::Config::Type_u16
Real unsigned 16b ints.
Definition: node.hh:43
sdr::Source::setConfig
virtual void setConfig(const Config &config)
Stores the configuration and propergates it if the configuration has been changed.
Definition: node.cc:98
sdr::IQSigGen::_dt
double _dt
The sample period.
Definition: siggen.hh:144
sdr::IQSigGen
Arbitrary function generator.
Definition: siggen.hh:90
sdr::IQSigGen::_tMax
double _tMax
The maximum time.
Definition: siggen.hh:148
sdr::SigGen::_t
double _t
The current time.
Definition: siggen.hh:72
sdr::SigGen::_sampleRate
double _sampleRate
The sample rate of the function generator.
Definition: siggen.hh:68
sdr::SigGen::_buffer
Buffer< Scalar > _buffer
The output buffer.
Definition: siggen.hh:82
sdr::Config::typeId
static Type typeId()
Returns the type-id of the template type.
sdr::IQSigGen::~IQSigGen
virtual ~IQSigGen()
Destructor.
Definition: siggen.hh:113
sdr::IQSigGen::_signals
std::list< std::vector< double > > _signals
A list of functions.
Definition: siggen.hh:152
sdr::IQSigGen::IQSigGen
IQSigGen(double samplerate, size_t buffersize, double tmax=-1)
Constructs the function generator.
Definition: siggen.hh:94
sdr::SigGen::_tMax
double _tMax
The maximum time.
Definition: siggen.hh:74
sdr::IQSigGen::next
void next()
Computes the next buffer.
Definition: siggen.hh:116
sdr::Config::Type_cs8
Complex (aka I/Q) type of signed 8b ints.
Definition: node.hh:48
sdr::Config::Type_cs16
Complex (aka I/Q) type of signed 16b ints.
Definition: node.hh:50
sdr::Config::Type_cu8
Complex (aka I/Q) type of unsigned 8b ints.
Definition: node.hh:47
sdr::SigGen
Arbitrary function generator.
Definition: siggen.hh:12
sdr::SigGen::next
void next()
Computes the next buffer.
Definition: siggen.hh:42
sdr::Buffer< Scalar >