CubicSDR/src/visual/SpectrumCanvas.cpp

250 lines
7.7 KiB
C++
Raw Normal View History

#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>
#include <wx/numformatter.h>
2015-01-01 21:10:54 -05:00
#include "WaterfallCanvas.h"
wxBEGIN_EVENT_TABLE(SpectrumCanvas, wxGLCanvas) EVT_PAINT(SpectrumCanvas::OnPaint)
EVT_IDLE(SpectrumCanvas::OnIdle)
EVT_MOTION(SpectrumCanvas::OnMouseMoved)
EVT_LEFT_DOWN(SpectrumCanvas::OnMouseDown)
EVT_LEFT_UP(SpectrumCanvas::OnMouseReleased)
EVT_LEAVE_WINDOW(SpectrumCanvas::OnMouseLeftWindow)
EVT_MOUSEWHEEL(SpectrumCanvas::OnMouseWheelMoved)
wxEND_EVENT_TABLE()
SpectrumCanvas::SpectrumCanvas(wxWindow *parent, int *attribList) :
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) {
glContext = new SpectrumContext(this, &wxGetApp().GetContext(this));
mouseTracker.setVertDragLock(true);
SetCursor(wxCURSOR_SIZEWE);
}
void SpectrumCanvas::setup(int fft_size_in) {
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);
if (out) {
free(out);
}
2015-01-06 19:15:27 -05:00
out = (fftwf_complex*) fftwf_malloc(sizeof(fftwf_complex) * fft_size);
if (plan) {
2015-01-06 19:15:27 -05:00
fftwf_destroy_plan(plan);
}
2015-01-06 19:15:27 -05:00
plan = fftwf_plan_dft_1d(fft_size, in, out, FFTW_FORWARD, FFTW_MEASURE);
fft_ceil_ma = fft_ceil_maa = 100.0;
fft_floor_ma = fft_floor_maa = 0.0;
}
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
const wxSize ClientSize = GetClientSize();
glContext->SetCurrent(*this);
initGLExtensions();
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);
glContext->Draw(spectrum_points, getCenterFrequency(), getBandwidth());
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++) {
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();
SwapBuffers();
}
void SpectrumCanvas::setData(DemodulatorThreadIQData *input) {
if (!input) {
return;
}
std::vector<liquid_float_complex> *data = &input->data;
if (data && data->size()) {
if (fft_size != data->size()) {
setup(data->size());
}
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);
}
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;
}
2015-01-06 19:15:27 -05:00
fftwf_execute(plan);
2015-01-06 19:15:27 -05:00
float fft_ceil = 0, fft_floor = 1;
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);
}
int n;
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);
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);
fft_result[i] = (z);
fft_result[fft_size / 2 + i] = (c);
}
for (int i = 0, iMax = fft_size; i < iMax; i++) {
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]);
for (int i = 0, iMax = fft_size; i < iMax; i++) {
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) {
Refresh(false);
}
void SpectrumCanvas::OnMouseMoved(wxMouseEvent& event) {
InteractiveCanvas::OnMouseMoved(event);
if (mouseTracker.mouseDown()) {
int freqChange = mouseTracker.getDeltaMouseX() * getBandwidth();
if (freqChange != 0) {
long long freq = wxGetApp().getFrequency();
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) {
waterfallCanvas->setCenterFrequency(centerFreq);
2015-01-01 21:10:54 -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;
} 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
}
}
2015-01-02 22:44:09 -05:00
} else {
setStatusText("Click and drag to adjust center frequency.");
}
}
void SpectrumCanvas::OnMouseDown(wxMouseEvent& event) {
InteractiveCanvas::OnMouseDown(event);
SetCursor(wxCURSOR_CROSS);
}
void SpectrumCanvas::OnMouseWheelMoved(wxMouseEvent& event) {
InteractiveCanvas::OnMouseWheelMoved(event);
}
void SpectrumCanvas::OnMouseReleased(wxMouseEvent& event) {
InteractiveCanvas::OnMouseReleased(event);
SetCursor(wxCURSOR_SIZEWE);
}
void SpectrumCanvas::OnMouseLeftWindow(wxMouseEvent& event) {
InteractiveCanvas::OnMouseLeftWindow(event);
SetCursor(wxCURSOR_SIZEWE);
}
2015-01-01 21:10:54 -05:00
void SpectrumCanvas::attachWaterfallCanvas(WaterfallCanvas* canvas_in) {
waterfallCanvas = canvas_in;
}