Skip to content

Commit

Permalink
Soapy: added set vs. actual center frequency check (#371)
Browse files Browse the repository at this point in the history
* Soapy: added set vs. actual center frequency

Signed-off-by: rstein <[email protected]>

---------

Signed-off-by: rstein <[email protected]>
  • Loading branch information
RalphSteinhagen committed Jun 27, 2024
1 parent aad9159 commit 912b5f7
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions blocks/soapy/include/gnuradio-4.0/soapy/Soapy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <SoapySDR/Device.hpp> // needed for existing C++ return types that are ABI stable (i.e. header-only defined)

#include <gnuradio-4.0/Profiler.hpp>
#include <fmt/ranges.h>
#include <map>
#include <string>
#include <vector>
Expand All @@ -17,6 +17,12 @@

namespace gr::blocks::soapy {

namespace detail {
bool equalWithinOnePercent(const std::vector<double>& a, const std::vector<double>& b) {
return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin(), [](double x, double y) { return std::abs(x - y) <= 0.01 * std::max(std::abs(x), std::abs(y)); });
}
} // namespace detail

template<typename T, std::size_t nPorts = std::dynamic_extent>
struct SoapyBlock : public Block<SoapyBlock<T, nPorts> /*, BlockingIO<false>*/> {
using Description = Doc<R""(A Soapy source block that interfaces with SDR hardware using the SoapySDR library.
Expand Down Expand Up @@ -78,11 +84,8 @@ This block supports multiple output ports and was tested against the 'rtlsdr' an
}

void start() { reinitDevice(); }

void pause() { _rxStream.deactivate(); }

void resume() { _rxStream.activate(); }

void stop() { _device.reset(); }

constexpr work::Status processBulk(PublishableSpan auto& output)
Expand Down Expand Up @@ -179,6 +182,15 @@ This block supports multiple output ports and was tested against the 'rtlsdr' an
for (std::size_t i = 0UZ; i < nChannels; i++) {
_device.setSampleRate(SOAPY_SDR_RX, rx_channels->at(i), static_cast<double>(sample_rate));
}

std::vector<double> actualSampleRates;
for (std::size_t i = 0UZ; i < nChannels; i++) {
actualSampleRates.push_back(_device.getSampleRate(SOAPY_SDR_RX, rx_channels->at(i)));
}

if (!detail::equalWithinOnePercent(actualSampleRates, std::vector<double>(nChannels, static_cast<double>(sample_rate)))) {
throw gr::exception(fmt::format("sample rate mismatch:\nSet: {} vs. Actual: {}\n", sample_rate, fmt::join(actualSampleRates, ", ")));
}
}

void setAntennae() {
Expand All @@ -201,6 +213,14 @@ This block supports multiple output ports and was tested against the 'rtlsdr' an
for (std::size_t i = 0UZ; i < nChannels; i++) {
_device.setCenterFrequency(SOAPY_SDR_RX, rx_channels->at(i), rx_center_frequency->at(std::min(i, nFrequency - 1UZ)));
}
std::vector<double> rx_center_frequency_actual;
for (std::size_t i = 0UZ; i < nChannels; i++) {
rx_center_frequency_actual.push_back(_device.getCenterFrequency(SOAPY_SDR_RX, rx_channels->at(i)));
}

if (!detail::equalWithinOnePercent(rx_center_frequency_actual, rx_center_frequency.value)) {
throw gr::exception(fmt::format("center frequency mismatch:\nSet: {} vs. Actual: {}\n", fmt::join(rx_center_frequency.value, ", "), fmt::join(rx_center_frequency_actual, ", ")));
}
}

void setBandwidth() {
Expand Down

0 comments on commit 912b5f7

Please sign in to comment.