From 91089b6809ba85776d3603f14e8c2b302cf6c716 Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 27 Apr 2019 01:19:17 +0200 Subject: [PATCH] Remote sink: fixed chanel frequency shift calculation and access --- plugins/channelrx/remotesink/remotesink.cpp | 13 +++++- plugins/channelrx/remotesink/remotesink.h | 4 +- sdrbase/dsp/hbfilterchainconverter.cpp | 49 +++++++++++++++++---- sdrbase/dsp/hbfilterchainconverter.h | 2 + 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/plugins/channelrx/remotesink/remotesink.cpp b/plugins/channelrx/remotesink/remotesink.cpp index 4d06230bb..23c02c243 100644 --- a/plugins/channelrx/remotesink/remotesink.cpp +++ b/plugins/channelrx/remotesink/remotesink.cpp @@ -38,6 +38,7 @@ #include "dsp/threadedbasebandsamplesink.h" #include "dsp/downchannelizer.h" #include "dsp/dspcommands.h" +#include "dsp/hbfilterchainconverter.h" #include "device/devicesourceapi.h" #include "../remotesink/remotesinkthread.h" @@ -61,6 +62,7 @@ RemoteSink::RemoteSink(DeviceSourceAPI *deviceAPI) : m_centerFrequency(0), m_frequencyOffset(0), m_sampleRate(48000), + m_deviceSampleRate(48000), m_nbBlocksFEC(0), m_txDelay(35), m_dataAddress("127.0.0.1"), @@ -273,7 +275,6 @@ bool RemoteSink::handleMessage(const Message& cmd) } setTxDelay(m_settings.m_txDelay, m_settings.m_nbFECBlocks); - m_frequencyOffset = notif.getFrequencyOffset(); return true; } @@ -286,6 +287,8 @@ bool RemoteSink::handleMessage(const Message& cmd) << " centerFrequency: " << notif.getCenterFrequency(); setCenterFrequency(notif.getCenterFrequency()); + m_deviceSampleRate = notif.getSampleRate(); + calculateFrequencyOffset(); // This is when device sample rate changes // Redo the channelizer stuff with the new sample rate to re-synchronize everything m_channelizer->set(m_channelizer->getInputMessageQueue(), @@ -322,6 +325,8 @@ bool RemoteSink::handleMessage(const Message& cmd) m_settings.m_log2Decim, m_settings.m_filterChainHash); + calculateFrequencyOffset(); // This is when decimation or filter chain changes + return true; } else @@ -413,6 +418,12 @@ void RemoteSink::validateFilterChainHash(RemoteSinkSettings& settings) settings.m_filterChainHash = settings.m_filterChainHash >= s ? s-1 : settings.m_filterChainHash; } +void RemoteSink::calculateFrequencyOffset() +{ + double shiftFactor = HBFilterChainConverter::getShiftFactor(m_settings.m_log2Decim, m_settings.m_filterChainHash); + m_frequencyOffset = m_deviceSampleRate * shiftFactor; +} + int RemoteSink::webapiSettingsGet( SWGSDRangel::SWGChannelSettings& response, QString& errorMessage) diff --git a/plugins/channelrx/remotesink/remotesink.h b/plugins/channelrx/remotesink/remotesink.h index 446784d73..2551471ab 100644 --- a/plugins/channelrx/remotesink/remotesink.h +++ b/plugins/channelrx/remotesink/remotesink.h @@ -120,7 +120,7 @@ public: virtual void getIdentifier(QString& id) { id = objectName(); } virtual void getTitle(QString& title) { title = "Remote Sink"; } - virtual qint64 getCenterFrequency() const { return 0; } + virtual qint64 getCenterFrequency() const { return m_frequencyOffset; } virtual QByteArray serialize() const; virtual bool deserialize(const QByteArray& data); @@ -173,6 +173,7 @@ private: uint64_t m_centerFrequency; int64_t m_frequencyOffset; uint32_t m_sampleRate; + uint32_t m_deviceSampleRate; int m_nbBlocksFEC; int m_txDelay; QString m_dataAddress; @@ -182,6 +183,7 @@ private: void applySettings(const RemoteSinkSettings& settings, bool force = false); void validateFilterChainHash(RemoteSinkSettings& settings); + void calculateFrequencyOffset(); void webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings& response, const RemoteSinkSettings& settings); void webapiReverseSendSettings(QList& channelSettingsKeys, const RemoteSinkSettings& settings, bool force); diff --git a/sdrbase/dsp/hbfilterchainconverter.cpp b/sdrbase/dsp/hbfilterchainconverter.cpp index 0cb7a58cc..1e984b42a 100644 --- a/sdrbase/dsp/hbfilterchainconverter.cpp +++ b/sdrbase/dsp/hbfilterchainconverter.cpp @@ -28,13 +28,10 @@ double HBFilterChainConverter::convertToIndexes(unsigned int log2, unsigned int } unsigned int s = 1; - unsigned int d = 1; unsigned int u = chainHash; - for (unsigned int i = 0; i < log2; i++) - { + for (unsigned int i = 0; i < log2; i++) { s *= 3; - d *= 2; } u %= s; // scale @@ -73,14 +70,11 @@ double HBFilterChainConverter::convertToString(unsigned int log2, unsigned int c } unsigned int s = 1; - unsigned int d = 1; unsigned int u = chainHash; chainString = ""; - for (unsigned int i = 0; i < log2; i++) - { + for (unsigned int i = 0; i < log2; i++) { s *= 3; - d *= 2; } u %= s; // scale @@ -115,5 +109,44 @@ double HBFilterChainConverter::convertToString(unsigned int log2, unsigned int c shift_stage *= 2; } + return shift; +} + +double HBFilterChainConverter::getShiftFactor(unsigned int log2, unsigned int chainHash) +{ + if (log2 == 0) + { + return 0.0; + } + + unsigned int s = 1; + unsigned int u = chainHash; + + for (unsigned int i = 0; i < log2; i++) { + s *= 3; + } + + u %= s; // scale + unsigned int ix = log2; + double shift = 0.0; + double shift_stage = 1.0 / (1<<(log2+1)); + + // base3 conversion + do + { + int r = u % 3; + shift += (r-1)*shift_stage; + shift_stage *= 2; + u /= 3; + ix--; + } while (u); + + // continue shift with leading zeroes. ix has the number of leading zeroes. + for (unsigned int i = 0; i < ix; i++) + { + shift -= shift_stage; + shift_stage *= 2; + } + return shift; } \ No newline at end of file diff --git a/sdrbase/dsp/hbfilterchainconverter.h b/sdrbase/dsp/hbfilterchainconverter.h index c572261bf..39c381bb7 100644 --- a/sdrbase/dsp/hbfilterchainconverter.h +++ b/sdrbase/dsp/hbfilterchainconverter.h @@ -34,6 +34,8 @@ public: static double convertToIndexes(unsigned int log2, unsigned int chainHash, std::vector& chainIndexes); // Same but used only for display giving a string representation of the filter chain static double convertToString(unsigned int log2, unsigned int chainHash, QString& chainString); + // Just calculate the frequency shift factor relative to sample rate + static double getShiftFactor(unsigned int log2, unsigned int chainHash); }; #endif // SDRBASE_DSP_HBFILTERCHAINCONVERTER_H \ No newline at end of file