2014-11-29 13:58:20 -05:00
|
|
|
#include "SDRPostThread.h"
|
|
|
|
#include "CubicSDRDefs.h"
|
|
|
|
#include "CubicSDR.h"
|
|
|
|
|
2014-12-23 23:37:18 -05:00
|
|
|
#include <vector>
|
|
|
|
#include <deque>
|
|
|
|
|
2015-07-29 20:57:02 -04:00
|
|
|
SDRPostThread::SDRPostThread() : IOThread(),
|
2015-09-07 20:46:03 -04:00
|
|
|
iqDataInQueue(NULL), iqDataOutQueue(NULL), iqVisualQueue(NULL), dcFilter(NULL){
|
2015-07-21 00:59:18 -04:00
|
|
|
|
|
|
|
swapIQ.store(false);
|
2015-02-03 12:42:44 -05:00
|
|
|
|
|
|
|
// create a lookup table
|
|
|
|
for (unsigned int i = 0; i <= 0xffff; i++) {
|
2015-05-31 22:13:14 -04:00
|
|
|
liquid_float_complex tmp,tmp_swap;
|
2015-02-03 12:42:44 -05:00
|
|
|
# if (__BYTE_ORDER == __LITTLE_ENDIAN)
|
2015-05-31 22:13:14 -04:00
|
|
|
tmp_swap.imag = tmp.real = (float(i & 0xff) - 127.4f) * (1.0f/128.0f);
|
|
|
|
tmp_swap.real = tmp.imag = (float(i >> 8) - 127.4f) * (1.0f/128.0f);
|
2015-02-03 12:42:44 -05:00
|
|
|
_lut.push_back(tmp);
|
2015-05-31 22:13:14 -04:00
|
|
|
_lut_swap.push_back(tmp_swap);
|
2015-02-03 12:42:44 -05:00
|
|
|
#else // BIG_ENDIAN
|
2015-05-31 22:13:14 -04:00
|
|
|
tmp_swap.imag = tmp.real = (float(i >> 8) - 127.4f) * (1.0f/128.0f);
|
|
|
|
tmp_swap.real = tmp.imag = (float(i & 0xff) - 127.4f) * (1.0f/128.0f);
|
2015-02-03 12:42:44 -05:00
|
|
|
_lut.push_back(tmp);
|
2015-05-31 22:13:14 -04:00
|
|
|
_lut_swap.push_back(tmp_swap);
|
2015-02-03 12:42:44 -05:00
|
|
|
#endif
|
|
|
|
}
|
2014-11-29 13:58:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
SDRPostThread::~SDRPostThread() {
|
2014-12-11 19:07:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void SDRPostThread::bindDemodulator(DemodulatorInstance *demod) {
|
2015-05-31 12:05:45 -04:00
|
|
|
busy_demod.lock();
|
|
|
|
demodulators.push_back(demod);
|
|
|
|
busy_demod.unlock();
|
2014-12-11 19:07:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void SDRPostThread::removeDemodulator(DemodulatorInstance *demod) {
|
|
|
|
if (!demod) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-31 12:05:45 -04:00
|
|
|
busy_demod.lock();
|
|
|
|
std::vector<DemodulatorInstance *>::iterator i = std::find(demodulators.begin(), demodulators.end(), demod);
|
|
|
|
|
|
|
|
if (i != demodulators.end()) {
|
|
|
|
demodulators.erase(i);
|
|
|
|
}
|
|
|
|
busy_demod.unlock();
|
2014-12-11 19:07:21 -05:00
|
|
|
}
|
|
|
|
|
2015-05-31 22:13:14 -04:00
|
|
|
void SDRPostThread::setSwapIQ(bool swapIQ) {
|
|
|
|
this->swapIQ.store(swapIQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SDRPostThread::getSwapIQ() {
|
|
|
|
return this->swapIQ.load();
|
|
|
|
}
|
|
|
|
|
2015-07-29 20:57:02 -04:00
|
|
|
void SDRPostThread::run() {
|
2014-12-18 20:11:25 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
pthread_t tID = pthread_self(); // ID of this thread
|
|
|
|
int priority = sched_get_priority_max( SCHED_FIFO) - 1;
|
2014-12-24 01:28:33 -05:00
|
|
|
sched_param prio = {priority}; // scheduling priority of thread
|
2014-12-18 20:11:25 -05:00
|
|
|
pthread_setschedparam(tID, SCHED_FIFO, &prio);
|
|
|
|
#endif
|
2014-12-16 20:33:44 -05:00
|
|
|
|
2014-11-29 13:58:20 -05:00
|
|
|
dcFilter = iirfilt_crcf_create_dc_blocker(0.0005);
|
|
|
|
|
2014-11-30 17:11:29 -05:00
|
|
|
std::cout << "SDR post-processing thread started.." << std::endl;
|
|
|
|
|
2015-07-30 19:30:46 -04:00
|
|
|
iqDataInQueue = (SDRThreadIQDataQueue*)getInputQueue("IQDataInput");
|
|
|
|
iqDataOutQueue = (DemodulatorThreadInputQueue*)getOutputQueue("IQDataOutput");
|
2015-08-11 00:50:43 -04:00
|
|
|
iqVisualQueue = (DemodulatorThreadInputQueue*)getOutputQueue("IQVisualDataOutput");
|
2015-07-30 19:30:46 -04:00
|
|
|
|
2015-07-28 21:56:39 -04:00
|
|
|
ReBuffer<DemodulatorThreadIQData> buffers;
|
2014-12-26 16:15:35 -05:00
|
|
|
std::vector<liquid_float_complex> fpData;
|
|
|
|
std::vector<liquid_float_complex> dataOut;
|
2015-08-11 00:50:43 -04:00
|
|
|
|
2015-08-12 22:14:14 -04:00
|
|
|
iqDataInQueue->set_max_num_items(0);
|
2015-08-12 21:45:02 -04:00
|
|
|
|
2014-11-29 13:58:20 -05:00
|
|
|
while (!terminated) {
|
2014-12-23 01:59:03 -05:00
|
|
|
SDRThreadIQData *data_in;
|
2015-08-11 00:50:43 -04:00
|
|
|
|
2015-07-30 19:30:46 -04:00
|
|
|
iqDataInQueue->pop(data_in);
|
2015-08-11 00:50:43 -04:00
|
|
|
// std::lock_guard < std::mutex > lock(data_in->m_mutex);
|
|
|
|
|
2014-12-23 01:59:03 -05:00
|
|
|
if (data_in && data_in->data.size()) {
|
2014-12-26 16:15:35 -05:00
|
|
|
int dataSize = data_in->data.size()/2;
|
2015-09-30 23:45:06 -04:00
|
|
|
|
|
|
|
if (dataSize > dataOut.capacity()) {
|
2014-12-26 16:15:35 -05:00
|
|
|
dataOut.reserve(dataSize);
|
|
|
|
}
|
2015-09-30 23:45:06 -04:00
|
|
|
if (dataSize != dataOut.size()) {
|
2014-12-26 16:15:35 -05:00
|
|
|
dataOut.resize(dataSize);
|
2014-12-24 03:03:34 -05:00
|
|
|
}
|
2015-09-13 22:18:29 -04:00
|
|
|
|
2015-08-11 00:50:43 -04:00
|
|
|
|
2015-09-30 23:45:06 -04:00
|
|
|
// if (swapIQ) {
|
|
|
|
// for (int i = 0; i < dataSize; i++) {
|
|
|
|
// fpData[i] = _lut_swap[*((uint16_t*)&data_in->data[2*i])];
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// for (int i = 0; i < dataSize; i++) {
|
|
|
|
// fpData[i] = _lut[*((uint16_t*)&data_in->data[2*i])];
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
if (data_in->dcCorrected) {
|
|
|
|
for (int i = 0; i < dataSize; i++) {
|
|
|
|
dataOut[i].real = data_in->data[i*2];
|
|
|
|
dataOut[i].imag = data_in->data[i*2+1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dataSize > fpData.capacity()) {
|
|
|
|
fpData.reserve(dataSize);
|
|
|
|
}
|
|
|
|
if (dataSize != fpData.size()) {
|
|
|
|
fpData.resize(dataSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < dataSize; i++) {
|
|
|
|
fpData[i].real = data_in->data[i*2];
|
|
|
|
fpData[i].imag = data_in->data[i*2+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
iirfilt_crcf_execute_block(dcFilter, &fpData[0], dataSize, &dataOut[0]);
|
|
|
|
}
|
2015-08-11 00:50:43 -04:00
|
|
|
|
2015-09-07 20:46:03 -04:00
|
|
|
if (iqVisualQueue != NULL && !iqVisualQueue->full()) {
|
2015-08-11 21:49:42 -04:00
|
|
|
DemodulatorThreadIQData *visualDataOut = visualDataBuffers.getBuffer();
|
|
|
|
visualDataOut->setRefCount(1);
|
2015-08-11 00:50:43 -04:00
|
|
|
|
2015-09-07 20:46:03 -04:00
|
|
|
int num_vis_samples = dataOut.size();
|
|
|
|
|
|
|
|
// if (visualDataOut->data.size() < num_vis_samples) {
|
|
|
|
// if (visualDataOut->data.capacity() < num_vis_samples) {
|
|
|
|
// visualDataOut->data.reserve(num_vis_samples);
|
|
|
|
// }
|
|
|
|
// visualDataOut->data.resize(num_vis_samples);
|
|
|
|
// }
|
|
|
|
//
|
2014-12-28 05:13:46 -05:00
|
|
|
visualDataOut->frequency = data_in->frequency;
|
2015-01-11 17:08:16 -05:00
|
|
|
visualDataOut->sampleRate = data_in->sampleRate;
|
2014-12-28 05:13:46 -05:00
|
|
|
visualDataOut->data.assign(dataOut.begin(), dataOut.begin() + num_vis_samples);
|
2015-08-11 00:50:43 -04:00
|
|
|
|
2015-07-30 19:30:46 -04:00
|
|
|
iqVisualQueue->push(visualDataOut);
|
2014-11-29 13:58:20 -05:00
|
|
|
}
|
2015-05-31 12:05:45 -04:00
|
|
|
|
|
|
|
busy_demod.lock();
|
2015-08-11 00:50:43 -04:00
|
|
|
|
2014-12-22 21:12:13 -05:00
|
|
|
int activeDemods = 0;
|
|
|
|
bool pushedData = false;
|
2015-08-11 00:50:43 -04:00
|
|
|
|
|
|
|
if (demodulators.size() || iqDataOutQueue != NULL) {
|
|
|
|
std::vector<DemodulatorInstance *>::iterator demod_i;
|
|
|
|
for (demod_i = demodulators.begin(); demod_i != demodulators.end(); demod_i++) {
|
|
|
|
DemodulatorInstance *demod = *demod_i;
|
2015-01-10 12:27:03 -05:00
|
|
|
if (demod->getFrequency() != data_in->frequency
|
2015-08-11 00:50:43 -04:00
|
|
|
&& abs(data_in->frequency - demod->getFrequency()) > (wxGetApp().getSampleRate() / 2)) {
|
2014-12-22 21:12:13 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
activeDemods++;
|
|
|
|
}
|
2015-08-11 00:50:43 -04:00
|
|
|
|
|
|
|
if (iqDataOutQueue != NULL) {
|
|
|
|
activeDemods++;
|
|
|
|
}
|
|
|
|
|
|
|
|
DemodulatorThreadIQData *demodDataOut = buffers.getBuffer();
|
|
|
|
|
|
|
|
// std::lock_guard < std::mutex > lock(demodDataOut->m_mutex);
|
|
|
|
demodDataOut->frequency = data_in->frequency;
|
|
|
|
demodDataOut->sampleRate = data_in->sampleRate;
|
|
|
|
demodDataOut->setRefCount(activeDemods);
|
|
|
|
demodDataOut->data.assign(dataOut.begin(), dataOut.end());
|
|
|
|
|
|
|
|
for (demod_i = demodulators.begin(); demod_i != demodulators.end(); demod_i++) {
|
|
|
|
DemodulatorInstance *demod = *demod_i;
|
|
|
|
DemodulatorThreadInputQueue *demodQueue = demod->getIQInputDataPipe();
|
|
|
|
|
|
|
|
if (abs(data_in->frequency - demod->getFrequency()) > (wxGetApp().getSampleRate() / 2)) {
|
|
|
|
if (demod->isActive() && !demod->isFollow() && !demod->isTracking()) {
|
|
|
|
demod->setActive(false);
|
|
|
|
DemodulatorThreadIQData *dummyDataOut = new DemodulatorThreadIQData;
|
|
|
|
dummyDataOut->frequency = data_in->frequency;
|
|
|
|
dummyDataOut->sampleRate = data_in->sampleRate;
|
|
|
|
demodQueue->push(dummyDataOut);
|
2014-12-22 21:12:13 -05:00
|
|
|
}
|
2015-08-11 00:50:43 -04:00
|
|
|
|
|
|
|
if (demod->isFollow() && wxGetApp().getFrequency() != demod->getFrequency()) {
|
|
|
|
wxGetApp().setFrequency(demod->getFrequency());
|
2014-12-11 20:50:58 -05:00
|
|
|
}
|
2015-08-11 00:50:43 -04:00
|
|
|
} else if (!demod->isActive()) {
|
|
|
|
demod->setActive(true);
|
|
|
|
if (wxGetApp().getDemodMgr().getLastActiveDemodulator() == NULL) {
|
|
|
|
wxGetApp().getDemodMgr().setActiveDemodulator(demod);
|
2015-03-26 22:45:52 -04:00
|
|
|
}
|
2014-12-22 21:12:13 -05:00
|
|
|
}
|
2015-08-11 00:50:43 -04:00
|
|
|
|
|
|
|
if (!demod->isActive()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (demod->isFollow()) {
|
|
|
|
demod->setFollow(false);
|
2014-12-22 23:27:52 -05:00
|
|
|
}
|
2015-08-11 00:50:43 -04:00
|
|
|
|
|
|
|
demodQueue->push(demodDataOut);
|
|
|
|
pushedData = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iqDataOutQueue != NULL) {
|
2015-08-12 21:45:02 -04:00
|
|
|
if (!iqDataOutQueue->full()) {
|
|
|
|
iqDataOutQueue->push(demodDataOut);
|
|
|
|
pushedData = true;
|
|
|
|
} else {
|
|
|
|
demodDataOut->decRefCount();
|
|
|
|
}
|
2015-08-11 00:50:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pushedData && iqDataOutQueue == NULL) {
|
|
|
|
demodDataOut->setRefCount(0);
|
2014-12-22 23:27:52 -05:00
|
|
|
}
|
2014-12-22 19:43:56 -05:00
|
|
|
}
|
2015-08-11 00:50:43 -04:00
|
|
|
|
2015-05-31 12:05:45 -04:00
|
|
|
busy_demod.unlock();
|
2014-12-23 01:59:03 -05:00
|
|
|
}
|
2014-12-24 00:11:41 -05:00
|
|
|
data_in->decRefCount();
|
2014-11-29 13:58:20 -05:00
|
|
|
}
|
2014-12-23 23:37:18 -05:00
|
|
|
|
2015-08-16 19:47:49 -04:00
|
|
|
// buffers.purge();
|
2015-07-28 21:56:39 -04:00
|
|
|
|
2015-07-30 19:30:46 -04:00
|
|
|
if (iqVisualQueue && !iqVisualQueue->empty()) {
|
2015-01-22 23:41:33 -05:00
|
|
|
DemodulatorThreadIQData *visualDataDummy;
|
2015-07-30 19:30:46 -04:00
|
|
|
iqVisualQueue->pop(visualDataDummy);
|
2015-01-22 23:41:33 -05:00
|
|
|
}
|
2014-12-23 23:37:18 -05:00
|
|
|
|
2015-08-16 19:47:49 -04:00
|
|
|
// visualDataBuffers.purge();
|
2015-01-22 21:32:32 -05:00
|
|
|
|
2014-11-30 17:11:29 -05:00
|
|
|
std::cout << "SDR post-processing thread done." << std::endl;
|
2014-11-29 13:58:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void SDRPostThread::terminate() {
|
|
|
|
terminated = true;
|
2014-12-23 01:59:03 -05:00
|
|
|
SDRThreadIQData *dummy = new SDRThreadIQData;
|
2015-07-30 19:30:46 -04:00
|
|
|
iqDataInQueue->push(dummy);
|
2014-11-29 13:58:20 -05:00
|
|
|
}
|