libsdr  0.1.0
A simple SDR library
fsk.hh
1 #ifndef __SDR_FSK_HH__
2 #define __SDR_FSK_HH__
3 
4 #include "node.hh"
5 #include "traits.hh"
6 #include "logger.hh"
7 
8 
9 namespace sdr {
10 
11 
18 class FSKDetector: public Sink<int16_t>, public Source
19 {
20 public:
25  FSKDetector(float baud, float Fmark, float Fspace);
26 
27  void config(const Config &src_cfg);
28  void process(const Buffer<int16_t> &buffer, bool allow_overwrite);
29 
30 protected:
32  uint8_t _process(int16_t sample);
33 
34 protected:
37  float _baud;
39  size_t _corrLen;
41  size_t _lutIdx;
43  float _Fmark;
45  float _Fspace;
56 };
57 
58 
69 template <class Scalar>
70 class ASKDetector: public Sink<Scalar>, public Source
71 {
72 public:
74  ASKDetector(bool invert=false)
75  : Sink<Scalar>(), Source(), _invert(invert)
76  {
77  // pass...
78  }
79 
80  void config(const Config &src_cfg) {
81  // Check if config is complete
82  if (!src_cfg.hasType() || !src_cfg.hasSampleRate()) { return; }
83 
84  // Check if buffer type matches
85  if (Config::typeId<int16_t>() != src_cfg.type()) {
86  ConfigError err;
87  err << "Can not configure ASKDetector: Invalid type " << src_cfg.type()
88  << ", expected " << Config::typeId<int16_t>();
89  throw err;
90  }
91 
92  // Allocate output buffer
93  _buffer = Buffer<uint8_t>(src_cfg.bufferSize());
94 
95  LogMessage msg(LOG_DEBUG);
96  msg << "Config ASKDetector node: " << std::endl
97  << " threshold: " << 0 << std::endl
98  << " invert: " << ( _invert ? "yes" : "no" ) << std::endl
99  << " symbol rate: " << src_cfg.sampleRate() << " Hz";
100  Logger::get().log(msg);
101 
102  // Forward config.
104  }
105 
106  void process(const Buffer<Scalar> &buffer, bool allow_overwrite) {
107  for (size_t i=0; i<buffer.size(); i++) {
108  _buffer[i] = ((buffer[i]>0)^_invert);
109  }
110  this->send(_buffer.head(buffer.size()), false);
111  }
112 
113 protected:
115  bool _invert;
118 };
119 
120 
124 class BitStream: public Sink<uint8_t>, public Source
125 {
126 public:
128  typedef enum {
131  } Mode;
132 
133 public:
137  BitStream(float baud, Mode mode = TRANSITION);
138 
139  void config(const Config &src_cfg);
140  void process(const Buffer<uint8_t> &buffer, bool allow_overwrite);
141 
142 protected:
144  float _baud;
148  size_t _corrLen;
152  size_t _symIdx;
154  int32_t _symSum;
156  int32_t _lastSymSum;
158  float _phase;
160  float _omega;
162  float _omegaMin;
164  float _omegaMax;
166  float _pllGain;
168  uint8_t _lastBits;
171 };
172 
173 
176 class BitDump : public Sink<uint8_t>
177 {
178 public:
181  BitDump(std::ostream &stream);
182 
183  void config(const Config &src_cfg);
184  void process(const Buffer<uint8_t> &buffer, bool allow_overwrite);
185 
186 protected:
188  std::ostream &_stream;
189 };
190 
191 }
192 #endif // __SDR_FSK_HH__
Buffer< std::complex< float > > _markHist
FIR filter buffer.
Definition: fsk.hh:51
Buffer< std::complex< float > > _spaceHist
FIR filter buffer.
Definition: fsk.hh:53
A collection of configuration information that is send by a source to all connected sinks to properga...
Definition: node.hh:35
float _omega
Phase velocity.
Definition: fsk.hh:160
ASKDetector(bool invert=false)
Constructor.
Definition: fsk.hh:74
float _pllGain
PLL gain.
Definition: fsk.hh:166
size_t _symIdx
Insertion index for the next symbol.
Definition: fsk.hh:152
float _phase
Current bit "phase".
Definition: fsk.hh:158
std::ostream & _stream
The output stream.
Definition: fsk.hh:188
Buffer< uint8_t > _buffer
The output buffer.
Definition: fsk.hh:117
BitStream(float baud, Mode mode=TRANSITION)
Constructor.
Definition: fsk.cc:102
virtual void send(const RawBuffer &buffer, bool allow_overwrite=false)
Sends the given buffer to all connected sinks.
Definition: node.cc:67
Typed sink.
Definition: node.hh:192
float _baud
Baudrate of the transmission.
Definition: fsk.hh:37
Definition: autocast.hh:8
void process(const Buffer< int16_t > &buffer, bool allow_overwrite)
Needs to be implemented by any sub-type to process the received data.
Definition: fsk.cc:90
void config(const Config &src_cfg)
Needs to be implemented by any sub-type to check and perform the configuration of the node...
Definition: fsk.hh:80
bool hasSampleRate() const
If true, the configuration has a sample rate.
Definition: node.hh:75
void config(const Config &src_cfg)
Needs to be implemented by any sub-type to check and perform the configuration of the node...
Definition: fsk.cc:109
Generic source class.
Definition: node.hh:213
Mode _mode
The bit detection mode.
Definition: fsk.hh:146
Transition mode (i.e. transition -> 0, no transition -> 1).
Definition: fsk.hh:130
Buffer< uint8_t > _buffer
Output buffer.
Definition: fsk.hh:170
int32_t _symSum
Sum over all received symbol (encoded as -1 & 1).
Definition: fsk.hh:154
float _Fspace
Space "tone" frequency.
Definition: fsk.hh:45
size_t size() const
Returns the number of elements of type T in this buffer.
Definition: buffer.hh:166
bool hasType() const
If true, the configuration has a type.
Definition: node.hh:69
bool _invert
If true the symbol logic is inverted.
Definition: fsk.hh:115
Buffer< std::complex< float > > _spaceLUT
Space frequency FIR filter LUT.
Definition: fsk.hh:49
uint8_t _lastBits
The last decoded bits (needed for transition mode).
Definition: fsk.hh:168
Buffer< int8_t > _buffer
Output buffer.
Definition: fsk.hh:55
FSKDetector(float baud, float Fmark, float Fspace)
Constructor.
Definition: fsk.cc:12
virtual void setConfig(const Config &config)
Stores the configuration and propergates it if the configuration has been changed.
Definition: node.cc:98
float _baud
The baud rate.
Definition: fsk.hh:144
Buffer< T > head(size_t n) const
Returns a new view on this buffer.
Definition: buffer.hh:237
Buffer< int8_t > _symbols
Last received symbols.
Definition: fsk.hh:150
void log(const LogMessage &message)
Logs a message.
Definition: logger.cc:100
Type type() const
Returns the type.
Definition: node.hh:71
float _omegaMax
Maximum phase velocity.
Definition: fsk.hh:164
Trivial node to dump a bit-stream to a std::ostream.
Definition: fsk.hh:176
static Logger & get()
Returns the singleton instance of the logger.
Definition: logger.cc:89
BitDump(std::ostream &stream)
Constructor.
Definition: fsk.cc:208
The configuration error class.
Definition: exception.hh:24
A log message.
Definition: logger.hh:22
size_t bufferSize() const
Returns the max.
Definition: node.hh:83
void process(const Buffer< Scalar > &buffer, bool allow_overwrite)
Needs to be implemented by any sub-type to process the received data.
Definition: fsk.hh:106
Implements the basic FSK/AFSK symbol detection.
Definition: fsk.hh:18
size_t _lutIdx
The current FIR filter LUT index.
Definition: fsk.hh:41
Mode
Possible bit decoding modes.
Definition: fsk.hh:128
void config(const Config &src_cfg)
Needs to be implemented by any sub-type to check and perform the configuration of the node...
Definition: fsk.cc:19
Rather trivial node to detect mark/space symbols by the amplitude.
Definition: fsk.hh:70
size_t _corrLen
The filter lenght.
Definition: fsk.hh:39
Normal mode (i.e. mark -> 1, space -> 0).
Definition: fsk.hh:129
void config(const Config &src_cfg)
Needs to be implemented by any sub-type to check and perform the configuration of the node...
Definition: fsk.cc:215
void process(const Buffer< uint8_t > &buffer, bool allow_overwrite)
Needs to be implemented by any sub-type to process the received data.
Definition: fsk.cc:229
Buffer< std::complex< float > > _markLUT
Mark frequency FIR filter LUT.
Definition: fsk.hh:47
size_t _corrLen
The approximative bit length in samples.
Definition: fsk.hh:148
Forward declaration of type tratis template.
Definition: traits.hh:20
int32_t _lastSymSum
Last sum over all received symbol (encoded as -1 & 1).
Definition: fsk.hh:156
float _omegaMin
Minimum phase velocity.
Definition: fsk.hh:162
Decodes a bitstream with the desired baud rate.
Definition: fsk.hh:124
uint8_t _process(int16_t sample)
Updates the mark/space FIR filter and returns the sampled symbol.
Definition: fsk.cc:69
A typed buffer.
Definition: buffer.hh:111
double sampleRate() const
Returns the sample rate.
Definition: node.hh:77
float _Fmark
Mark "tone" frequency.
Definition: fsk.hh:43
void process(const Buffer< uint8_t > &buffer, bool allow_overwrite)
Needs to be implemented by any sub-type to process the received data.
Definition: fsk.cc:158