2014-11-15 23:41:41 -05:00
|
|
|
#include "SpectrumCanvas.h"
|
|
|
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !wxUSE_GLCANVAS
|
|
|
|
#error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "CubicSDR.h"
|
|
|
|
#include "CubicSDRDefs.h"
|
|
|
|
#include "AppFrame.h"
|
|
|
|
#include <algorithm>
|
2014-11-25 00:35:06 -05:00
|
|
|
#include <wx/numformatter.h>
|
2015-01-01 21:10:54 -05:00
|
|
|
#include "WaterfallCanvas.h"
|
2014-11-15 23:41:41 -05:00
|
|
|
|
|
|
|
wxBEGIN_EVENT_TABLE(SpectrumCanvas, wxGLCanvas) EVT_PAINT(SpectrumCanvas::OnPaint)
|
|
|
|
EVT_IDLE(SpectrumCanvas::OnIdle)
|
2015-01-03 17:07:39 -05:00
|
|
|
EVT_MOTION(SpectrumCanvas::OnMouseMoved)
|
|
|
|
EVT_LEFT_DOWN(SpectrumCanvas::OnMouseDown)
|
|
|
|
EVT_LEFT_UP(SpectrumCanvas::OnMouseReleased)
|
|
|
|
EVT_LEAVE_WINDOW(SpectrumCanvas::OnMouseLeftWindow)
|
|
|
|
EVT_MOUSEWHEEL(SpectrumCanvas::OnMouseWheelMoved)
|
2014-11-15 23:41:41 -05:00
|
|
|
wxEND_EVENT_TABLE()
|
|
|
|
|
|
|
|
SpectrumCanvas::SpectrumCanvas(wxWindow *parent, int *attribList) :
|
2015-01-02 21:32:24 -05:00
|
|
|
InteractiveCanvas(parent, attribList), fft_size(0), in(NULL), out(NULL), plan(NULL), fft_ceil_ma(1), fft_ceil_maa(1), fft_floor_ma(0), fft_floor_maa(
|
|
|
|
0), waterfallCanvas(NULL) {
|
2014-11-15 23:41:41 -05:00
|
|
|
|
|
|
|
glContext = new SpectrumContext(this, &wxGetApp().GetContext(this));
|
2014-11-25 00:35:06 -05:00
|
|
|
|
2015-01-03 17:07:39 -05:00
|
|
|
mouseTracker.setVertDragLock(true);
|
2014-11-25 22:51:14 -05:00
|
|
|
|
2014-11-25 00:35:06 -05:00
|
|
|
SetCursor(wxCURSOR_SIZEWE);
|
2014-11-15 23:41:41 -05:00
|
|
|
}
|
|
|
|
|
2015-01-03 17:07:39 -05:00
|
|
|
void SpectrumCanvas::setup(int fft_size_in) {
|
2014-12-28 05:13:46 -05:00
|
|
|
if (fft_size == fft_size_in) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fft_size = fft_size_in;
|
|
|
|
|
|
|
|
if (in) {
|
|
|
|
free(in);
|
|
|
|
}
|
2015-01-06 19:15:27 -05:00
|
|
|
in = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * fft_size);
|
2014-12-28 05:13:46 -05:00
|
|
|
if (out) {
|
|
|
|
free(out);
|
|
|
|
}
|
2015-01-06 19:15:27 -05:00
|
|
|
out = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * fft_size);
|
2014-12-28 05:13:46 -05:00
|
|
|
if (plan) {
|
2015-01-06 19:15:27 -05:00
|
|
|
fftwf_destroy_plan(plan);
|
2014-12-28 05:13:46 -05:00
|
|
|
}
|
2015-01-06 19:15:27 -05:00
|
|
|
plan = fftwf_plan_dft_1d(fft_size, in, out, FFTW_FORWARD, FFTW_MEASURE);
|
2014-12-28 05:13:46 -05:00
|
|
|
|
|
|
|
fft_ceil_ma = fft_ceil_maa = 100.0;
|
|
|
|
fft_floor_ma = fft_floor_maa = 0.0;
|
|
|
|
}
|
|
|
|
|
2014-11-15 23:41:41 -05:00
|
|
|
SpectrumCanvas::~SpectrumCanvas() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpectrumCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) {
|
|
|
|
wxPaintDC dc(this);
|
2015-02-10 23:49:34 -05:00
|
|
|
#ifdef __APPLE__ // force half-rate?
|
|
|
|
glFinish();
|
|
|
|
#endif
|
2014-11-15 23:41:41 -05:00
|
|
|
const wxSize ClientSize = GetClientSize();
|
|
|
|
|
|
|
|
glContext->SetCurrent(*this);
|
2015-02-09 20:49:21 -05:00
|
|
|
initGLExtensions();
|
|
|
|
|
2014-11-15 23:41:41 -05:00
|
|
|
glViewport(0, 0, ClientSize.x, ClientSize.y);
|
|
|
|
|
2015-01-15 00:59:33 -05:00
|
|
|
glContext->BeginDraw(ThemeMgr::mgr.currentTheme->fftBackground.r, ThemeMgr::mgr.currentTheme->fftBackground.g, ThemeMgr::mgr.currentTheme->fftBackground.b);
|
2015-01-03 17:07:39 -05:00
|
|
|
glContext->Draw(spectrum_points, getCenterFrequency(), getBandwidth());
|
2014-11-15 23:41:41 -05:00
|
|
|
|
2014-12-08 21:08:03 -05:00
|
|
|
std::vector<DemodulatorInstance *> &demods = wxGetApp().getDemodMgr().getDemodulators();
|
|
|
|
|
|
|
|
for (int i = 0, iMax = demods.size(); i < iMax; i++) {
|
2015-01-14 22:14:57 -05:00
|
|
|
glContext->DrawDemodInfo(demods[i], ThemeMgr::mgr.currentTheme->fftHighlight.r, ThemeMgr::mgr.currentTheme->fftHighlight.g, ThemeMgr::mgr.currentTheme->fftHighlight.b, getCenterFrequency(), getBandwidth());
|
2014-12-08 21:08:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
glContext->EndDraw();
|
|
|
|
|
2014-11-15 23:41:41 -05:00
|
|
|
SwapBuffers();
|
|
|
|
}
|
|
|
|
|
2014-12-28 05:13:46 -05:00
|
|
|
void SpectrumCanvas::setData(DemodulatorThreadIQData *input) {
|
|
|
|
if (!input) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::vector<liquid_float_complex> *data = &input->data;
|
2014-11-15 23:41:41 -05:00
|
|
|
if (data && data->size()) {
|
2014-12-28 05:13:46 -05:00
|
|
|
if (fft_size != data->size()) {
|
2015-01-03 17:07:39 -05:00
|
|
|
setup(data->size());
|
2014-12-28 05:13:46 -05:00
|
|
|
}
|
|
|
|
if (spectrum_points.size() < fft_size * 2) {
|
|
|
|
if (spectrum_points.capacity() < fft_size * 2) {
|
|
|
|
spectrum_points.reserve(fft_size * 2);
|
|
|
|
}
|
|
|
|
spectrum_points.resize(fft_size * 2);
|
2014-11-15 23:41:41 -05:00
|
|
|
}
|
|
|
|
|
2014-12-28 05:13:46 -05:00
|
|
|
for (int i = 0; i < fft_size; i++) {
|
2014-12-26 16:15:35 -05:00
|
|
|
in[i][0] = (*data)[i].real;
|
|
|
|
in[i][1] = (*data)[i].imag;
|
2014-11-15 23:41:41 -05:00
|
|
|
}
|
|
|
|
|
2015-01-06 19:15:27 -05:00
|
|
|
fftwf_execute(plan);
|
2014-11-15 23:41:41 -05:00
|
|
|
|
2015-01-06 19:15:27 -05:00
|
|
|
float fft_ceil = 0, fft_floor = 1;
|
2014-11-15 23:41:41 -05:00
|
|
|
|
2014-12-28 05:13:46 -05:00
|
|
|
if (fft_result.size() != fft_size) {
|
|
|
|
if (fft_result.capacity() < fft_size) {
|
|
|
|
fft_result.reserve(fft_size);
|
|
|
|
fft_result_ma.reserve(fft_size);
|
|
|
|
fft_result_maa.reserve(fft_size);
|
|
|
|
}
|
|
|
|
fft_result.resize(fft_size);
|
|
|
|
fft_result_ma.resize(fft_size);
|
|
|
|
fft_result_maa.resize(fft_size);
|
2014-11-15 23:41:41 -05:00
|
|
|
}
|
|
|
|
|
2014-11-29 13:58:20 -05:00
|
|
|
int n;
|
2014-12-28 05:13:46 -05:00
|
|
|
for (int i = 0, iMax = fft_size / 2; i < iMax; i++) {
|
2014-12-08 21:08:03 -05:00
|
|
|
n = (i == 0) ? 1 : i;
|
2015-01-06 19:15:27 -05:00
|
|
|
float a = out[n][0];
|
|
|
|
float b = out[n][1];
|
|
|
|
float c = sqrt(a * a + b * b);
|
2014-11-29 13:58:20 -05:00
|
|
|
|
2015-01-06 19:15:27 -05:00
|
|
|
float x = out[fft_size / 2 + n][0];
|
|
|
|
float y = out[fft_size / 2 + n][1];
|
|
|
|
float z = sqrt(x * x + y * y);
|
2014-11-29 13:58:20 -05:00
|
|
|
|
|
|
|
fft_result[i] = (z);
|
2014-12-28 05:13:46 -05:00
|
|
|
fft_result[fft_size / 2 + i] = (c);
|
2014-11-15 23:41:41 -05:00
|
|
|
}
|
|
|
|
|
2014-12-28 05:13:46 -05:00
|
|
|
for (int i = 0, iMax = fft_size; i < iMax; i++) {
|
2014-11-15 23:41:41 -05:00
|
|
|
fft_result_maa[i] += (fft_result_ma[i] - fft_result_maa[i]) * 0.65;
|
|
|
|
fft_result_ma[i] += (fft_result[i] - fft_result_ma[i]) * 0.65;
|
|
|
|
|
|
|
|
if (fft_result_maa[i] > fft_ceil) {
|
|
|
|
fft_ceil = fft_result_maa[i];
|
|
|
|
}
|
|
|
|
if (fft_result_maa[i] < fft_floor) {
|
|
|
|
fft_floor = fft_result_maa[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fft_ceil += 1;
|
|
|
|
fft_floor -= 1;
|
|
|
|
|
|
|
|
fft_ceil_ma = fft_ceil_ma + (fft_ceil - fft_ceil_ma) * 0.01;
|
|
|
|
fft_ceil_maa = fft_ceil_maa + (fft_ceil_ma - fft_ceil_maa) * 0.01;
|
|
|
|
|
|
|
|
fft_floor_ma = fft_floor_ma + (fft_floor - fft_floor_ma) * 0.01;
|
|
|
|
fft_floor_maa = fft_floor_maa + (fft_floor_ma - fft_floor_maa) * 0.01;
|
|
|
|
|
2015-01-06 19:15:27 -05:00
|
|
|
// fftwf_execute(plan[1]);
|
2014-11-15 23:41:41 -05:00
|
|
|
|
2014-12-28 05:13:46 -05:00
|
|
|
for (int i = 0, iMax = fft_size; i < iMax; i++) {
|
2014-11-15 23:41:41 -05:00
|
|
|
float v = (log10(fft_result_maa[i] - fft_floor_maa) / log10(fft_ceil_maa - fft_floor_maa));
|
|
|
|
spectrum_points[i * 2] = ((float) i / (float) iMax);
|
|
|
|
spectrum_points[i * 2 + 1] = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpectrumCanvas::OnIdle(wxIdleEvent &event) {
|
2014-11-25 22:51:14 -05:00
|
|
|
Refresh(false);
|
2014-11-15 23:41:41 -05:00
|
|
|
}
|
2014-11-25 00:35:06 -05:00
|
|
|
|
2015-01-03 17:07:39 -05:00
|
|
|
void SpectrumCanvas::OnMouseMoved(wxMouseEvent& event) {
|
|
|
|
InteractiveCanvas::OnMouseMoved(event);
|
|
|
|
if (mouseTracker.mouseDown()) {
|
|
|
|
int freqChange = mouseTracker.getDeltaMouseX() * getBandwidth();
|
2014-11-25 22:51:14 -05:00
|
|
|
|
|
|
|
if (freqChange != 0) {
|
2015-01-04 17:11:20 -05:00
|
|
|
long long freq = wxGetApp().getFrequency();
|
2014-11-25 22:51:14 -05:00
|
|
|
|
2015-01-01 21:10:54 -05:00
|
|
|
if (isView) {
|
2015-01-06 19:15:27 -05:00
|
|
|
if (isView) {
|
|
|
|
if (centerFreq - freqChange < bandwidth/2) {
|
|
|
|
centerFreq = bandwidth/2;
|
|
|
|
} else {
|
|
|
|
centerFreq -= freqChange;
|
|
|
|
}
|
|
|
|
}
|
2015-01-01 21:10:54 -05:00
|
|
|
if (waterfallCanvas) {
|
2015-01-03 17:07:39 -05:00
|
|
|
waterfallCanvas->setCenterFrequency(centerFreq);
|
2015-01-01 21:10:54 -05:00
|
|
|
}
|
|
|
|
|
2015-01-04 17:11:20 -05:00
|
|
|
long long bwOfs = (centerFreq > freq) ? ((long long) bandwidth / 2) : (-(long long) bandwidth / 2);
|
|
|
|
long long freqEdge = centerFreq + bwOfs;
|
2015-01-02 02:44:50 -05:00
|
|
|
|
2015-01-11 17:08:16 -05:00
|
|
|
if (abs(freq - freqEdge) > (wxGetApp().getSampleRate() / 2)) {
|
|
|
|
freqChange = -((centerFreq > freq) ? (freqEdge - freq - (wxGetApp().getSampleRate() / 2)) : (freqEdge - freq + (wxGetApp().getSampleRate() / 2)));
|
2015-01-01 21:10:54 -05:00
|
|
|
} else {
|
|
|
|
freqChange = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freqChange) {
|
2015-01-11 17:08:16 -05:00
|
|
|
if (freq - freqChange < wxGetApp().getSampleRate()/2) {
|
|
|
|
freq = wxGetApp().getSampleRate()/2;
|
2015-01-04 13:20:31 -05:00
|
|
|
} else {
|
|
|
|
freq -= freqChange;
|
|
|
|
}
|
2015-01-01 21:10:54 -05:00
|
|
|
wxGetApp().setFrequency(freq);
|
2015-01-02 22:44:09 -05:00
|
|
|
setStatusText("Set center frequency: %s", freq);
|
2015-01-01 21:10:54 -05:00
|
|
|
}
|
|
|
|
|
2014-11-25 00:35:06 -05:00
|
|
|
}
|
2015-01-02 22:44:09 -05:00
|
|
|
} else {
|
|
|
|
setStatusText("Click and drag to adjust center frequency.");
|
2014-11-25 00:35:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 17:07:39 -05:00
|
|
|
void SpectrumCanvas::OnMouseDown(wxMouseEvent& event) {
|
|
|
|
InteractiveCanvas::OnMouseDown(event);
|
2014-11-25 00:35:06 -05:00
|
|
|
SetCursor(wxCURSOR_CROSS);
|
|
|
|
}
|
|
|
|
|
2015-01-03 17:07:39 -05:00
|
|
|
void SpectrumCanvas::OnMouseWheelMoved(wxMouseEvent& event) {
|
|
|
|
InteractiveCanvas::OnMouseWheelMoved(event);
|
2014-11-25 00:35:06 -05:00
|
|
|
}
|
|
|
|
|
2015-01-03 17:07:39 -05:00
|
|
|
void SpectrumCanvas::OnMouseReleased(wxMouseEvent& event) {
|
|
|
|
InteractiveCanvas::OnMouseReleased(event);
|
2014-11-25 00:35:06 -05:00
|
|
|
SetCursor(wxCURSOR_SIZEWE);
|
|
|
|
}
|
|
|
|
|
2015-01-03 17:07:39 -05:00
|
|
|
void SpectrumCanvas::OnMouseLeftWindow(wxMouseEvent& event) {
|
|
|
|
InteractiveCanvas::OnMouseLeftWindow(event);
|
2014-11-25 00:35:06 -05:00
|
|
|
SetCursor(wxCURSOR_SIZEWE);
|
|
|
|
}
|
|
|
|
|
2015-01-01 21:10:54 -05:00
|
|
|
void SpectrumCanvas::attachWaterfallCanvas(WaterfallCanvas* canvas_in) {
|
|
|
|
waterfallCanvas = canvas_in;
|
|
|
|
}
|
2014-11-25 00:35:06 -05:00
|
|
|
|