Drag up/down to set arbitrary FM demod bandwidth

Shouldn't need separate WBFM/FM/NFM setting this way -- Just "FM" and
then set arbitrary bandwidth by dragging.

Also removed redundant demod resampling stages left over from early
experiments.
This commit is contained in:
Charles J. Cliffe
2014-11-26 21:05:19 -05:00
parent f441546023
commit ddbc08a4ff
13 changed files with 256 additions and 87 deletions
+25 -5
View File
@@ -2,6 +2,12 @@
DemodulatorInstance::DemodulatorInstance() :
t_Demod(NULL), threadQueueDemod(NULL), demodulatorThread(NULL) {
threadQueueDemod = new DemodulatorThreadInputQueue;
threadQueueCommand = new DemodulatorThreadCommandQueue;
demodulatorThread = new DemodulatorThread(threadQueueDemod);
demodulatorThread->setCommandQueue(threadQueueCommand);
}
DemodulatorInstance::~DemodulatorInstance() {
@@ -14,20 +20,30 @@ void DemodulatorInstance::setVisualOutputQueue(DemodulatorThreadOutputQueue *tQu
demodulatorThread->setVisualOutputQueue(tQueue);
}
void DemodulatorInstance::init() {
if (demodulatorThread) {
void DemodulatorInstance::run() {
if (t_Demod) {
terminate();
delete threadQueueDemod;
delete demodulatorThread;
delete t_Demod;
threadQueueDemod = new DemodulatorThreadInputQueue;
threadQueueCommand = new DemodulatorThreadCommandQueue;
demodulatorThread = new DemodulatorThread(threadQueueDemod);
demodulatorThread->setCommandQueue(threadQueueCommand);
}
threadQueueDemod = new DemodulatorThreadInputQueue;
demodulatorThread = new DemodulatorThread(threadQueueDemod, &params);
t_Demod = new std::thread(&DemodulatorThread::threadMain, demodulatorThread);
}
DemodulatorThreadCommandQueue *DemodulatorInstance::getCommandQueue() {
return threadQueueCommand;
}
DemodulatorThreadParameters &DemodulatorInstance::getParams() {
return demodulatorThread->getParams();
}
void DemodulatorInstance::terminate() {
demodulatorThread->terminate();
t_Demod->join();
@@ -55,3 +71,7 @@ void DemodulatorMgr::terminateAll() {
delete d;
}
}
std::vector<DemodulatorInstance *> &DemodulatorMgr::getDemodulators() {
return demods;
}
+6 -2
View File
@@ -11,12 +11,15 @@ public:
std::thread *t_Demod;
DemodulatorThreadInputQueue* threadQueueDemod;
DemodulatorThreadParameters params;
DemodulatorThreadCommandQueue* threadQueueCommand;
DemodulatorInstance();
~DemodulatorInstance();
void setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue);
void init();
DemodulatorThreadCommandQueue *getCommandQueue();
DemodulatorThreadParameters &getParams();
void run();
void terminate();
};
@@ -26,6 +29,7 @@ public:
~DemodulatorMgr();
DemodulatorInstance *newThread();
std::vector<DemodulatorInstance *> &getDemodulators();
void terminateAll();
private:
+61 -35
View File
@@ -2,21 +2,27 @@
#include "CubicSDRDefs.h"
#include <vector>
DemodulatorThread::DemodulatorThread(DemodulatorThreadInputQueue* pQueue, DemodulatorThreadParameters *params_in) :
inputQueue(pQueue), visOutQueue(NULL), terminated(false) {
DemodulatorThread::DemodulatorThread(DemodulatorThreadInputQueue* pQueue) :
inputQueue(pQueue), visOutQueue(NULL), terminated(false), initialized(false), audio_resampler(NULL), audio_resample_ratio(1) {
DemodulatorThreadParameters defaultParams;
if (!params_in) {
params = defaultParams;
} else {
params = *params_in;
}
void DemodulatorThread::initialize() {
initialized = false;
resample_ratio = (float) (params.bandwidth) / (float) params.inputRate;
audio_resample_ratio = (float) (params.audioSampleRate) / (float) params.bandwidth;
float fc = 0.5 * ((double) params.bandwidth / (double) params.inputRate); // filter cutoff frequency
if (fc <= 0) {
fc = 0;
}
resample_ratio = (float) (params.inputResampleRate) / (float) params.inputRate;
second_resampler_ratio = (float) (params.demodResampleRate) / (float) params.inputResampleRate;
audio_resample_ratio = (float) (params.audioSampleRate) / (float) params.demodResampleRate;
if (fc >= 0.5) {
fc = 0.5;
}
float fc = 0.5f * ((float) params.inputResampleRate / (float) params.inputRate) * 0.75; // filter cutoff frequency
float ft = 0.05f; // filter transition
float As = 60.0f; // stop-band attenuation [dB]
float mu = 0.0f; // fractional timing offset
@@ -28,25 +34,21 @@ DemodulatorThread::DemodulatorThread(DemodulatorThreadInputQueue* pQueue, Demodu
fir_filter = firfilt_crcf_create(h, h_len);
h_len = estimate_req_filter_len(ft, As);
liquid_firdes_kaiser(h_len, (float) params.filterFrequency / (float) params.demodResampleRate, As, mu, h);
fir_audio_filter = firfilt_crcf_create(h, h_len);
// create multi-stage arbitrary resampler object
resampler = msresamp_crcf_create(resample_ratio, As);
msresamp_crcf_print(resampler);
second_resampler = msresamp_crcf_create(second_resampler_ratio, As);
msresamp_crcf_print(second_resampler);
// msresamp_crcf_print(resampler);
audio_resampler = msresamp_crcf_create(audio_resample_ratio, As);
msresamp_crcf_print(audio_resampler);
// msresamp_crcf_print(audio_resampler);
float kf = 0.75; // modulation factor
fdem = freqdem_create(kf);
freqdem_print(fdem);
// freqdem_print(fdem);
initialized = true;
std::cout << "inputResampleRate " << params.bandwidth << std::endl;
}
DemodulatorThread::~DemodulatorThread() {
@@ -55,10 +57,45 @@ DemodulatorThread::~DemodulatorThread() {
void DemodulatorThread::threadMain() {
if (!initialized) {
initialize();
}
while (!terminated) {
DemodulatorThreadIQData inp;
inputQueue->pop(inp);
if (!commandQueue->empty()) {
bool paramsChanged = false;
while (!commandQueue->empty()) {
DemodulatorThreadCommand command;
commandQueue->pop(command);
switch (command.cmd) {
case DemodulatorThreadCommand::SDR_THREAD_CMD_SETBANDWIDTH:
if (command.int_value < 3000) {
command.int_value = 3000;
}
if (command.int_value > SRATE) {
command.int_value = SRATE;
}
params.bandwidth = command.int_value;
paramsChanged = true;
break;
}
}
if (paramsChanged) {
initialize();
while (!inputQueue->empty()) { // catch up
inputQueue->pop(inp);
}
}
}
if (!initialized) {
continue;
}
std::vector<signed char> *data = &inp.data;
if (data->size()) {
liquid_float_complex filtered_input[BUF_SIZE / 2];
@@ -103,22 +140,11 @@ void DemodulatorThread::threadMain() {
}
}
int wbfm_out_size = ceil((float) (num_written) * second_resampler_ratio);
liquid_float_complex resampled_wbfm_output[wbfm_out_size];
unsigned int num_wbfm_written;
msresamp_crcf_execute(second_resampler, resampled_output, num_written, resampled_wbfm_output, &num_wbfm_written);
for (int i = 0; i < num_wbfm_written; i++) {
firfilt_crcf_push(fir_audio_filter, resampled_wbfm_output[i]);
firfilt_crcf_execute(fir_audio_filter, &resampled_wbfm_output[i]);
}
int audio_out_size = ceil((float) (num_wbfm_written) * audio_resample_ratio);
int audio_out_size = ceil((float) (num_written) * audio_resample_ratio);
liquid_float_complex resampled_audio_output[audio_out_size];
unsigned int num_audio_written;
msresamp_crcf_execute(audio_resampler, resampled_wbfm_output, num_wbfm_written, resampled_audio_output, &num_audio_written);
msresamp_crcf_execute(audio_resampler, resampled_output, num_written, resampled_audio_output, &num_audio_written);
std::vector<float> newBuffer;
newBuffer.resize(num_audio_written * 2);
+38 -11
View File
@@ -16,9 +16,29 @@
#include "CubicSDRDefs.h"
enum DemodulatorType {
DEMOD_TYPE_NULL, DEMOD_TYPE_AM, DEMOD_TYPE_FM, DEMOD_TYPE_LSB, DEMOD_TYPE_USB, DEMOD_TYPE_WFM
DEMOD_TYPE_NULL, DEMOD_TYPE_AM, DEMOD_TYPE_FM, DEMOD_TYPE_LSB, DEMOD_TYPE_USB
};
class DemodulatorThreadCommand {
public:
enum DemodulatorThreadCommandEnum {
SDR_THREAD_CMD_NULL,
SDR_THREAD_CMD_SETBANDWIDTH
};
DemodulatorThreadCommand() : cmd(cmd), int_value(SDR_THREAD_CMD_NULL) {
}
DemodulatorThreadCommand(DemodulatorThreadCommandEnum cmd) : cmd(cmd), int_value(0) {
}
DemodulatorThreadCommandEnum cmd;
int int_value;
};
class DemodulatorThreadIQData {
public:
unsigned int frequency;
@@ -65,18 +85,16 @@ public:
class DemodulatorThreadParameters {
public:
unsigned int frequency;
unsigned int inputRate;
unsigned int inputResampleRate; // set equal to disable second stage re-sampling?
unsigned int demodResampleRate;
unsigned int filterFrequency;
unsigned int bandwidth; // set equal to disable second stage re-sampling?
unsigned int audioSampleRate;
AudioThreadInputQueue *audioInputQueue;
DemodulatorType demodType;
DemodulatorThreadParameters() :
audioInputQueue(NULL), inputRate(SRATE), inputResampleRate(200000), demodResampleRate(100000), audioSampleRate(AUDIO_FREQUENCY), filterFrequency(
32000), demodType(DEMOD_TYPE_WFM) {
frequency(0), audioInputQueue(NULL), inputRate(SRATE), bandwidth(200000), audioSampleRate(AUDIO_FREQUENCY), demodType(DEMOD_TYPE_FM) {
}
@@ -87,11 +105,12 @@ public:
typedef ThreadQueue<DemodulatorThreadIQData> DemodulatorThreadInputQueue;
typedef ThreadQueue<AudioThreadInput> DemodulatorThreadOutputQueue;
typedef ThreadQueue<DemodulatorThreadCommand> DemodulatorThreadCommandQueue;
class DemodulatorThread {
public:
DemodulatorThread(DemodulatorThreadInputQueue* pQueue, DemodulatorThreadParameters *params);
DemodulatorThread(DemodulatorThreadInputQueue* pQueue);
~DemodulatorThread();
void threadMain();
@@ -101,21 +120,28 @@ public:
visOutQueue->set_max_num_items(1);
}
void setCommandQueue(DemodulatorThreadCommandQueue *tQueue) {
commandQueue = tQueue;
}
DemodulatorThreadParameters &getParams() {
return params;
}
void initialize();
void terminate();
protected:
DemodulatorThreadInputQueue* inputQueue;
DemodulatorThreadOutputQueue* visOutQueue;
DemodulatorThreadCommandQueue* commandQueue;
firfilt_crcf fir_filter;
firfilt_crcf fir_audio_filter;
msresamp_crcf resampler;
float resample_ratio;
msresamp_crcf second_resampler;
float second_resampler_ratio;
msresamp_crcf audio_resampler;
float audio_resample_ratio;
@@ -123,4 +149,5 @@ protected:
freqdem fdem;
std::atomic<bool> terminated;
std::atomic<bool> initialized;
};