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

View File

@ -27,8 +27,9 @@ bool CubicSDR::OnInit() {
threadAudio = new std::thread(&AudioThread::threadMain, audioThread);
demodulatorTest = demodMgr.newThread();
demodulatorTest->params.audioInputQueue = audioInputQueue;
demodulatorTest->init();
demodulatorTest->getParams().audioInputQueue = audioInputQueue;
demodulatorTest->getParams().frequency = DEFAULT_FREQ;
demodulatorTest->run();
audioVisualQueue = new DemodulatorThreadOutputQueue();
demodulatorTest->setVisualOutputQueue(audioVisualQueue);
@ -87,6 +88,7 @@ PrimaryGLContext& CubicSDR::GetContext(wxGLCanvas *canvas) {
void CubicSDR::setFrequency(unsigned int freq) {
frequency = freq;
demodulatorTest->getParams().frequency = freq;
SDRThreadCommand command(SDRThreadCommand::SDR_THREAD_CMD_TUNE);
command.int_value = freq;
threadCmdQueueSDR->push(command);

View File

@ -40,6 +40,10 @@ public:
return demodulatorTest;
}
DemodulatorMgr &getDemodMgr() {
return demodMgr;
}
private:
PrimaryGLContext *m_glContext;

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;
}

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:

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);

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;
};

View File

@ -15,17 +15,21 @@ void MouseTracker::OnMouseMoved(wxMouseEvent& event) {
deltaMouseY = mouseY - lastMouseY;
if (isMouseDown) {
lastMouseX = mouseX;
if (vertDragLock && mouseY != lastMouseY) {
target->WarpPointer(event.m_x, lastMouseY * ClientSize.y);
} else {
lastMouseY = mouseY;
if (horizDragLock && vertDragLock) {
target->WarpPointer(originMouseX * ClientSize.x, originMouseY * ClientSize.y);
mouseX = originMouseX;
mouseY = originMouseY;
} else if (vertDragLock && mouseY != lastMouseY) {
target->WarpPointer(event.m_x, originMouseY * ClientSize.y);
mouseY = originMouseY;
} else if (horizDragLock && mouseX != lastMouseX) {
target->WarpPointer(originMouseX * ClientSize.x, event.m_y);
mouseX = originMouseX;
}
} else {
lastMouseY = mouseY;
lastMouseX = mouseX;
}
lastMouseX = mouseX;
lastMouseY = mouseY;
}
void MouseTracker::OnMouseDown(wxMouseEvent& event) {
@ -105,6 +109,10 @@ void MouseTracker::setVertDragLock(bool dragLock) {
vertDragLock = dragLock;
}
void MouseTracker::setHorizDragLock(bool dragLock) {
horizDragLock = dragLock;
}
bool MouseTracker::mouseDown() {
return isMouseDown;
}

View File

@ -6,13 +6,13 @@ class MouseTracker {
public:
MouseTracker(wxWindow *target) :
target(target), mouseX(0), mouseY(0), lastMouseX(0), lastMouseY(0), originMouseX(0), originMouseY(0), deltaMouseX(0), deltaMouseY(0), isMouseDown(
false), vertDragLock(false), isMouseInView(false) {
false), vertDragLock(false), horizDragLock(false), isMouseInView(false) {
}
MouseTracker() :
target(NULL), mouseX(0), mouseY(0), lastMouseX(0), lastMouseY(0), originMouseX(0), originMouseY(0), deltaMouseX(0), deltaMouseY(0), isMouseDown(
false), vertDragLock(false), isMouseInView(false) {
false), vertDragLock(false), horizDragLock(false), isMouseInView(false) {
}
@ -35,6 +35,7 @@ public:
float getMouseY();
void setVertDragLock(bool dragLock);
void setHorizDragLock(bool dragLock);
bool mouseDown();
bool mouseInView();
void setTarget(wxWindow *target_in);
@ -46,6 +47,6 @@ private:
float originMouseX, originMouseY;
bool isMouseDown, isMouseInView;
bool vertDragLock;
bool vertDragLock, horizDragLock;
wxWindow *target;
};

View File

@ -207,6 +207,15 @@ public:
return m_queue.empty();
}
/**
* Remove any items in the queue.
*/
void flush() const {
std::lock_guard < std::mutex > lock(m_mutex);
std::queue<T, Container> emptyQueue;
std::swap(m_queue, emptyQueue);
}
/**
* Swaps the contents.
* \param[out] sq The ThreadQueue to swap with 'this'.

View File

@ -29,7 +29,7 @@ wxEND_EVENT_TABLE()
WaterfallCanvas::WaterfallCanvas(wxWindow *parent, int *attribList) :
wxGLCanvas(parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize,
wxFULL_REPAINT_ON_RESIZE), parent(parent), frameTimer(0) {
wxFULL_REPAINT_ON_RESIZE), parent(parent), frameTimer(0), bwChange(false), demodBW(0) {
int in_block_size = BUF_SIZE / 2;
int out_block_size = FFT_SIZE;
@ -45,7 +45,8 @@ WaterfallCanvas::WaterfallCanvas(wxWindow *parent, int *attribList) :
timer.start();
mTracker.setTarget(this);
mTracker.setVertDragLock(true);
mTracker.setHorizDragLock(true);
SetCursor(wxCURSOR_CROSS);
}
@ -63,6 +64,12 @@ void WaterfallCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) {
glContext->BeginDraw();
glContext->Draw(spectrum_points);
std::vector<DemodulatorInstance *> *demods = &wxGetApp().getDemodMgr().getDemodulators();
for (int i = 0, iMax = demods->size(); i < iMax; i++) {
glContext->DrawDemod((*demods)[i]);
}
if (mTracker.mouseInView()) {
glContext->DrawFreqSelector(mTracker.getMouseX());
}
@ -180,34 +187,61 @@ void WaterfallCanvas::OnIdle(wxIdleEvent &event) {
void WaterfallCanvas::mouseMoved(wxMouseEvent& event) {
mTracker.OnMouseMoved(event);
DemodulatorInstance *demod = wxGetApp().getDemodTest();
if (mTracker.mouseDown()) {
int freqChange = mTracker.getDeltaMouseX() * SRATE;
if (demod && mTracker.getDeltaMouseY()) {
int bwDiff = (int)(mTracker.getDeltaMouseY() * 100000.0);
if (freqChange != 0) {
int freq = wxGetApp().getFrequency();
freq -= freqChange;
wxGetApp().setFrequency(freq);
if (!demodBW) {
demodBW = demod->getParams().bandwidth;
}
((wxFrame*) parent)->GetStatusBar()->SetStatusText(
wxString::Format(wxT("Set center frequency: %s"),
wxNumberFormatter::ToString((long) freq, wxNumberFormatter::Style_WithThousandsSep)));
DemodulatorThreadCommand command;
command.cmd = DemodulatorThreadCommand::SDR_THREAD_CMD_SETBANDWIDTH;
demodBW = demodBW - bwDiff;
if (demodBW < 1000) {
demodBW = 1000;
}
if (demodBW > SRATE) {
demodBW = SRATE;
}
command.int_value = demodBW;
demod->getCommandQueue()->push(command);
bwChange = true;
}
// int freqChange = mTracker.getDeltaMouseX() * SRATE;
//
// if (freqChange != 0) {
// int freq = wxGetApp().getFrequency();
// freq -= freqChange;
// wxGetApp().setFrequency(freq);
//
// ((wxFrame*) parent)->GetStatusBar()->SetStatusText(
// wxString::Format(wxT("Set center frequency: %s"),
// wxNumberFormatter::ToString((long) freq, wxNumberFormatter::Style_WithThousandsSep)));
// }
}
}
void WaterfallCanvas::mouseDown(wxMouseEvent& event) {
mTracker.OnMouseDown(event);
SetCursor(wxCURSOR_CROSS);
SetCursor(wxCURSOR_SIZENS);
bwChange = false;
}
void WaterfallCanvas::mouseWheelMoved(wxMouseEvent& event) {
DemodulatorInstance *demod = wxGetApp().getDemodTest();
mTracker.OnMouseWheelMoved(event);
}
void WaterfallCanvas::mouseReleased(wxMouseEvent& event) {
mTracker.OnMouseReleased(event);
if (mTracker.getOriginDeltaMouseX() == 0 && mTracker.getOriginDeltaMouseX() == 0) {
if (mTracker.getOriginDeltaMouseX() == 0 && mTracker.getOriginDeltaMouseY() == 0 && !bwChange) {
float pos = mTracker.getMouseX();
@ -222,15 +256,15 @@ void WaterfallCanvas::mouseReleased(wxMouseEvent& event) {
wxNumberFormatter::ToString((long) freq, wxNumberFormatter::Style_WithThousandsSep)));
}
SetCursor(wxCURSOR_SIZEWE);
SetCursor(wxCURSOR_CROSS);
}
void WaterfallCanvas::mouseLeftWindow(wxMouseEvent& event) {
mTracker.OnMouseLeftWindow(event);
SetCursor(wxCURSOR_SIZEWE);
SetCursor(wxCURSOR_CROSS);
}
void WaterfallCanvas::mouseEnterWindow(wxMouseEvent& event) {
mTracker.OnMouseEnterWindow(event);
SetCursor(wxCURSOR_SIZEWE);
SetCursor(wxCURSOR_CROSS);
}

View File

@ -48,6 +48,8 @@ private:
Timer timer;
float frameTimer;
MouseTracker mTracker;
bool bwChange;
int demodBW;
// event table
wxDECLARE_EVENT_TABLE();

View File

@ -82,14 +82,14 @@ void WaterfallContext::Draw(std::vector<float> &points) {
}
void WaterfallContext::DrawFreqSelector(float uxPos) {
DemodulatorInstance *demod = wxGetApp().getDemodTest();
void WaterfallContext::DrawDemod(DemodulatorInstance *demod) {
if (!demod) {
return;
}
glClear(GL_DEPTH_BUFFER_BIT);
float uxPos = (float) (demod->getParams().frequency - (wxGetApp().getFrequency() - SRATE / 2)) / (float) SRATE;
glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 1.0);
@ -98,7 +98,7 @@ void WaterfallContext::DrawFreqSelector(float uxPos) {
glVertex3f((uxPos - 0.5) * 2.0, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0, -1.0, 0.0);
float ofs = ((float) demod->params.inputResampleRate) / (float) SRATE;
float ofs = ((float) demod->getParams().bandwidth) / (float) SRATE;
glVertex3f((uxPos - 0.5) * 2.0 - ofs, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 - ofs, -1.0, 0.0);
@ -107,6 +107,36 @@ void WaterfallContext::DrawFreqSelector(float uxPos) {
glVertex3f((uxPos - 0.5) * 2.0 + ofs, -1.0, 0.0);
glEnd();
glEnable(GL_DEPTH_TEST);
}
void WaterfallContext::DrawFreqSelector(float uxPos) {
DemodulatorInstance *demod = wxGetApp().getDemodTest();
if (!demod) {
return;
}
glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f((uxPos - 0.5) * 2.0, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0, -1.0, 0.0);
float ofs = ((float) demod->getParams().bandwidth) / (float) SRATE;
glVertex3f((uxPos - 0.5) * 2.0 - ofs, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 - ofs, -1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 + ofs, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 + ofs, -1.0, 0.0);
glEnd();
glEnable(GL_DEPTH_TEST);
}

View File

@ -2,6 +2,7 @@
#include "PrimaryGLContext.h"
#include "Gradient.h"
#include "DemodulatorMgr.h"
#define NUM_WATERFALL_LINES 512
@ -14,6 +15,7 @@ public:
void BeginDraw();
void Draw(std::vector<float> &points);
void DrawFreqSelector(float uxPos);
void DrawDemod(DemodulatorInstance *demod);
void EndDraw();
private: