From dc81e2dd9ca7f19e64fd9a7027a2ef39f90e7298 Mon Sep 17 00:00:00 2001 From: f4exb Date: Thu, 1 Oct 2015 06:30:50 +0200 Subject: [PATCH] RTLSDR: implemented Fc position adjustment --- plugins/samplesource/rtlsdr/rtlsdrgui.cpp | 15 ++++ plugins/samplesource/rtlsdr/rtlsdrgui.h | 1 + plugins/samplesource/rtlsdr/rtlsdrgui.ui | 45 +++++++++- plugins/samplesource/rtlsdr/rtlsdrinput.cpp | 62 ++++++++++++- .../samplesource/rtlsdr/rtlsdrsettings.cpp | 6 ++ plugins/samplesource/rtlsdr/rtlsdrsettings.h | 7 ++ plugins/samplesource/rtlsdr/rtlsdrthread.cpp | 86 +++++++++++++++---- plugins/samplesource/rtlsdr/rtlsdrthread.h | 2 + 8 files changed, 203 insertions(+), 21 deletions(-) diff --git a/plugins/samplesource/rtlsdr/rtlsdrgui.cpp b/plugins/samplesource/rtlsdr/rtlsdrgui.cpp index 34d6e84ec..8024665f3 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrgui.cpp +++ b/plugins/samplesource/rtlsdr/rtlsdrgui.cpp @@ -136,6 +136,7 @@ void RTLSDRGui::displaySettings() ui->ppmText->setText(tr("%1").arg(m_settings.m_loPpmCorrection)); ui->decimText->setText(tr("%1").arg(1<decim->setValue(m_settings.m_log2Decim); + ui->fcPos->setCurrentIndex((int) m_settings.m_fcPos); if (m_gains.size() > 0) { @@ -191,6 +192,20 @@ void RTLSDRGui::on_decim_valueChanged(int value) sendSettings(); } +void RTLSDRGui::on_fcPos_currentIndexChanged(int index) +{ + if (index == 0) { + m_settings.m_fcPos = RTLSDRSettings::FC_POS_INFRA; + sendSettings(); + } else if (index == 1) { + m_settings.m_fcPos = RTLSDRSettings::FC_POS_SUPRA; + sendSettings(); + } else if (index == 2) { + m_settings.m_fcPos = RTLSDRSettings::FC_POS_CENTER; + sendSettings(); + } +} + void RTLSDRGui::on_ppm_valueChanged(int value) { if ((value > 99) || (value < -99)) diff --git a/plugins/samplesource/rtlsdr/rtlsdrgui.h b/plugins/samplesource/rtlsdr/rtlsdrgui.h index 14de077e0..92c3da350 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrgui.h +++ b/plugins/samplesource/rtlsdr/rtlsdrgui.h @@ -47,6 +47,7 @@ private slots: void on_dcOffset_toggled(bool checked); void on_iqImbalance_toggled(bool checked); void on_decim_valueChanged(int value); + void on_fcPos_currentIndexChanged(int index); void on_ppm_valueChanged(int value); void on_gain_valueChanged(int value); void on_samplerate_valueChanged(int value); diff --git a/plugins/samplesource/rtlsdr/rtlsdrgui.ui b/plugins/samplesource/rtlsdr/rtlsdrgui.ui index 71f2ec58a..5ed11a312 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrgui.ui +++ b/plugins/samplesource/rtlsdr/rtlsdrgui.ui @@ -7,7 +7,7 @@ 0 0 247 - 189 + 219 @@ -275,6 +275,49 @@ + + + + + + + Inf + + + + + Sup + + + + + Cent + + + + + + + + Fc pos + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + diff --git a/plugins/samplesource/rtlsdr/rtlsdrinput.cpp b/plugins/samplesource/rtlsdr/rtlsdrinput.cpp index fdeb0843c..a730f1fe3 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrinput.cpp +++ b/plugins/samplesource/rtlsdr/rtlsdrinput.cpp @@ -235,12 +235,13 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force) { if( rtlsdr_set_sample_rate(m_dev, settings.m_devSampleRate) < 0) { - qCritical("could not set sample rate: %d", settings.m_devSampleRate); + qCritical("RTLSDRInput::applySettings: could not set sample rate: %d", settings.m_devSampleRate); } else { m_settings.m_devSampleRate = settings.m_devSampleRate; m_rtlSDRThread->setSamplerate(settings.m_devSampleRate); + qDebug("RTLSDRInput::applySettings: sample rate set to %d", m_settings.m_devSampleRate); } } } @@ -276,8 +277,63 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force) forwardChange = true; } - m_settings.m_centerFrequency = settings.m_centerFrequency; + qint64 deviceCenterFrequency = m_settings.m_centerFrequency; + qint64 f_img = deviceCenterFrequency; + quint32 devSampleRate = m_settings.m_devSampleRate; + if (force || (m_settings.m_centerFrequency != settings.m_centerFrequency) + || (m_settings.m_fcPos != settings.m_fcPos)) + { + m_settings.m_centerFrequency = settings.m_centerFrequency; + + if ((m_settings.m_log2Decim == 0) || (settings.m_fcPos == RTLSDRSettings::FC_POS_CENTER)) + { + deviceCenterFrequency = m_settings.m_centerFrequency; + f_img = deviceCenterFrequency; + } + else + { + if (settings.m_fcPos == RTLSDRSettings::FC_POS_INFRA) + { + deviceCenterFrequency = m_settings.m_centerFrequency + (devSampleRate / 4); + f_img = deviceCenterFrequency + devSampleRate/2; + } + else if (settings.m_fcPos == RTLSDRSettings::FC_POS_SUPRA) + { + deviceCenterFrequency = m_settings.m_centerFrequency - (devSampleRate / 4); + f_img = deviceCenterFrequency - devSampleRate/2; + } + } + + if(m_dev != 0) + { + if (rtlsdr_set_center_freq( m_dev, deviceCenterFrequency ) != 0) + { + qDebug("rtlsdr_set_center_freq(%lld) failed", deviceCenterFrequency); + } + else + { + qDebug() << "RTLSDRInput::applySettings: center freq: " << m_settings.m_centerFrequency << " Hz" + << " device center freq: " << deviceCenterFrequency << " Hz" + << " device sample rate: " << devSampleRate << "Hz" + << " Actual sample rate: " << devSampleRate/(1<setFcPos((int) m_settings.m_fcPos); + qDebug() << "RTLSDRInput: set fc pos (enum) to " << (int) m_settings.m_fcPos; + } + } + + /* if(m_dev != 0) { qint64 centerFrequency = m_settings.m_centerFrequency + (m_settings.m_devSampleRate / 4); @@ -295,7 +351,7 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force) { qDebug("rtlsdr_set_center_freq(%lld) failed", m_settings.m_centerFrequency); } - } + }*/ if ((m_settings.m_dcBlock != settings.m_dcBlock) || force) { diff --git a/plugins/samplesource/rtlsdr/rtlsdrsettings.cpp b/plugins/samplesource/rtlsdr/rtlsdrsettings.cpp index 9f6b9dc53..38f97e533 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrsettings.cpp +++ b/plugins/samplesource/rtlsdr/rtlsdrsettings.cpp @@ -30,6 +30,7 @@ void RTLSDRSettings::resetToDefaults() m_gain = 0; m_loPpmCorrection = 0; m_log2Decim = 4; + m_fcPos = FC_POS_CENTER; m_dcBlock = false; m_iqImbalance = false; } @@ -44,6 +45,7 @@ QByteArray RTLSDRSettings::serialize() const s.writeU32(4, m_log2Decim); s.writeBool(5, m_dcBlock); s.writeBool(6, m_iqImbalance); + s.writeS32(7, (int) m_fcPos); return s.final(); } @@ -60,12 +62,16 @@ bool RTLSDRSettings::deserialize(const QByteArray& data) if (d.getVersion() == 1) { + int intval; + d.readS32(1, &m_devSampleRate, 0); d.readS32(2, &m_gain, 0); d.readS32(3, &m_loPpmCorrection, 0); d.readU32(4, &m_log2Decim, 4); d.readBool(5, &m_dcBlock, false); d.readBool(6, &m_iqImbalance, false); + d.readS32(7, &intval, 0); + m_fcPos = (fcPos_t) intval; return true; } diff --git a/plugins/samplesource/rtlsdr/rtlsdrsettings.h b/plugins/samplesource/rtlsdr/rtlsdrsettings.h index e8ec4714c..627166079 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrsettings.h +++ b/plugins/samplesource/rtlsdr/rtlsdrsettings.h @@ -18,11 +18,18 @@ #define _RTLSDR_RTLSDRSETTINGS_H_ struct RTLSDRSettings { + typedef enum { + FC_POS_INFRA = 0, + FC_POS_SUPRA, + FC_POS_CENTER + } fcPos_t; + int m_devSampleRate; quint64 m_centerFrequency; qint32 m_gain; qint32 m_loPpmCorrection; quint32 m_log2Decim; + fcPos_t m_fcPos; bool m_dcBlock; bool m_iqImbalance; diff --git a/plugins/samplesource/rtlsdr/rtlsdrthread.cpp b/plugins/samplesource/rtlsdr/rtlsdrthread.cpp index 6af8dd5cd..80a75c2ef 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrthread.cpp +++ b/plugins/samplesource/rtlsdr/rtlsdrthread.cpp @@ -63,6 +63,11 @@ void RTLSDRThread::setLog2Decimation(unsigned int log2_decim) m_log2Decim = log2_decim; } +void RTLSDRThread::setFcPos(int fcPos) +{ + m_fcPos = fcPos; +} + void RTLSDRThread::run() { int res; @@ -85,25 +90,72 @@ void RTLSDRThread::callback(const quint8* buf, qint32 len) { SampleVector::iterator it = m_convertBuffer.begin(); - switch (m_log2Decim) + if (m_log2Decim == 0) { - case 0: m_decimators.decimate1(&it, buf, len); - break; - case 1: - m_decimators.decimate2_inf(&it, buf, len); - break; - case 2: - m_decimators.decimate4_inf(&it, buf, len); - break; - case 3: - m_decimators.decimate8_inf(&it, buf, len); - break; - case 4: - m_decimators.decimate16_inf(&it, buf, len); - break; - default: - break; + } + else + { + if (m_fcPos == 0) // Infradyne + { + switch (m_log2Decim) + { + case 1: + m_decimators.decimate2_inf(&it, buf, len); + break; + case 2: + m_decimators.decimate4_inf(&it, buf, len); + break; + case 3: + m_decimators.decimate8_inf(&it, buf, len); + break; + case 4: + m_decimators.decimate16_inf(&it, buf, len); + break; + default: + break; + } + } + else if (m_fcPos == 1) // Supradyne + { + switch (m_log2Decim) + { + case 1: + m_decimators.decimate2_sup(&it, buf, len); + break; + case 2: + m_decimators.decimate4_sup(&it, buf, len); + break; + case 3: + m_decimators.decimate8_sup(&it, buf, len); + break; + case 4: + m_decimators.decimate16_sup(&it, buf, len); + break; + default: + break; + } + } + else // Centered + { + switch (m_log2Decim) + { + case 1: + m_decimators.decimate2_cen(&it, buf, len); + break; + case 2: + m_decimators.decimate4_cen(&it, buf, len); + break; + case 3: + m_decimators.decimate8_cen(&it, buf, len); + break; + case 4: + m_decimators.decimate16_cen(&it, buf, len); + break; + default: + break; + } + } } m_sampleFifo->write(m_convertBuffer.begin(), it); diff --git a/plugins/samplesource/rtlsdr/rtlsdrthread.h b/plugins/samplesource/rtlsdr/rtlsdrthread.h index 01f5806c1..d7a4caa8c 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrthread.h +++ b/plugins/samplesource/rtlsdr/rtlsdrthread.h @@ -37,6 +37,7 @@ public: void stopWork(); void setSamplerate(int samplerate); void setLog2Decimation(unsigned int log2_decim); + void setFcPos(int fcPos); private: QMutex m_startWaitMutex; @@ -49,6 +50,7 @@ private: int m_samplerate; unsigned int m_log2Decim; + int m_fcPos; Decimators m_decimators;