Skip to content

Commit

Permalink
added conditional compilation for ofdm-decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
JvanKatwijk committed Mar 22, 2018
1 parent 16ccfdf commit b2db9d5
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 25 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ Feel free to adapt each (all) of these programs to your own ideas.

=======================================================================

LIBRARY CONFIGURATION PARAMETER (IMPORTANT)

For e.g. the RPI 2, where the CPU has more cores, but the capacity
of each core is limited, the best option is to split the processing
of the frontend into two parts.
A configuration parameter in the CMakeLists.txt file controls this

If __THREADED_DECODING is defined, either in the file
"library/includes/ofdm/ofdm-decoder.h" or with a line
add_definitions (-D__THREADED_DECODING) in the CMakeLists.txt file,

The "ofdm_decoding" (involving a lot of FFT's) will be done in a
separate thread. This is the way I run it on an RPI2.

The drawback is that synchronization might take longer.

========================================================================

=======================================================================

The C (C++) example programs
------------------------------------------------------------------------

Expand Down
17 changes: 5 additions & 12 deletions devices/sdrplay-handler/sdrplay-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mir_sdr_DeviceT devDesc [4];

if (hwVersion == 255) {
nrBits = 14;
denominator = 16384.0;
denominator = 8192.0;
}
else {
nrBits = 12;
Expand Down Expand Up @@ -186,11 +186,11 @@ void myStreamCallback (int16_t *xi,
int16_t i;
sdrplayHandler *p = static_cast<sdrplayHandler *> (cbContext);
std::complex<float> localBuf [numSamples];
int nrBits = p -> nrBits;
float denominator = p -> denominator;

for (i = 0; i < (int)numSamples; i ++)
localBuf [i] = std::complex<float> (float (xi [i]) / nrBits,
float (xq [i]) / nrBits);
localBuf [i] = std::complex<float> (float (xi [i]) / denominator,
float (xq [i]) / denominator);
p -> _I_Buffer -> putDataIntoBuffer (localBuf, numSamples);
(void) firstSampleNum;
(void) grChanged;
Expand Down Expand Up @@ -237,10 +237,6 @@ int localGRed = 102 - theGain;
mir_sdr_SetPpm ((float)ppmCorrection);
err = mir_sdr_SetDcMode (4, 1);
err = mir_sdr_SetDcTrackTime (63);
//
mir_sdr_SetSyncUpdatePeriod ((int)(inputRate / 2));
mir_sdr_SetSyncUpdateSampleNum (samplesPerPacket);
mir_sdr_DCoffsetIQimbalanceControl (0, 1);
running. store (true);
return true;
}
Expand All @@ -256,11 +252,8 @@ void sdrplayHandler::stopReader (void) {
//
// Note that the sdrPlay returns 12/14 bit values
int32_t sdrplayHandler::getSamples (std::complex<float> *V, int32_t size) {
int32_t count = 0;
float sum = 0;

count = _I_Buffer -> getDataFromBuffer (V, size);
return count;
return _I_Buffer -> getDataFromBuffer (V, size);
}

int32_t sdrplayHandler::Samples (void) {
Expand Down
4 changes: 2 additions & 2 deletions devices/sdrplay-handler/sdrplay-handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class sdrplayHandler: public deviceHandler {
// need to be visible, since being accessed from
// within the callback
RingBuffer<std::complex<float>> *_I_Buffer;
int16_t nrBits;
float denominator;
private:

int16_t hwVersion;
float denominator;
int16_t nrBits;
uint16_t deviceIndex;
uint32_t numofDevs; // int32_t not my choice
int32_t inputRate;
Expand Down
1 change: 1 addition & 0 deletions example-2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ if (DEFINED_SERVER)
add_definitions(-DHAVE_SERVER)
endif ()

#add_definitions (-D__THREADED_DECODING)
#########################################################################
find_package (PkgConfig)

Expand Down
13 changes: 11 additions & 2 deletions library/includes/ofdm/ofdm-decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "freq-interleaver.h"
#include "semaphore.h"

// UNCOMMENT THE DEFINE FOR E.G. THE RPI 2
//#define __THREADED_DECODING
class ficHandler;
class mscHandler;
class dabParams;
Expand All @@ -48,7 +50,7 @@ class ofdmDecoder {
mscHandler *);
~ofdmDecoder (void);
void processBlock_0 (std::complex<float> *);
void decodeblock (std::complex<float> *, int32_t n);
void decodeBlock (std::complex<float> *, int32_t n);
int16_t get_snr (void);
void stop (void);
void start (void);
Expand All @@ -59,6 +61,7 @@ class ofdmDecoder {
RingBuffer<std::complex<float>> *iqBuffer;
ficHandler *my_ficHandler;
mscHandler *my_mscHandler;
#ifdef __THREADED_DECODING
Semaphore bufferSpace;
std::thread threadHandle;
std::mutex myMutex;
Expand All @@ -68,11 +71,17 @@ class ofdmDecoder {
std::atomic<bool> running;
std::complex<float> **command;
int16_t amount;
int cnt;
int16_t currentBlock;
void processBlock_0 (void);
void decodeFICblock (int32_t n);
void decodeMscblock (int32_t n);
#else
void decodeFICblock (std::complex<float> *,
int32_t n);
void decodeMscblock (std::complex<float> *,
int32_t n);
#endif
int cnt;
int32_t T_s;
int32_t T_u;
int32_t T_g;
Expand Down
2 changes: 1 addition & 1 deletion library/src/backend/fic-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int16_t local = 0;
* each 128 bit block contains 4 subblocks of 32 bits
* on which the given puncturing is applied
*/
memset (punctureTable, 0, (3072 + 24) * sizeof (uint8_t));
memset (punctureTable, 0, (3072 + 24) * sizeof (bool));

for (i = 0; i < 21; i ++) {
for (k = 0; k < 32 * 4; k ++) {
Expand Down
45 changes: 38 additions & 7 deletions library/src/ofdm/ofdm-decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
RingBuffer<std::complex<float>> *iqBuffer,
ficHandler *my_ficHandler,
mscHandler *my_mscHandler):
#ifdef __THREADED_DECODING
bufferSpace (p -> get_L ()),
#endif
myMapper (p) {
int16_t i;
this -> params = p;
Expand All @@ -64,7 +66,7 @@ int16_t i;
//
current_snr = 0;
cnt = 0;

#ifdef __THREADED_DECODING
/**
* When implemented in a thread, the thread controls the
* reading in of the data and processing the data through
Expand All @@ -79,9 +81,11 @@ int16_t i;
amount = 0;
//
// will be started from within ofdmProcessor
#endif
}

ofdmDecoder::~ofdmDecoder (void) {
#ifdef __THREADED_DECODING
int16_t i;
if (running. load ()) {
running. store (false);
Expand All @@ -95,22 +99,28 @@ int16_t i;
for (i = 0; i < nrBlocks; i ++)
delete[] command [i];
delete[] command;
#endif
}

void ofdmDecoder::start (void) {
#ifdef __THREADED_DECODING
amount = 0;
running. store (true);
threadHandle = std::thread (&ofdmDecoder::run, this);
#endif
}

void ofdmDecoder::stop (void) {
#ifdef __THREADED_DECODING
if (running. load ()) {
running. store (false);
Locker. notify_all ();
amount = 100;
threadHandle. join ();
}
#endif
}
#ifdef __THREADED_DECODING
/**
* The code in the thread executes a simple loop,
* waiting for the next block and executing the interpretation
Expand Down Expand Up @@ -158,22 +168,34 @@ void ofdmDecoder::processBlock_0 (std::complex<float> *vi) {
Locker. notify_one ();
}

void ofdmDecoder::decodeblock (std::complex<float> *vi, int32_t blkno) {
void ofdmDecoder::decodeBlock (std::complex<float> *vi, int32_t blkno) {
bufferSpace. acquire ();
memcpy (command [blkno], &vi [T_g], sizeof (std::complex<float>) * T_u);
myMutex. lock ();
amount ++;
myMutex. unlock ();
Locker. notify_one ();
}
#else
void ofdmDecoder::decodeBlock (std::complex<float> *vi, int32_t blkno) {
if (blkno < 4)
decodeFICblock (vi, blkno);
else
decodeMscblock (vi, blkno);
}
#endif
/**
* Note that the distinction, made in the ofdmProcessor class
* does not add much here, iff we decide to choose the multi core
* option definitely, then code may be simplified there.
*/
#ifdef __THREADED_DECODING
void ofdmDecoder::processBlock_0 (void) {

memcpy (fft_buffer, command [0], T_u * sizeof (std::complex<float>));
#else
void ofdmDecoder::processBlock_0 (std::complex<float> *v) {
memcpy (fft_buffer, v, T_u * sizeof (std::complex<float>));
#endif
my_fftHandler -> do_FFT (fft_handler::fftForward);
/**
* The SNR is determined by looking at a segment of bins
Expand All @@ -197,11 +219,17 @@ void ofdmDecoder::processBlock_0 (void) {
* \brief decodeFICblock
* do the transforms and hand over the result to the fichandler
*/
#ifdef __THREADED_DECODING
void ofdmDecoder::decodeFICblock (int32_t blkno) {
int16_t i;
std::complex<float> conjVector [T_u];

memcpy (fft_buffer, command [blkno], T_u * sizeof (std::complex<float>));
#else
void ofdmDecoder::decodeFICblock (std::complex<float> * v, int32_t blkno) {
int16_t i;
std::complex<float> conjVector [T_u];
memcpy (fft_buffer, &v [T_g], T_u * sizeof (std::complex<float>));
#endif
fftlabel:
/**
* first step: do the FFT
Expand Down Expand Up @@ -264,17 +292,20 @@ std::complex<float> conjVector [T_u];
* restrain the blocks from which the constellation is shown
* to the FIC blocks
*/
#ifdef __THREADED_DECODING
void ofdmDecoder::decodeMscblock (int32_t blkno) {
int16_t i;

memcpy (fft_buffer, command [blkno], T_u * sizeof (std::complex<float>));
#else
void ofdmDecoder::decodeMscblock (std::complex<float> *v, int32_t blkno) {
memcpy (fft_buffer, &v [T_g], T_u * sizeof (std::complex<float>));
#endif
fftLabel:
my_fftHandler -> do_FFT (fft_handler::fftForward);
//
// Note that "mapIn" maps to -carriers / 2 .. carriers / 2
// we did not set the fft output to low .. high
toBitsLabel:
for (i = 0; i < carriers; i ++) {
for (int i = 0; i < carriers; i ++) {
int16_t index = myMapper. mapIn (i);
if (index < 0)
index += T_u;
Expand Down
2 changes: 1 addition & 1 deletion library/src/ofdm/ofdm-processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ int index_attempts = 0;
for (i = (int)T_u; i < (int)T_s; i ++)
FreqCorr += ofdmBuffer [i] * conj (ofdmBuffer [i - T_u]);

my_ofdmDecoder. decodeblock (ofdmBuffer, ofdmSymbolCount);
my_ofdmDecoder. decodeBlock (ofdmBuffer, ofdmSymbolCount);
}

// we integrate the newly found frequency error with the
Expand Down

0 comments on commit b2db9d5

Please sign in to comment.