libsdr  0.1.0
A simple SDR library
siggen.hh
1 #ifndef __SDR_SIGGEN_HH__
2 #define __SDR_SIGGEN_HH__
3 
4 #include "node.hh"
5 
6 
7 namespace sdr {
8 
11 template <class Scalar>
12 class SigGen: public Source
13 {
14 public:
16  SigGen(double samplerate, size_t buffersize, double tmax=-1)
17  : Source(), _sampleRate(samplerate), _dt(1./_sampleRate), _t(0), _tMax(tmax), _scale(1),
18  _bufferSize(buffersize), _buffer(buffersize)
19  {
20  switch (Config::typeId<Scalar>()) {
21  case Config::Type_u8:
22  case Config::Type_s8:
23  case Config::Type_cu8:
24  case Config::Type_cs8:
25  _scale = 1<<6; break;
26  case Config::Type_u16:
27  case Config::Type_s16:
28  case Config::Type_cu16:
29  case Config::Type_cs16:
30  _scale = 1<<14; break;
31  default:
32  _scale = 1; break;
33  }
34 
35  this->setConfig(Config(Config::typeId<Scalar>(), samplerate, buffersize, 1));
36  }
37 
39  virtual ~SigGen() {}
40 
42  void next() {
43  // Stop processing once the max time elapsed
44  if ((_tMax>0) && (_t >= _tMax)) { Queue::get().stop(); return; }
45  // Assemble signal
46  for (size_t i=0; i<_bufferSize; i++) {
47  _buffer[i] = 0;
48  if (_signals.size() > 0) {
49  std::list< std::vector<double> >::iterator item = _signals.begin();
50  for (; item != _signals.end(); item++) {
51  _buffer[i] += (_scale*((*item)[1] * sin(2*M_PI*(*item)[0]*_t + (*item)[2]))/_signals.size());
52  }
53  }
54  _t += _dt;
55  }
56  // Send buffer
57  this->send(_buffer);
58  }
59 
61  void addSine(double freq, double ampl=1, double phase=0) {
62  std::vector<double> tmp(3); tmp[0] = freq; tmp[1] = ampl; tmp[2] = phase;
63  _signals.push_back(tmp);
64  }
65 
66 protected:
68  double _sampleRate;
70  double _dt;
72  double _t;
74  double _tMax;
76  double _scale;
78  std::list< std::vector<double> > _signals;
80  size_t _bufferSize;
83 };
84 
85 
86 
89 template <class Scalar>
90 class IQSigGen: public Source
91 {
92 public:
94  IQSigGen(double samplerate, size_t buffersize, double tmax=-1)
95  : Source(), _sampleRate(samplerate), _dt(1./_sampleRate), _t(0), _tMax(tmax), _scale(1),
96  _bufferSize(buffersize), _buffer(buffersize)
97  {
98  switch (Config::typeId<Scalar>()) {
99  case Config::Type_cu8:
100  case Config::Type_cs8:
101  _scale = 1<<6; break;
102  case Config::Type_cu16:
103  case Config::Type_cs16:
104  _scale = 1<<14; break;
105  default:
106  _scale = 1; break;
107  }
108 
109  this->setConfig(Config(Config::typeId< std::complex<Scalar> >(), samplerate, buffersize, 1));
110  }
111 
113  virtual ~IQSigGen() {}
114 
116  void next() {
117  // Stop processing once the max time elapsed
118  if ((_tMax>0) && (_t >= _tMax)) { Queue::get().stop(); return; }
119  // Assemble signal
120  for (size_t i=0; i<_bufferSize; i++) {
121  _buffer[i] = 0;
122  if (_signals.size() > 0) {
123  std::list< std::vector<double> >::iterator item = _signals.begin();
124  for (; item != _signals.end(); item++) {
125  _buffer[i] += (_scale*((*item)[1] * std::exp(std::complex<double>(0, 2*M_PI*(*item)[0]*_t + (*item)[2])))/double(_signals.size()));
126  }
127  }
128  _t += _dt;
129  }
130  // Send buffer
131  this->send(_buffer);
132  }
133 
135  void addSine(double freq, double ampl=1, double phase=0) {
136  std::vector<double> tmp(3); tmp[0] = freq; tmp[1] = ampl; tmp[2] = phase;
137  _signals.push_back(tmp);
138  }
139 
140 protected:
142  double _sampleRate;
144  double _dt;
146  double _t;
148  double _tMax;
150  double _scale;
152  std::list< std::vector<double> > _signals;
154  size_t _bufferSize;
157 };
158 
159 }
160 
161 
162 #endif // __SDR_SIGGEN_HH__
void addSine(double freq, double ampl=1, double phase=0)
Add a sine function to the function generator.
Definition: siggen.hh:61
Buffer< std::complex< Scalar > > _buffer
The output buffer.
Definition: siggen.hh:156
A collection of configuration information that is send by a source to all connected sinks to properga...
Definition: node.hh:35
std::list< std::vector< double > > _signals
A list of functions.
Definition: siggen.hh:78
size_t _bufferSize
The size of the output buffer.
Definition: siggen.hh:80
virtual ~SigGen()
Destructor.
Definition: siggen.hh:39
double _scale
The scaling of the signal.
Definition: siggen.hh:150
Real signed 16b ints.
Definition: node.hh:44
SigGen(double samplerate, size_t buffersize, double tmax=-1)
Constructs the function generator.
Definition: siggen.hh:16
virtual void send(const RawBuffer &buffer, bool allow_overwrite=false)
Sends the given buffer to all connected sinks.
Definition: node.cc:67
static Queue & get()
Get a reference to the global instance of the queue.
Definition: queue.cc:15
Complex (aka I/Q) type of unsigned 16b ints.
Definition: node.hh:49
Definition: autocast.hh:8
double _dt
The sample period.
Definition: siggen.hh:70
double _sampleRate
The sample rate of the function generator.
Definition: siggen.hh:142
Real signed 8b ints.
Definition: node.hh:42
void stop()
Signals the queue to stop processing.
Definition: queue.cc:64
void addSine(double freq, double ampl=1, double phase=0)
Add a sine function to the function generator.
Definition: siggen.hh:135
Generic source class.
Definition: node.hh:213
Real unsigned 8b ints.
Definition: node.hh:41
size_t _bufferSize
The size of the output buffer.
Definition: siggen.hh:154
double _t
The current time.
Definition: siggen.hh:146
double _scale
The scaling of the signal.
Definition: siggen.hh:76
Real unsigned 16b ints.
Definition: node.hh:43
virtual void setConfig(const Config &config)
Stores the configuration and propergates it if the configuration has been changed.
Definition: node.cc:98
double _dt
The sample period.
Definition: siggen.hh:144
Arbitrary function generator.
Definition: siggen.hh:90
double _tMax
The maximum time.
Definition: siggen.hh:148
double _t
The current time.
Definition: siggen.hh:72
double _sampleRate
The sample rate of the function generator.
Definition: siggen.hh:68
Buffer< Scalar > _buffer
The output buffer.
Definition: siggen.hh:82
static Type typeId()
Returns the type-id of the template type.
virtual ~IQSigGen()
Destructor.
Definition: siggen.hh:113
std::list< std::vector< double > > _signals
A list of functions.
Definition: siggen.hh:152
IQSigGen(double samplerate, size_t buffersize, double tmax=-1)
Constructs the function generator.
Definition: siggen.hh:94
double _tMax
The maximum time.
Definition: siggen.hh:74
void next()
Computes the next buffer.
Definition: siggen.hh:116
Complex (aka I/Q) type of signed 8b ints.
Definition: node.hh:48
Complex (aka I/Q) type of signed 16b ints.
Definition: node.hh:50
Complex (aka I/Q) type of unsigned 8b ints.
Definition: node.hh:47
Arbitrary function generator.
Definition: siggen.hh:12
void next()
Computes the next buffer.
Definition: siggen.hh:42