From c7c2e72e6876c0160f1785ead5663df2465025e9 Mon Sep 17 00:00:00 2001 From: Hannes Matuschek Date: Wed, 6 Aug 2014 17:37:40 +0200 Subject: [PATCH] Minors. --- examples/sdr_afsk1200.cc | 63 ++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/examples/sdr_afsk1200.cc b/examples/sdr_afsk1200.cc index b604574..0596517 100644 --- a/examples/sdr_afsk1200.cc +++ b/examples/sdr_afsk1200.cc @@ -73,38 +73,44 @@ public: throw err; } - // The sample rate + // The input sample rate _sampleRate = src_cfg.sampleRate(); + // Symbols per bit (limit to 32 symbols per bit) _corrLen = std::min(int(_sampleRate/_baud), 32); + + // Compute symbol rate: + _symbolRate = _baud*_corrLen; + // Samples per symbol (fractional): - // The resulting sub-sampling will have a symbol rate of _corrLen*_baud - _mu = 0.0; _muIncr = _sampleRate/(_corrLen*_baud); + _mu = 0.0; _muIncr = _sampleRate/_symbolRate; + // Delayline for interpolating sub-sampler _dl = Buffer(2*8); - for (size_t i=0; i<2*8; i++) { _dl[i] = 0; } + for (size_t i=0; i<(2*8); i++) { _dl[i] = 0; } _dl_idx = 0; // Assemble phase LUT: - double phiMark=0, phiSpace=0; _markLUT = Buffer< std::complex >(_corrLen); _spaceLUT = Buffer< std::complex >(_corrLen); + // Allocate ring-buffers for mark and space symbols _markHist = Buffer< std::complex >(_corrLen); _spaceHist = Buffer< std::complex >(_corrLen); // Initialize LUTs and ring-buffers + double phiMark=0, phiSpace=0; for (size_t i=0; i<_corrLen; i++) { _markLUT[i] = std::exp(std::complex(0.0, phiMark)); _spaceLUT[i] = std::exp(std::complex(0.0, phiSpace)); - phiMark += (2.*M_PI*_Fmark)/(_baud*_corrLen); - phiSpace += (2.*M_PI*_Fspace)/(_baud*_corrLen); + phiMark += (2.*M_PI*_Fmark)/_symbolRate; + phiSpace += (2.*M_PI*_Fspace)/_symbolRate; _markHist[i] = 0; _spaceHist[i] = 0; } _lutIdx = 0; // Get phase increment per symbol - _phase = 0; _phaseInc = _phasePeriod/_corrLen; + _phase = 0; _phaseInc = _phaseMask/_corrLen; // Allocate output buffer: _buffer = Buffer(src_cfg.bufferSize()/_corrLen + 1); @@ -114,7 +120,7 @@ public: << " input sample rate: " << _sampleRate << "Hz" << std::endl << " samples per symbol: " << _muIncr << std::endl << " symbols per bit: " << _corrLen << std::endl - << " symbol rate: " << (_corrLen*_baud) << "Hz" << std::endl + << " symbol rate: " << _symbolRate << "Hz" << std::endl << " Phase incr/symbol: " << float(_phaseInc)/_phasePeriod; Logger::get().log(msg); @@ -130,7 +136,7 @@ public: while ((_mu>1) && (i0)&0x1u); + _symbols <<= 1; _symbols |= (f>0); // If transition - if ((_symbols ^ (_symbols >> 1)) & 0x1u) { + if ((_symbols ^ (_symbols >> 1)) & 1) { // Phase correction - if (_phase < (_phasePeriod/2 + _phaseInc/2)) { + if (_phase < (_phasePeriod/2)) { _phase += _phaseInc/8; } else { _phase -= _phaseInc/8; @@ -158,6 +164,7 @@ public: // Advance phase _phase += _phaseInc; + // Sample bit if (_phase >= _phasePeriod) { // Modulo 2 pi _phase &= _phaseMask; @@ -173,23 +180,22 @@ public: protected: - inline float _getSymbol() { - std::complex markSum(0), spaceSum(0); + inline double _getSymbol() { + std::complex markSum(0), spaceSum(0); for (size_t i=0; i<_corrLen; i++) { - markSum += _markHist[i]/float(1<<16); - spaceSum += _spaceHist[i]/float(1<<16); + markSum += _markHist[i]; + spaceSum += _spaceHist[i]; } - markSum /= float(_corrLen); spaceSum /= float(_corrLen); - float f = markSum.real()*markSum.real() + - markSum.imag()*markSum.imag() - - spaceSum.real()*spaceSum.real() - + double f = markSum.real()*markSum.real() + + markSum.imag()*markSum.imag() - + spaceSum.real()*spaceSum.real() - spaceSum.imag()*spaceSum.imag(); return f; } protected: - float _sampleRate; + float _sampleRate, _symbolRate; float _baud; float _Fmark, _Fspace; @@ -249,13 +255,13 @@ public: virtual void process(const Buffer &buffer, bool allow_overwrite) { for (size_t i=0; i 2)) { *_ptr = 0; if (! check_crc_ccitt(_rxbuffer, _ptr-_rxbuffer)) { - std::cerr << "Got invalid buffer : " << _rxbuffer << std::endl; + std::cerr << "Got invalid buffer: " << _rxbuffer << std::endl; } else { std::cerr << "GOT: " << _rxbuffer << std::endl; } @@ -273,15 +279,16 @@ public: if ((_bitstream & 0x3f) == 0x3e) { /* stuffed bit */ continue; } - if (_bitstream & 1) { _bitbuffer |= 0x100u; } + if (_bitstream & 1) { _bitbuffer |= 0x100; } if (_bitbuffer & 1) { - if ((_ptr-_rxbuffer) > 512) { + if ((_ptr-_rxbuffer) >= 512) { Logger::get().log(LogMessage(LOG_ERROR, "AX.25 packet too long.")); _state = 0; continue; } std::cerr << "Got byte: " << std::hex << int( (_bitbuffer>>1) & 0xff ) - << ": " << char(_bitbuffer>>1) << std::endl; + << ": " << char(_bitbuffer>>1) + << "(" << char(_bitbuffer>>2) << ")" << std::endl; *_ptr++ = (_bitbuffer >> 1); _bitbuffer = 0x80u; continue; } _bitbuffer >>= 1;