Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soapy: added set vs. actual center frequency check #371

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading