From b49d5c3898f4a6f03d7cb033508ba1416e30c6f0 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Sun, 26 Mar 2017 13:28:02 +0200 Subject: Working pipeline --- Makefile.am | 2 + src/DabModulator.cpp | 47 +++++++----- src/MemlessPoly.cpp | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/MemlessPoly.h | 76 +++++++++++++++++++ 4 files changed, 310 insertions(+), 17 deletions(-) create mode 100644 src/MemlessPoly.cpp create mode 100644 src/MemlessPoly.h diff --git a/Makefile.am b/Makefile.am index f4e8e00..d4365fd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -62,6 +62,8 @@ odr_dabmod_SOURCES = src/DabMod.cpp \ src/FicSource.h \ src/FIRFilter.cpp \ src/FIRFilter.h \ + src/MemlessPoly.cpp \ + src/MemlessPoly.h \ src/PuncturingRule.cpp \ src/PuncturingRule.h \ src/PuncturingEncoder.cpp \ diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index c41b8fc..bd14396 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -47,6 +47,7 @@ #include "Resampler.h" #include "ConvEncoder.h" #include "FIRFilter.h" +#include "MemlessPoly.h" #include "TII.h" #include "PuncturingEncoder.h" #include "TimeInterleaver.h" @@ -215,6 +216,11 @@ int DabModulator::process(Buffer* dataOut) cifFilter = make_shared(myFilterTapsFilename); rcs.enrol(cifFilter.get()); } + + shared_ptr cifPoly; + cifPoly = make_shared("default"); + rcs.enrol(cifPoly.get()); + auto myOutput = make_shared(dataOut); shared_ptr cifRes; @@ -348,23 +354,30 @@ int DabModulator::process(Buffer* dataOut) myFlowgraph->connect(cifOfdm, cifGain); myFlowgraph->connect(cifGain, cifGuard); - if (cifFilter) { - myFlowgraph->connect(cifGuard, cifFilter); - if (cifRes) { - myFlowgraph->connect(cifFilter, cifRes); - myFlowgraph->connect(cifRes, myOutput); - } else { - myFlowgraph->connect(cifFilter, myOutput); - } - } - else { //no filtering - if (cifRes) { - myFlowgraph->connect(cifGuard, cifRes); - myFlowgraph->connect(cifRes, myOutput); - } else { - myFlowgraph->connect(cifGuard, myOutput); - } - + //if (cifFilter) { + // myFlowgraph->connect(cifGuard, cifFilter); + // if (cifRes) { + // myFlowgraph->connect(cifFilter, cifRes); + // myFlowgraph->connect(cifRes, myOutput); + // } else { + // myFlowgraph->connect(cifFilter, myOutput); + // } + //} + //else { //no filtering + // if (cifRes) { + // myFlowgraph->connect(cifGuard, cifRes); + // myFlowgraph->connect(cifRes, myOutput); + // } else { + // myFlowgraph->connect(cifGuard, myOutput); + // } + //} + if (cifRes) { + myFlowgraph->connect(cifGuard, cifRes); + myFlowgraph->connect(cifRes, cifPoly); + myFlowgraph->connect(cifPoly, myOutput); + } else { + myFlowgraph->connect(cifGuard, cifPoly); + myFlowgraph->connect(cifPoly, myOutput); } } diff --git a/src/MemlessPoly.cpp b/src/MemlessPoly.cpp new file mode 100644 index 0000000..a34dde1 --- /dev/null +++ b/src/MemlessPoly.cpp @@ -0,0 +1,202 @@ +/* + Copyright (C) 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in + Right of Canada (Communications Research Center Canada) + + Copyright (C) 2017 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://opendigitalradio.org + + This block implements a FIR filter. The real filter taps are given + as floats, and the block can take advantage of SSE. + For better performance, filtering is done in another thread, leading + to a pipeline delay of two calls to MemlessPoly::process + */ +/* + This file is part of ODR-DabMod. + + ODR-DabMod is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + ODR-DabMod is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ODR-DabMod. If not, see . + */ + +#include "MemlessPoly.h" +#include "PcDebug.h" +#include "Utils.h" + +#include +#include + +#include +#include +#include +#include + +#ifdef __SSE__ +# include +#endif + +using namespace std; + +/* This is the FIR Filter calculated with the doc/fir-filter/generate-filter.py script + * with settings + * gain = 1 + * sampling_freq = 2.048e6 + * cutoff = 810e3 + * transition_width = 250e3 + * + * It is a good default filter for the common scenarios. + */ + + //0.8, -0.2, 0.2, 0.25, +static const std::array default_coefficients({ + 0.1, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0 + }); + + +MemlessPoly::MemlessPoly(const std::string& taps_file) : + PipelinedModCodec(), + RemoteControllable("memlesspoly"), + m_taps_file(taps_file) +{ + PDEBUG("MemlessPoly::MemlessPoly(%s) @ %p\n", + taps_file.c_str(), this); + + RC_ADD_PARAMETER(ntaps, "(Read-only) number of filter taps."); + RC_ADD_PARAMETER(tapsfile, "Filename containing filter taps. When written to, the new file gets automatically loaded."); + + load_filter_taps(m_taps_file); + + start_pipeline_thread(); +} + +void MemlessPoly::load_filter_taps(const std::string &tapsFile) +{ + std::vector filter_taps; + if (tapsFile == "default") { + std::copy(default_coefficients.begin(), default_coefficients.end(), + std::back_inserter(filter_taps)); + } + else { + std::ifstream taps_fstream(tapsFile.c_str()); + if(!taps_fstream) { + fprintf(stderr, "MemlessPoly: file %s could not be opened !\n", tapsFile.c_str()); + throw std::runtime_error("MemlessPoly: Could not open file with taps! "); + } + int n_taps; + taps_fstream >> n_taps; + + if (n_taps <= 0) { + fprintf(stderr, "MemlessPoly: warning: taps file has invalid format\n"); + throw std::runtime_error("MemlessPoly: taps file has invalid format."); + } + + if (n_taps > 100) { + fprintf(stderr, "MemlessPoly: warning: taps file has more than 100 taps\n"); + } + + fprintf(stderr, "MemlessPoly: Reading %d taps...\n", n_taps); + + filter_taps.resize(n_taps); + + int n; + for (n = 0; n < n_taps; n++) { + taps_fstream >> filter_taps[n]; + PDEBUG("MemlessPoly: tap: %f\n", filter_taps[n] ); + if (taps_fstream.eof()) { + fprintf(stderr, "MemlessPoly: file %s should contains %d taps, but EOF reached "\ + "after %d taps !\n", tapsFile.c_str(), n_taps, n); + throw std::runtime_error("MemlessPoly: filtertaps file invalid ! "); + } + } + } + + { + std::lock_guard lock(m_taps_mutex); + + m_taps = filter_taps; + } +} + + +int MemlessPoly::internal_process(Buffer* const dataIn, Buffer* dataOut) +{ + size_t i; + + const float* in = reinterpret_cast(dataIn->getData()); + float* out = reinterpret_cast(dataOut->getData()); + size_t sizeIn = dataIn->getLength() / sizeof(float); + + { + std::lock_guard lock(m_taps_mutex); + for (i = 0; i < sizeIn; i += 1) { + float mag = std::abs(in[i]); + //out[i] = in[i]; + out[i] = in[i] * ( + default_coefficients[0] + + default_coefficients[1] * mag + + default_coefficients[2] * mag*mag + + default_coefficients[3] * mag*mag*mag + + default_coefficients[4] * mag*mag*mag*mag + + default_coefficients[5] * mag*mag*mag*mag*mag + + default_coefficients[6] * mag*mag*mag*mag*mag*mag + + default_coefficients[7] * mag*mag*mag*mag*mag*mag*mag + ); + } + } + + return dataOut->getLength(); +} + +void MemlessPoly::set_parameter(const string& parameter, const string& value) +{ + stringstream ss(value); + ss.exceptions ( stringstream::failbit | stringstream::badbit ); + + if (parameter == "ntaps") { + throw ParameterError("Parameter 'ntaps' is read-only"); + } + else if (parameter == "tapsfile") { + try { + load_filter_taps(value); + m_taps_file = value; + } + catch (std::runtime_error &e) { + throw ParameterError(e.what()); + } + } + else { + stringstream ss; + ss << "Parameter '" << parameter << + "' is not exported by controllable " << get_rc_name(); + throw ParameterError(ss.str()); + } +} + +const string MemlessPoly::get_parameter(const string& parameter) const +{ + stringstream ss; + if (parameter == "ntaps") { + ss << m_taps.size(); + } + else if (parameter == "tapsfile") { + ss << m_taps_file; + } + else { + ss << "Parameter '" << parameter << + "' is not exported by controllable " << get_rc_name(); + throw ParameterError(ss.str()); + } + return ss.str(); +} + diff --git a/src/MemlessPoly.h b/src/MemlessPoly.h new file mode 100644 index 0000000..fe372d8 --- /dev/null +++ b/src/MemlessPoly.h @@ -0,0 +1,76 @@ +/* + Copyright (C) 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in + Right of Canada (Communications Research Center Canada) + + Copyright (C) 2017 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://opendigitalradio.org + */ +/* + This file is part of ODR-DabMod. + + ODR-DabMod is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + ODR-DabMod is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ODR-DabMod. If not, see . + */ + +#pragma once + +#ifdef HAVE_CONFIG_H +# include +#endif + + +#include "RemoteControl.h" +#include "ModPlugin.h" +#include "PcDebug.h" +#include "ThreadsafeQueue.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#define MEMLESSPOLY_PIPELINE_DELAY 1 + +typedef std::complex complexf; + +class MemlessPoly : public PipelinedModCodec, public RemoteControllable +{ +public: + MemlessPoly(const std::string& taps_file); + + virtual const char* name() { return "MemlessPoly"; } + + /******* REMOTE CONTROL ********/ + virtual void set_parameter(const std::string& parameter, + const std::string& value); + + virtual const std::string get_parameter( + const std::string& parameter) const; + + +protected: + int internal_process(Buffer* const dataIn, Buffer* dataOut); + void load_filter_taps(const std::string &tapsFile); + + std::string m_taps_file; + + mutable std::mutex m_taps_mutex; + std::vector m_taps; +}; + -- cgit v1.2.3 From 0cded83e492e1fa66ee6be45dae9db53fd17da96 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Sun, 26 Mar 2017 13:32:34 +0200 Subject: Working pipeline --- src/MemlessPoly.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MemlessPoly.cpp b/src/MemlessPoly.cpp index a34dde1..a9970e2 100644 --- a/src/MemlessPoly.cpp +++ b/src/MemlessPoly.cpp @@ -166,7 +166,7 @@ void MemlessPoly::set_parameter(const string& parameter, const string& value) if (parameter == "ntaps") { throw ParameterError("Parameter 'ntaps' is read-only"); } - else if (parameter == "tapsfile") { + else if (parameter == "coeffile") { try { load_filter_taps(value); m_taps_file = value; -- cgit v1.2.3 From fce4eab342a5e4bfad01c5f7e704ece35bd718bc Mon Sep 17 00:00:00 2001 From: andreas128 Date: Sun, 26 Mar 2017 16:02:02 +0200 Subject: Fix remote for polynom coefficients --- src/MemlessPoly.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/MemlessPoly.cpp b/src/MemlessPoly.cpp index a9970e2..b4bd5d0 100644 --- a/src/MemlessPoly.cpp +++ b/src/MemlessPoly.cpp @@ -73,7 +73,7 @@ MemlessPoly::MemlessPoly(const std::string& taps_file) : taps_file.c_str(), this); RC_ADD_PARAMETER(ntaps, "(Read-only) number of filter taps."); - RC_ADD_PARAMETER(tapsfile, "Filename containing filter taps. When written to, the new file gets automatically loaded."); + RC_ADD_PARAMETER(coeffile, "Filename containing filter taps. When written to, the new file gets automatically loaded."); load_filter_taps(m_taps_file); @@ -143,14 +143,14 @@ int MemlessPoly::internal_process(Buffer* const dataIn, Buffer* dataOut) float mag = std::abs(in[i]); //out[i] = in[i]; out[i] = in[i] * ( - default_coefficients[0] + - default_coefficients[1] * mag + - default_coefficients[2] * mag*mag + - default_coefficients[3] * mag*mag*mag + - default_coefficients[4] * mag*mag*mag*mag + - default_coefficients[5] * mag*mag*mag*mag*mag + - default_coefficients[6] * mag*mag*mag*mag*mag*mag + - default_coefficients[7] * mag*mag*mag*mag*mag*mag*mag + m_taps[0] + + m_taps[1] * mag + + m_taps[2] * mag*mag + + m_taps[3] * mag*mag*mag + + m_taps[4] * mag*mag*mag*mag + + m_taps[5] * mag*mag*mag*mag*mag + + m_taps[6] * mag*mag*mag*mag*mag*mag + + m_taps[7] * mag*mag*mag*mag*mag*mag*mag ); } } -- cgit v1.2.3 From 89ac4f53d0a10d1c07980fae5ddeb8818e7b9733 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Sat, 1 Apr 2017 12:13:01 +0100 Subject: Fix file reading --- src/ConfigParser.cpp | 6 ++++++ src/ConfigParser.h | 2 ++ src/DabMod.cpp | 6 ++++-- src/DabModulator.cpp | 26 +++++++++++++++++++------- src/DabModulator.h | 4 +++- src/MemlessPoly.cpp | 1 + src/MemlessPoly.h | 4 +++- 7 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index 393f58a..b7649df 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -168,6 +168,12 @@ static void parse_configfile( pt.get("firfilter.filtertapsfile", "default"); } + // Poly coefficients: + if (pt.get("poly.enabled", 0) == 1) { + mod_settings.polyCoefFilename = + pt.get("poly.polycoeffile", "default"); + } + // Output options std::string output_selected; try { diff --git a/src/ConfigParser.h b/src/ConfigParser.h index 02b798a..fede801 100644 --- a/src/ConfigParser.h +++ b/src/ConfigParser.h @@ -74,6 +74,8 @@ struct mod_settings_t { std::string filterTapsFilename = ""; + std::string polyCoefFilename = ""; + #if defined(HAVE_OUTPUT_UHD) OutputUHDConfig outputuhd_conf; diff --git a/src/DabMod.cpp b/src/DabMod.cpp index 4e4cdab..adc4cf2 100644 --- a/src/DabMod.cpp +++ b/src/DabMod.cpp @@ -322,7 +322,8 @@ int launch_modulator(int argc, char* argv[]) mod_settings.digitalgain, mod_settings.normalise, mod_settings.gainmodeVariance, - mod_settings.filterTapsFilename); + mod_settings.filterTapsFilename, + mod_settings.polyCoefFilename); if (format_converter) { flowgraph.connect(modulator, format_converter); @@ -426,7 +427,8 @@ int launch_modulator(int argc, char* argv[]) mod_settings.digitalgain, mod_settings.normalise, mod_settings.gainmodeVariance, - mod_settings.filterTapsFilename); + mod_settings.filterTapsFilename, + mod_settings.polyCoefFilename); if (format_converter) { flowgraph.connect(modulator, format_converter); diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index bd14396..81257f4 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -62,7 +62,8 @@ DabModulator::DabModulator( unsigned dabMode, GainMode gainMode, float& digGain, float normalise, float gainmodeVariance, - const std::string& filterTapsFilename + const std::string& filterTapsFilename, + const std::string& polyCoefFilename ) : ModInput(), myOutputRate(outputRate), @@ -75,6 +76,7 @@ DabModulator::DabModulator( myEtiSource(etiSource), myFlowgraph(NULL), myFilterTapsFilename(filterTapsFilename), + myPolyCoefFilename(polyCoefFilename), myTiiConfig(tiiConfig) { PDEBUG("DabModulator::DabModulator(%u, %u, %u, %zu) @ %p\n", @@ -218,8 +220,12 @@ int DabModulator::process(Buffer* dataOut) } shared_ptr cifPoly; - cifPoly = make_shared("default"); - rcs.enrol(cifPoly.get()); + if (not myPolyCoefFilename.empty()) { + cifPoly = make_shared(myPolyCoefFilename); + std::cout << myPolyCoefFilename << "\n"; + std::cout << cifPoly->m_taps[0] << " " << cifPoly->m_taps[1] << " "<< cifPoly->m_taps[2] << " "<< cifPoly->m_taps[3] << " "<< cifPoly->m_taps[4] << " "<< cifPoly->m_taps[5] << " "<< cifPoly->m_taps[6] << " "<< cifPoly->m_taps[7] << "\n"; + rcs.enrol(cifPoly.get()); + } auto myOutput = make_shared(dataOut); @@ -371,13 +377,19 @@ int DabModulator::process(Buffer* dataOut) // myFlowgraph->connect(cifGuard, myOutput); // } //} + //if (cifRes) { + // myFlowgraph->connect(cifGuard, cifRes); + // myFlowgraph->connect(cifRes, cifPoly); + // myFlowgraph->connect(cifPoly, myOutput); + //} else { + // myFlowgraph->connect(cifGuard, cifPoly); + // myFlowgraph->connect(cifPoly, myOutput); + //} if (cifRes) { myFlowgraph->connect(cifGuard, cifRes); - myFlowgraph->connect(cifRes, cifPoly); - myFlowgraph->connect(cifPoly, myOutput); + myFlowgraph->connect(cifRes, myOutput); } else { - myFlowgraph->connect(cifGuard, cifPoly); - myFlowgraph->connect(cifPoly, myOutput); + myFlowgraph->connect(cifGuard, myOutput); } } diff --git a/src/DabModulator.h b/src/DabModulator.h index c9bdbe1..0c691dd 100644 --- a/src/DabModulator.h +++ b/src/DabModulator.h @@ -55,7 +55,8 @@ public: unsigned dabMode, GainMode gainMode, float& digGain, float normalise, float gainmodeVariance, - const std::string& filterTapsFilename); + const std::string& filterTapsFilename, + const std::string& polyCoefFilename); DabModulator(const DabModulator& other) = delete; DabModulator& operator=(const DabModulator& other) = delete; virtual ~DabModulator(); @@ -80,6 +81,7 @@ protected: Flowgraph* myFlowgraph; OutputMemory* myOutput; std::string myFilterTapsFilename; + std::string myPolyCoefFilename; tii_config_t& myTiiConfig; size_t myNbSymbols; diff --git a/src/MemlessPoly.cpp b/src/MemlessPoly.cpp index b4bd5d0..1faf338 100644 --- a/src/MemlessPoly.cpp +++ b/src/MemlessPoly.cpp @@ -84,6 +84,7 @@ void MemlessPoly::load_filter_taps(const std::string &tapsFile) { std::vector filter_taps; if (tapsFile == "default") { + std::cout << "MemlessPoly default\n"; std::copy(default_coefficients.begin(), default_coefficients.end(), std::back_inserter(filter_taps)); } diff --git a/src/MemlessPoly.h b/src/MemlessPoly.h index fe372d8..33ae202 100644 --- a/src/MemlessPoly.h +++ b/src/MemlessPoly.h @@ -63,6 +63,9 @@ public: virtual const std::string get_parameter( const std::string& parameter) const; +//TODO to protected + std::vector m_taps; + protected: int internal_process(Buffer* const dataIn, Buffer* dataOut); @@ -71,6 +74,5 @@ protected: std::string m_taps_file; mutable std::mutex m_taps_mutex; - std::vector m_taps; }; -- cgit v1.2.3 From ebf4bac7c87ec4b7a25fb626a53b8c407d3f446b Mon Sep 17 00:00:00 2001 From: andreas128 Date: Thu, 6 Apr 2017 21:57:07 +0100 Subject: Refactor MemlessPoly to have more meaningful names --- src/MemlessPoly.cpp | 126 +++++++++++++++++++++++----------------------------- src/MemlessPoly.h | 10 ++--- 2 files changed, 60 insertions(+), 76 deletions(-) diff --git a/src/MemlessPoly.cpp b/src/MemlessPoly.cpp index 1faf338..7e074eb 100644 --- a/src/MemlessPoly.cpp +++ b/src/MemlessPoly.cpp @@ -4,12 +4,12 @@ Copyright (C) 2017 Matthias P. Braendli, matthias.braendli@mpb.li + Andreas Steger, andreas.steger@digris.ch http://opendigitalradio.org - This block implements a FIR filter. The real filter taps are given - as floats, and the block can take advantage of SSE. - For better performance, filtering is done in another thread, leading + This block implements a memoryless polynom for digital predistortion. + For better performance, multiplying is done in another thread, leading to a pipeline delay of two calls to MemlessPoly::process */ /* @@ -41,117 +41,101 @@ #include #include -#ifdef __SSE__ -# include -#endif - using namespace std; -/* This is the FIR Filter calculated with the doc/fir-filter/generate-filter.py script - * with settings - * gain = 1 - * sampling_freq = 2.048e6 - * cutoff = 810e3 - * transition_width = 250e3 - * - * It is a good default filter for the common scenarios. - */ - //0.8, -0.2, 0.2, 0.25, +// By default the signal is unchanged static const std::array default_coefficients({ - 0.1, 0.0, 0.0, 0.0, + 1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }); -MemlessPoly::MemlessPoly(const std::string& taps_file) : +MemlessPoly::MemlessPoly(const std::string& coefs_file) : PipelinedModCodec(), RemoteControllable("memlesspoly"), - m_taps_file(taps_file) + m_coefs_file(coefs_file) { PDEBUG("MemlessPoly::MemlessPoly(%s) @ %p\n", - taps_file.c_str(), this); + coefs_file.c_str(), this); - RC_ADD_PARAMETER(ntaps, "(Read-only) number of filter taps."); - RC_ADD_PARAMETER(coeffile, "Filename containing filter taps. When written to, the new file gets automatically loaded."); + RC_ADD_PARAMETER(ncoefs, "(Read-only) number of coefficients."); + RC_ADD_PARAMETER(coeffile, "Filename containing coefficients. When written to, the new file gets automatically loaded."); - load_filter_taps(m_taps_file); + load_coefficients(m_coefs_file); start_pipeline_thread(); } -void MemlessPoly::load_filter_taps(const std::string &tapsFile) +void MemlessPoly::load_coefficients(const std::string &coefFile) { - std::vector filter_taps; - if (tapsFile == "default") { - std::cout << "MemlessPoly default\n"; + std::vector coefs; + if (coefFile == "default") { std::copy(default_coefficients.begin(), default_coefficients.end(), - std::back_inserter(filter_taps)); + std::back_inserter(coefs)); } else { - std::ifstream taps_fstream(tapsFile.c_str()); - if(!taps_fstream) { - fprintf(stderr, "MemlessPoly: file %s could not be opened !\n", tapsFile.c_str()); - throw std::runtime_error("MemlessPoly: Could not open file with taps! "); + std::ifstream coef_fstream(coefFile.c_str()); + if(!coef_fstream) { + fprintf(stderr, "MemlessPoly: file %s could not be opened !\n", coefFile.c_str()); + throw std::runtime_error("MemlessPoly: Could not open file with coefs! "); } - int n_taps; - taps_fstream >> n_taps; + int n_coefs; + coef_fstream >> n_coefs; - if (n_taps <= 0) { - fprintf(stderr, "MemlessPoly: warning: taps file has invalid format\n"); - throw std::runtime_error("MemlessPoly: taps file has invalid format."); + if (n_coefs <= 0) { + fprintf(stderr, "MemlessPoly: warning: coefs file has invalid format\n"); + throw std::runtime_error("MemlessPoly: coefs file has invalid format."); } - if (n_taps > 100) { - fprintf(stderr, "MemlessPoly: warning: taps file has more than 100 taps\n"); + if (n_coefs != 8) { + throw std::runtime_error( "MemlessPoly: error: coefs file does not have 8 coefs\n"); } - fprintf(stderr, "MemlessPoly: Reading %d taps...\n", n_taps); + fprintf(stderr, "MemlessPoly: Reading %d coefs...\n", n_coefs); - filter_taps.resize(n_taps); + coefs.resize(n_coefs); int n; - for (n = 0; n < n_taps; n++) { - taps_fstream >> filter_taps[n]; - PDEBUG("MemlessPoly: tap: %f\n", filter_taps[n] ); - if (taps_fstream.eof()) { - fprintf(stderr, "MemlessPoly: file %s should contains %d taps, but EOF reached "\ - "after %d taps !\n", tapsFile.c_str(), n_taps, n); - throw std::runtime_error("MemlessPoly: filtertaps file invalid ! "); + for (n = 0; n < n_coefs; n++) { + coef_fstream >> coefs[n]; + PDEBUG("MemlessPoly: coef: %f\n", coefs[n] ); + if (coef_fstream.eof()) { + fprintf(stderr, "MemlessPoly: file %s should contains %d coefs, but EOF reached "\ + "after %d coefs !\n", coefFile.c_str(), n_coefs, n); + throw std::runtime_error("MemlessPoly: coefs file invalid ! "); } } } { - std::lock_guard lock(m_taps_mutex); + std::lock_guard lock(m_coefs_mutex); - m_taps = filter_taps; + m_coefs = coefs; } } int MemlessPoly::internal_process(Buffer* const dataIn, Buffer* dataOut) { - size_t i; - const float* in = reinterpret_cast(dataIn->getData()); float* out = reinterpret_cast(dataOut->getData()); size_t sizeIn = dataIn->getLength() / sizeof(float); { - std::lock_guard lock(m_taps_mutex); - for (i = 0; i < sizeIn; i += 1) { + std::lock_guard lock(m_coefs_mutex); + for (size_t i = 0; i < sizeIn; i += 1) { float mag = std::abs(in[i]); //out[i] = in[i]; out[i] = in[i] * ( - m_taps[0] + - m_taps[1] * mag + - m_taps[2] * mag*mag + - m_taps[3] * mag*mag*mag + - m_taps[4] * mag*mag*mag*mag + - m_taps[5] * mag*mag*mag*mag*mag + - m_taps[6] * mag*mag*mag*mag*mag*mag + - m_taps[7] * mag*mag*mag*mag*mag*mag*mag + m_coefs[0] + + m_coefs[1] * mag + + m_coefs[2] * mag*mag + + m_coefs[3] * mag*mag*mag + + m_coefs[4] * mag*mag*mag*mag + + m_coefs[5] * mag*mag*mag*mag*mag + + m_coefs[6] * mag*mag*mag*mag*mag*mag + + m_coefs[7] * mag*mag*mag*mag*mag*mag*mag ); } } @@ -164,13 +148,13 @@ void MemlessPoly::set_parameter(const string& parameter, const string& value) stringstream ss(value); ss.exceptions ( stringstream::failbit | stringstream::badbit ); - if (parameter == "ntaps") { - throw ParameterError("Parameter 'ntaps' is read-only"); + if (parameter == "ncoefs") { + throw ParameterError("Parameter 'ncoefs' is read-only"); } else if (parameter == "coeffile") { try { - load_filter_taps(value); - m_taps_file = value; + load_coefficients(value); + m_coefs_file = value; } catch (std::runtime_error &e) { throw ParameterError(e.what()); @@ -187,11 +171,11 @@ void MemlessPoly::set_parameter(const string& parameter, const string& value) const string MemlessPoly::get_parameter(const string& parameter) const { stringstream ss; - if (parameter == "ntaps") { - ss << m_taps.size(); + if (parameter == "ncoefs") { + ss << m_coefs.size(); } - else if (parameter == "tapsfile") { - ss << m_taps_file; + else if (parameter == "coefFile") { + ss << m_coefs_file; } else { ss << "Parameter '" << parameter << diff --git a/src/MemlessPoly.h b/src/MemlessPoly.h index 33ae202..210b4b4 100644 --- a/src/MemlessPoly.h +++ b/src/MemlessPoly.h @@ -52,7 +52,7 @@ typedef std::complex complexf; class MemlessPoly : public PipelinedModCodec, public RemoteControllable { public: - MemlessPoly(const std::string& taps_file); + MemlessPoly(const std::string& coefs_file); virtual const char* name() { return "MemlessPoly"; } @@ -64,15 +64,15 @@ public: const std::string& parameter) const; //TODO to protected - std::vector m_taps; + std::vector m_coefs; protected: int internal_process(Buffer* const dataIn, Buffer* dataOut); - void load_filter_taps(const std::string &tapsFile); + void load_coefficients(const std::string &coefFile); - std::string m_taps_file; + std::string m_coefs_file; - mutable std::mutex m_taps_mutex; + mutable std::mutex m_coefs_mutex; }; -- cgit v1.2.3 From 072c2e1277690fa6bd5016fdb69455fd28ba785e Mon Sep 17 00:00:00 2001 From: andreas128 Date: Thu, 6 Apr 2017 22:01:15 +0100 Subject: Fix print to stdout --- src/DabModulator.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index 81257f4..757dd9a 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -206,7 +206,8 @@ int DabModulator::process(Buffer* dataOut) (1 + myNbSymbols), myNbCarriers, mySpacing); auto cifGain = make_shared( - mySpacing, myGainMode, myDigGain, myNormalise, myGainmodeVariance); + mySpacing, myGainMode, myDigGain, myNormalise, + myGainmodeVariance); rcs.enrol(cifGain.get()); @@ -222,8 +223,12 @@ int DabModulator::process(Buffer* dataOut) shared_ptr cifPoly; if (not myPolyCoefFilename.empty()) { cifPoly = make_shared(myPolyCoefFilename); - std::cout << myPolyCoefFilename << "\n"; - std::cout << cifPoly->m_taps[0] << " " << cifPoly->m_taps[1] << " "<< cifPoly->m_taps[2] << " "<< cifPoly->m_taps[3] << " "<< cifPoly->m_taps[4] << " "<< cifPoly->m_taps[5] << " "<< cifPoly->m_taps[6] << " "<< cifPoly->m_taps[7] << "\n"; + etiLog.level(debug) << myPolyCoefFilename << "\n"; + etiLog.level(debug) << cifPoly->m_coefs[0] << " " << + cifPoly->m_coefs[1] << " "<< cifPoly->m_coefs[2] << " "<< + cifPoly->m_coefs[3] << " "<< cifPoly->m_coefs[4] << " "<< + cifPoly->m_coefs[5] << " "<< cifPoly->m_coefs[6] << " "<< + cifPoly->m_coefs[7] << "\n"; rcs.enrol(cifPoly.get()); } @@ -318,7 +323,8 @@ int DabModulator::process(Buffer* dataOut) auto subchConv = make_shared(subchSizeIn); // Configuring puncturing encoder - auto subchPunc = make_shared(subchannel->framesizeCu()); + auto subchPunc = + make_shared(subchannel->framesizeCu()); for (const auto& rule : subchannel->get_rules()) { PDEBUG(" Adding rule:\n"); -- cgit v1.2.3 From f450abc563b5b0a8f2625d8d1c14f1f04adbd26d Mon Sep 17 00:00:00 2001 From: andreas128 Date: Thu, 6 Apr 2017 22:01:48 +0100 Subject: Add warning for incomplete flowgraph --- src/DabModulator.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index 757dd9a..8e3af8a 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -366,6 +366,7 @@ int DabModulator::process(Buffer* dataOut) myFlowgraph->connect(cifOfdm, cifGain); myFlowgraph->connect(cifGain, cifGuard); +#warning "Flowgraph logic incomplete (skips FIRFilter)!" //if (cifFilter) { // myFlowgraph->connect(cifGuard, cifFilter); // if (cifRes) { @@ -383,19 +384,13 @@ int DabModulator::process(Buffer* dataOut) // myFlowgraph->connect(cifGuard, myOutput); // } //} - //if (cifRes) { - // myFlowgraph->connect(cifGuard, cifRes); - // myFlowgraph->connect(cifRes, cifPoly); - // myFlowgraph->connect(cifPoly, myOutput); - //} else { - // myFlowgraph->connect(cifGuard, cifPoly); - // myFlowgraph->connect(cifPoly, myOutput); - //} if (cifRes) { myFlowgraph->connect(cifGuard, cifRes); - myFlowgraph->connect(cifRes, myOutput); + myFlowgraph->connect(cifRes, cifPoly); + myFlowgraph->connect(cifPoly, myOutput); } else { - myFlowgraph->connect(cifGuard, myOutput); + myFlowgraph->connect(cifGuard, cifPoly); + myFlowgraph->connect(cifPoly, myOutput); } } -- cgit v1.2.3 From ca8ff2b57c4bccfaba4be0d7cee089a3022ea4b6 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Thu, 6 Apr 2017 22:20:10 +0100 Subject: Add MemlessPoly documentation for config file --- doc/example.ini | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/doc/example.ini b/doc/example.ini index f8cec36..ffb20d7 100644 --- a/doc/example.ini +++ b/doc/example.ini @@ -135,6 +135,21 @@ enabled=1 ; If filtertapsfile is not given, the default taps are used. ;filtertapsfile=simple_taps.txt +[poly] +;Predistortion using memoryless polynom +enabled=1 +polycoeffile=polyCoefs +;eg: +;echo "8 +;0.1 +;0 +;0 +;0 +;0 +;0 +;0 +;0" > polyCoefs + [output] ; choose output: possible values: uhd, file, zmq, soapysdr output=uhd -- cgit v1.2.3 From 2b877e304d52c406720050aa55eed97b6f7869be Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 7 May 2017 14:22:21 +0200 Subject: Add WIP for OutputUHDFeedback --- Makefile.am | 4 +- doc/example.ini | 5 + src/ConfigParser.cpp | 2 + src/OutputUHD.cpp | 21 ++-- src/OutputUHD.h | 15 ++- src/OutputUHDFeedback.cpp | 245 ++++++++++++++++++++++++++++++++++++++++++++++ src/OutputUHDFeedback.h | 114 +++++++++++++++++++++ 7 files changed, 383 insertions(+), 23 deletions(-) create mode 100644 src/OutputUHDFeedback.cpp create mode 100644 src/OutputUHDFeedback.h diff --git a/Makefile.am b/Makefile.am index d4365fd..12dfe6e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ # Copyright (C) 2007, 2008, 2009, 2010 Her Majesty the Queen in Right # of Canada (Communications Research Center Canada) # -# Copyright (C) 2016 +# Copyright (C) 2017 # Matthias P. Braendli, matthias.braendli@mpb.li # http://opendigitalradio.org @@ -82,6 +82,8 @@ odr_dabmod_SOURCES = src/DabMod.cpp \ src/TimestampDecoder.cpp \ src/OutputUHD.cpp \ src/OutputUHD.h \ + src/OutputUHDFeedback.cpp \ + src/OutputUHDFeedback.h \ src/OutputSoapy.cpp \ src/OutputSoapy.h \ src/InputMemory.cpp \ diff --git a/doc/example.ini b/doc/example.ini index ffb20d7..fa03d26 100644 --- a/doc/example.ini +++ b/doc/example.ini @@ -255,6 +255,11 @@ behaviour_refclk_lock_lost=ignore ; default value: 0 max_gps_holdover_time=600 +; Enable the TCP server to communicate TX and RX feedback for +; digital predistortion. +; Set to 0 to disable +dpd_port=50055 + ; section defining ZeroMQ output properties [zmqoutput] diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index b7649df..8892642 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -255,6 +255,8 @@ static void parse_configfile( outputuhd_conf.maxGPSHoldoverTime = pt.get("uhdoutput.max_gps_holdover_time", 0); + outputuhd_conf.dpdFeedbackServerPort = pt.get("uhdoutput.dpd_port", 0); + mod_settings.outputuhd_conf = outputuhd_conf; mod_settings.useUHDOutput = 1; } diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index d78c4bf..6dc8878 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -169,8 +169,6 @@ OutputUHD::OutputUHD( RC_ADD_PARAMETER(muting, "Mute the output by stopping the transmitter"); RC_ADD_PARAMETER(staticdelay, "Set static delay (uS) between 0 and 96000"); - // TODO: find out how to use boost::bind to give the logger to the - // uhd_msg_handler uhd::msg::register_handler(uhd_msg_handler); uhd::set_thread_priority_safe(); @@ -286,13 +284,9 @@ OutputUHD::OutputUHD( SetDelayBuffer(myConf.dabMode); - MDEBUG("OutputUHD:UHD ready.\n"); -} + uhdFeedback.setup(myUsrp, myConf.dpdFeedbackServerPort); - -OutputUHD::~OutputUHD() -{ - MDEBUG("OutputUHD::~OutputUHD() @ %p\n", this); + MDEBUG("OutputUHD:UHD ready.\n"); } @@ -426,12 +420,11 @@ int OutputUHD::process(Buffer* dataIn) "OutputUHD: dropping one frame with invalid FCT"; } else { - while (true) { - size_t num_frames = uwd.frames.push_wait_if_full(frame, - FRAMES_MAX_SIZE); - etiLog.log(trace, "UHD,push %zu", num_frames); - break; - } + uhdFeedback.set_tx_frame(frame.buf, frame.ts); + + size_t num_frames = uwd.frames.push_wait_if_full(frame, + FRAMES_MAX_SIZE); + etiLog.log(trace, "UHD,push %zu", num_frames); } } diff --git a/src/OutputUHD.h b/src/OutputUHD.h index d42245f..1246fc5 100644 --- a/src/OutputUHD.h +++ b/src/OutputUHD.h @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2016 + Copyright (C) 2017 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -56,6 +56,7 @@ DESCRIPTION: #include "TimestampDecoder.h" #include "RemoteControl.h" #include "ThreadsafeQueue.h" +#include "OutputUHDFeedback.h" #include #include @@ -210,14 +211,15 @@ struct OutputUHDConfig { // static delay in microseconds int staticDelayUs = 0; -}; + // TCP port on which to serve TX and RX samples for the + // digital pre distortion learning tool + uint16_t dpdFeedbackServerPort = 0; +}; class OutputUHD: public ModOutput, public RemoteControllable { public: - OutputUHD(OutputUHDConfig& config); - ~OutputUHD(); int process(Buffer* dataIn); @@ -235,11 +237,7 @@ class OutputUHD: public ModOutput, public RemoteControllable { virtual const std::string get_parameter( const std::string& parameter) const; - protected: - OutputUHD(const OutputUHD& other) = delete; - OutputUHD& operator=(const OutputUHD& other) = delete; - EtiSource *myEtiSource; OutputUHDConfig& myConf; uhd::usrp::multi_usrp::sptr myUsrp; @@ -248,6 +246,7 @@ class OutputUHD: public ModOutput, public RemoteControllable { bool gps_fix_verified; struct UHDWorkerData uwd; UHDWorker worker; + OutputUHDFeedback uhdFeedback; private: // Resize the internal delay buffer according to the dabMode and diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp new file mode 100644 index 0000000..dfe0f74 --- /dev/null +++ b/src/OutputUHDFeedback.cpp @@ -0,0 +1,245 @@ +/* + Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the + Queen in Right of Canada (Communications Research Center Canada) + + Copyright (C) 2017 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://opendigitalradio.org + +DESCRIPTION: + This presents a TCP socket to an external tool which calculates + a Digital Predistortion model from a short sequence of transmit + samples and corresponding receive samples. +*/ + +/* + This file is part of ODR-DabMod. + + ODR-DabMod is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + ODR-DabMod is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ODR-DabMod. If not, see . + */ + +#include +#include +#include +#include "OutputUHDFeedback.h" +#include "Utils.h" + +using namespace std; +typedef std::complex complexf; + +OutputUHDFeedback::OutputUHDFeedback() +{ + running = false; +} + +void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port) +{ + myUsrp = usrp; + burstRequest.state = BurstRequestState::None; + + if (port) { + m_port = port; + running = true; + + rx_burst_thread = boost::thread(&OutputUHDFeedback::ReceiveBurstThread, this); + burst_tcp_thread = boost::thread(&OutputUHDFeedback::ServeFeedbackThread, this); + } +} + +OutputUHDFeedback::~OutputUHDFeedback() +{ + running = false; + rx_burst_thread.join(); + burst_tcp_thread.join(); +} + +void OutputUHDFeedback::set_tx_frame( + const std::vector &buf, + const struct frame_timestamp& ts) +{ + boost::mutex::scoped_lock lock(burstRequest.mutex); + + if (burstRequest.state == BurstRequestState::SaveTransmitFrame) { + const size_t n = std::min( + burstRequest.frame_length * sizeof(complexf), buf.size()); + + burstRequest.tx_samples.clear(); + burstRequest.tx_samples.resize(n); + copy(buf.begin(), buf.begin() + n, burstRequest.tx_samples.begin()); + + burstRequest.tx_second = ts.timestamp_sec; + burstRequest.tx_pps = ts.timestamp_pps; + + // Prepare the next state + burstRequest.rx_second = ts.timestamp_sec; + burstRequest.rx_pps = ts.timestamp_pps; + burstRequest.state = BurstRequestState::SaveReceiveFrame; + + lock.unlock(); + burstRequest.mutex_notification.notify_one(); + } + else { + lock.unlock(); + } +} + +void OutputUHDFeedback::ReceiveBurstThread() +{ + set_thread_name("uhdreceiveburst"); + + uhd::stream_args_t stream_args("fc32"); //complex floats + auto rxStream = myUsrp->get_rx_stream(stream_args); + + while (running) { + boost::mutex::scoped_lock lock(burstRequest.mutex); + while (burstRequest.state != BurstRequestState::SaveReceiveFrame) { + if (not running) break; + burstRequest.mutex_notification.wait(lock); + } + + if (not running) break; + + uhd::stream_cmd_t cmd( + uhd::stream_cmd_t::stream_mode_t::STREAM_MODE_NUM_SAMPS_AND_DONE); + cmd.num_samps = burstRequest.frame_length; + cmd.stream_now = false; + + double pps = burstRequest.rx_pps / 16384000.0; + cmd.time_spec = uhd::time_spec_t(burstRequest.rx_second, pps); + + rxStream->issue_stream_cmd(cmd); + + uhd::rx_metadata_t md; + burstRequest.rx_samples.resize(burstRequest.frame_length * sizeof(complexf)); + rxStream->recv(&burstRequest.rx_samples[0], burstRequest.frame_length, md); + + burstRequest.rx_second = md.time_spec.get_full_secs(); + burstRequest.rx_pps = md.time_spec.get_frac_secs() * 16384000.0; + + burstRequest.state = BurstRequestState::Acquired; + + lock.unlock(); + burstRequest.mutex_notification.notify_one(); + } +} + +void OutputUHDFeedback::ServeFeedbackThread() +{ + set_thread_name("uhdservefeedback"); + + int server_sock = -1; + try { + if ((server_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { + throw std::runtime_error("Can't create TCP socket"); + } + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_port = htons(m_port); + addr.sin_addr.s_addr = htonl(INADDR_ANY); + + if (bind(server_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + throw std::runtime_error("Can't bind TCP socket"); + } + + if (listen(server_sock, 1) < 0) { + throw std::runtime_error("Can't listen TCP socket"); + } + + while (running) { + struct sockaddr_in client; + socklen_t client_len = sizeof(client); + int client_sock = accept(server_sock, + (struct sockaddr*)&client, &client_len); + + if (client_sock < 0) { + throw runtime_error("Could not establish new connection"); + } + + while (running) { + uint8_t request_version = 0; + int read = recv(client_sock, &request_version, 1, 0); + if (!read) break; // done reading + if (read < 0) { + etiLog.level(info) << + "DPD Feedback Server Client read request verson failed"; + } + + if (request_version != 1) { + etiLog.level(info) << "DPD Feedback Server wrong request version"; + break; + } + + uint32_t num_samples = 0; + read = recv(client_sock, &num_samples, 4, 0); + if (!read) break; // done reading + if (read < 0) { + etiLog.level(info) << + "DPD Feedback Server Client read num samples failed"; + } + + // We are ready to issue the request now + { + boost::mutex::scoped_lock lock(burstRequest.mutex); + burstRequest.frame_length = num_samples; + burstRequest.state = BurstRequestState::SaveTransmitFrame; + + lock.unlock(); + } + + // Wait for the result to be ready + boost::mutex::scoped_lock lock(burstRequest.mutex); + while (burstRequest.state != BurstRequestState::Acquired) { + if (not running) break; + burstRequest.mutex_notification.wait(lock); + } + + burstRequest.state = BurstRequestState::None; + lock.unlock(); + + if (send(client_sock, + &burstRequest.tx_second, + sizeof(burstRequest.tx_second), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_second failed"; + break; + } + + if (send(client_sock, + &burstRequest.tx_pps, + sizeof(burstRequest.tx_pps), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_pps failed"; + break; + } + +#warning "Send buf" + } + + close(client_sock); + } + } + catch (runtime_error &e) { + etiLog.level(error) << "DPD Feedback Server fault: " << e.what(); + } + + running = false; + + if (server_sock != -1) { + close(server_sock); + } +} diff --git a/src/OutputUHDFeedback.h b/src/OutputUHDFeedback.h new file mode 100644 index 0000000..31f7547 --- /dev/null +++ b/src/OutputUHDFeedback.h @@ -0,0 +1,114 @@ +/* + Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Her Majesty the + Queen in Right of Canada (Communications Research Center Canada) + + Copyright (C) 2017 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://opendigitalradio.org + +DESCRIPTION: + This presents a TCP socket to an external tool which calculates + a Digital Predistortion model from a short sequence of transmit + samples and corresponding receive samples. +*/ + +/* + This file is part of ODR-DabMod. + + ODR-DabMod is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + ODR-DabMod is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ODR-DabMod. If not, see . + */ + +#pragma once + +#ifdef HAVE_CONFIG_H +# include +#endif + +#ifdef HAVE_OUTPUT_UHD + +#include +#include +#include +#include +#include +#include + +#include "Log.h" +#include "TimestampDecoder.h" + +enum class BurstRequestState { + None, // To pending request + SaveTransmitFrame, // The TX thread has to save an outgoing frame + SaveReceiveFrame, // The RX thread has to save an incoming frame + Acquired, // Both TX and RX frames are ready +}; + +struct UHDReceiveBurstRequest { + // All fields in this struct are protected + mutable boost::mutex mutex; + boost::condition_variable mutex_notification; + + BurstRequestState state; + + // In the SaveTransmit states, frame_length samples are saved into + // the vectors + size_t frame_length; + + // The timestamp of the first sample of the TX buffers + uint32_t tx_second; + uint32_t tx_pps; // in units of 1/16384000s + + std::vector tx_samples; + + // The timestamp of the first sample of the RX buffers + uint32_t rx_second; + uint32_t rx_pps; + + std::vector rx_samples; +}; + + +class OutputUHDFeedback { + public: + OutputUHDFeedback(); + OutputUHDFeedback(const OutputUHDFeedback& other) = delete; + OutputUHDFeedback& operator=(const OutputUHDFeedback& other) = delete; + ~OutputUHDFeedback(); + + void setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port); + + void set_tx_frame(const std::vector &buf, + const struct frame_timestamp& ts); + + + private: + // Thread that reacts to burstRequests and receives from the USRP + void ReceiveBurstThread(void); + + // Thread that listens for requests over TCP to get TX and RX feedback + void ServeFeedbackThread(void); + + boost::thread rx_burst_thread; + boost::thread burst_tcp_thread; + + UHDReceiveBurstRequest burstRequest; + + bool running = false; + uint16_t m_port = 0; + uhd::usrp::multi_usrp::sptr myUsrp; +}; + + +#endif // HAVE_OUTPUT_UHD -- cgit v1.2.3 From 64e77f042402b5f881fb99b28bd15fe343e51494 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 08:27:45 +0200 Subject: Initialise flowgraph correctly with FIRFilter --- src/DabModulator.cpp | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index 8e3af8a..d425f04 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -360,42 +360,38 @@ int DabModulator::process(Buffer* dataOut) if (useCicEq) { myFlowgraph->connect(cifSig, cifCicEq); myFlowgraph->connect(cifCicEq, cifOfdm); - } else { + } + else { myFlowgraph->connect(cifSig, cifOfdm); } myFlowgraph->connect(cifOfdm, cifGain); myFlowgraph->connect(cifGain, cifGuard); -#warning "Flowgraph logic incomplete (skips FIRFilter)!" - //if (cifFilter) { - // myFlowgraph->connect(cifGuard, cifFilter); - // if (cifRes) { - // myFlowgraph->connect(cifFilter, cifRes); - // myFlowgraph->connect(cifRes, myOutput); - // } else { - // myFlowgraph->connect(cifFilter, myOutput); - // } - //} - //else { //no filtering - // if (cifRes) { - // myFlowgraph->connect(cifGuard, cifRes); - // myFlowgraph->connect(cifRes, myOutput); - // } else { - // myFlowgraph->connect(cifGuard, myOutput); - // } - //} - if (cifRes) { - myFlowgraph->connect(cifGuard, cifRes); - myFlowgraph->connect(cifRes, cifPoly); - myFlowgraph->connect(cifPoly, myOutput); - } else { - myFlowgraph->connect(cifGuard, cifPoly); - myFlowgraph->connect(cifPoly, myOutput); + if (cifFilter) { + myFlowgraph->connect(cifGuard, cifFilter); + if (cifRes) { + myFlowgraph->connect(cifFilter, cifRes); + myFlowgraph->connect(cifRes, cifPoly); + } + else { + myFlowgraph->connect(cifFilter, cifPoly); + } } + else { + if (cifRes) { + myFlowgraph->connect(cifGuard, cifRes); + myFlowgraph->connect(cifRes, cifPoly); + } + else { + myFlowgraph->connect(cifGuard, cifPoly); + } + } + + myFlowgraph->connect(cifPoly, myOutput); } //////////////////////////////////////////////////////////////////// - // Proccessing data + // Processing data //////////////////////////////////////////////////////////////////// return myFlowgraph->run(); } -- cgit v1.2.3 From 4fad4de6ec39b2741f8545ed78aa58ea0a6edc6c Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 08:28:47 +0200 Subject: UHD Feedback: Do not send the beginning of the frame --- src/OutputUHD.cpp | 2 +- src/OutputUHDFeedback.cpp | 48 ++++++++++++++++++++++++++++++++--------------- src/OutputUHDFeedback.h | 10 +++++----- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index 6dc8878..6edf7df 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -284,7 +284,7 @@ OutputUHD::OutputUHD( SetDelayBuffer(myConf.dabMode); - uhdFeedback.setup(myUsrp, myConf.dpdFeedbackServerPort); + uhdFeedback.setup(myUsrp, myConf.dpdFeedbackServerPort, myConf.sampleRate); MDEBUG("OutputUHD:UHD ready.\n"); } diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index dfe0f74..73497a1 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -30,7 +30,14 @@ DESCRIPTION: along with ODR-DabMod. If not, see . */ +#ifdef HAVE_CONFIG_H +# include +#endif + +#ifdef HAVE_OUTPUT_UHD + #include +#include #include #include #include "OutputUHDFeedback.h" @@ -41,17 +48,18 @@ typedef std::complex complexf; OutputUHDFeedback::OutputUHDFeedback() { - running = false; + m_running = false; } -void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port) +void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port, uint32_t sampleRate) { - myUsrp = usrp; + m_usrp = usrp; + m_sampleRate = sampleRate; burstRequest.state = BurstRequestState::None; if (port) { m_port = port; - running = true; + m_running = true; rx_burst_thread = boost::thread(&OutputUHDFeedback::ReceiveBurstThread, this); burst_tcp_thread = boost::thread(&OutputUHDFeedback::ServeFeedbackThread, this); @@ -60,14 +68,14 @@ void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port) OutputUHDFeedback::~OutputUHDFeedback() { - running = false; + m_running = false; rx_burst_thread.join(); burst_tcp_thread.join(); } void OutputUHDFeedback::set_tx_frame( const std::vector &buf, - const struct frame_timestamp& ts) + const struct frame_timestamp &buf_ts) { boost::mutex::scoped_lock lock(burstRequest.mutex); @@ -77,7 +85,15 @@ void OutputUHDFeedback::set_tx_frame( burstRequest.tx_samples.clear(); burstRequest.tx_samples.resize(n); - copy(buf.begin(), buf.begin() + n, burstRequest.tx_samples.begin()); + // A frame will always begin with the NULL symbol, which contains + // no power. Instead of taking n samples at the beginning of the + // frame, we take them at the end and adapt the timestamp accordingly. + + const size_t start_ix = buf.size() - n; + copy(buf.begin() + start_ix, buf.end(), burstRequest.tx_samples.begin()); + + frame_timestamp ts = buf_ts; + ts += (1.0 * start_ix) / (sizeof(complexf) * m_sampleRate); burstRequest.tx_second = ts.timestamp_sec; burstRequest.tx_pps = ts.timestamp_pps; @@ -100,16 +116,16 @@ void OutputUHDFeedback::ReceiveBurstThread() set_thread_name("uhdreceiveburst"); uhd::stream_args_t stream_args("fc32"); //complex floats - auto rxStream = myUsrp->get_rx_stream(stream_args); + auto rxStream = m_usrp->get_rx_stream(stream_args); - while (running) { + while (m_running) { boost::mutex::scoped_lock lock(burstRequest.mutex); while (burstRequest.state != BurstRequestState::SaveReceiveFrame) { - if (not running) break; + if (not m_running) break; burstRequest.mutex_notification.wait(lock); } - if (not running) break; + if (not m_running) break; uhd::stream_cmd_t cmd( uhd::stream_cmd_t::stream_mode_t::STREAM_MODE_NUM_SAMPS_AND_DONE); @@ -158,7 +174,7 @@ void OutputUHDFeedback::ServeFeedbackThread() throw std::runtime_error("Can't listen TCP socket"); } - while (running) { + while (m_running) { struct sockaddr_in client; socklen_t client_len = sizeof(client); int client_sock = accept(server_sock, @@ -168,7 +184,7 @@ void OutputUHDFeedback::ServeFeedbackThread() throw runtime_error("Could not establish new connection"); } - while (running) { + while (m_running) { uint8_t request_version = 0; int read = recv(client_sock, &request_version, 1, 0); if (!read) break; // done reading @@ -202,7 +218,7 @@ void OutputUHDFeedback::ServeFeedbackThread() // Wait for the result to be ready boost::mutex::scoped_lock lock(burstRequest.mutex); while (burstRequest.state != BurstRequestState::Acquired) { - if (not running) break; + if (not m_running) break; burstRequest.mutex_notification.wait(lock); } @@ -237,9 +253,11 @@ void OutputUHDFeedback::ServeFeedbackThread() etiLog.level(error) << "DPD Feedback Server fault: " << e.what(); } - running = false; + m_running = false; if (server_sock != -1) { close(server_sock); } } + +#endif diff --git a/src/OutputUHDFeedback.h b/src/OutputUHDFeedback.h index 31f7547..afc06b0 100644 --- a/src/OutputUHDFeedback.h +++ b/src/OutputUHDFeedback.h @@ -79,7 +79,7 @@ struct UHDReceiveBurstRequest { std::vector rx_samples; }; - +// Serve TX samples and RX feedback samples over a TCP connection class OutputUHDFeedback { public: OutputUHDFeedback(); @@ -87,12 +87,11 @@ class OutputUHDFeedback { OutputUHDFeedback& operator=(const OutputUHDFeedback& other) = delete; ~OutputUHDFeedback(); - void setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port); + void setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port, uint32_t sampleRate); void set_tx_frame(const std::vector &buf, const struct frame_timestamp& ts); - private: // Thread that reacts to burstRequests and receives from the USRP void ReceiveBurstThread(void); @@ -105,9 +104,10 @@ class OutputUHDFeedback { UHDReceiveBurstRequest burstRequest; - bool running = false; + bool m_running = false; uint16_t m_port = 0; - uhd::usrp::multi_usrp::sptr myUsrp; + uint32_t m_sampleRate = 0; + uhd::usrp::multi_usrp::sptr m_usrp; }; -- cgit v1.2.3 From 9bf9469ccdc037d26ccd69ac9e81e64d74fc7008 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 09:47:01 +0200 Subject: Handle absence of cifPoly in flowgraph setup --- src/DabModulator.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/DabModulator.cpp b/src/DabModulator.cpp index d425f04..4e0bc33 100644 --- a/src/DabModulator.cpp +++ b/src/DabModulator.cpp @@ -367,27 +367,33 @@ int DabModulator::process(Buffer* dataOut) myFlowgraph->connect(cifOfdm, cifGain); myFlowgraph->connect(cifGain, cifGuard); + auto cifOut = cifPoly ? + static_pointer_cast(cifPoly) : + static_pointer_cast(myOutput); + if (cifFilter) { myFlowgraph->connect(cifGuard, cifFilter); if (cifRes) { myFlowgraph->connect(cifFilter, cifRes); - myFlowgraph->connect(cifRes, cifPoly); + myFlowgraph->connect(cifRes, cifOut); } else { - myFlowgraph->connect(cifFilter, cifPoly); + myFlowgraph->connect(cifFilter, cifOut); } } else { if (cifRes) { myFlowgraph->connect(cifGuard, cifRes); - myFlowgraph->connect(cifRes, cifPoly); + myFlowgraph->connect(cifRes, cifOut); } else { - myFlowgraph->connect(cifGuard, cifPoly); + myFlowgraph->connect(cifGuard, cifOut); } } - myFlowgraph->connect(cifPoly, myOutput); + if (cifPoly) { + myFlowgraph->connect(cifPoly, myOutput); + } } //////////////////////////////////////////////////////////////////// -- cgit v1.2.3 From 5ac10e37d07cfe723ea4b396f08563889dff5a2b Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 10:20:23 +0200 Subject: Properly terminate dpd server on ctrl-c --- src/OutputUHDFeedback.cpp | 120 ++++++++++++++++++++++++++++++++++++++-------- src/OutputUHDFeedback.h | 12 +++-- 2 files changed, 107 insertions(+), 25 deletions(-) diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 73497a1..8584839 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -40,6 +40,7 @@ DESCRIPTION: #include #include #include +#include #include "OutputUHDFeedback.h" #include "Utils.h" @@ -48,7 +49,7 @@ typedef std::complex complexf; OutputUHDFeedback::OutputUHDFeedback() { - m_running = false; + m_running.store(false); } void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port, uint32_t sampleRate) @@ -59,7 +60,7 @@ void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port, u if (port) { m_port = port; - m_running = true; + m_running.store(true); rx_burst_thread = boost::thread(&OutputUHDFeedback::ReceiveBurstThread, this); burst_tcp_thread = boost::thread(&OutputUHDFeedback::ServeFeedbackThread, this); @@ -68,7 +69,11 @@ void OutputUHDFeedback::setup(uhd::usrp::multi_usrp::sptr usrp, uint16_t port, u OutputUHDFeedback::~OutputUHDFeedback() { - m_running = false; + m_running.store(false); + + rx_burst_thread.interrupt(); + burst_tcp_thread.interrupt(); + rx_burst_thread.join(); burst_tcp_thread.join(); } @@ -79,9 +84,13 @@ void OutputUHDFeedback::set_tx_frame( { boost::mutex::scoped_lock lock(burstRequest.mutex); + assert(buf.size() % sizeof(complexf) == 0); + if (burstRequest.state == BurstRequestState::SaveTransmitFrame) { const size_t n = std::min( - burstRequest.frame_length * sizeof(complexf), buf.size()); + burstRequest.num_samples * sizeof(complexf), buf.size()); + + burstRequest.num_samples = n / sizeof(complexf); burstRequest.tx_samples.clear(); burstRequest.tx_samples.resize(n); @@ -129,7 +138,7 @@ void OutputUHDFeedback::ReceiveBurstThread() uhd::stream_cmd_t cmd( uhd::stream_cmd_t::stream_mode_t::STREAM_MODE_NUM_SAMPS_AND_DONE); - cmd.num_samps = burstRequest.frame_length; + cmd.num_samps = burstRequest.num_samples; cmd.stream_now = false; double pps = burstRequest.rx_pps / 16384000.0; @@ -138,9 +147,10 @@ void OutputUHDFeedback::ReceiveBurstThread() rxStream->issue_stream_cmd(cmd); uhd::rx_metadata_t md; - burstRequest.rx_samples.resize(burstRequest.frame_length * sizeof(complexf)); - rxStream->recv(&burstRequest.rx_samples[0], burstRequest.frame_length, md); + burstRequest.rx_samples.resize(burstRequest.num_samples * sizeof(complexf)); + rxStream->recv(&burstRequest.rx_samples[0], burstRequest.num_samples, md); + // The recv might have happened at another time than requested burstRequest.rx_second = md.time_spec.get_full_secs(); burstRequest.rx_pps = md.time_spec.get_frac_secs() * 16384000.0; @@ -151,13 +161,32 @@ void OutputUHDFeedback::ReceiveBurstThread() } } +static int accept_with_timeout(int server_socket, int timeout_ms, struct sockaddr_in *client) +{ + struct pollfd fds[1]; + fds[0].fd = server_socket; + fds[0].events = POLLIN | POLLOUT; + + int retval = poll(fds, 1, timeout_ms); + + if (retval == -1) { + throw std::runtime_error("TCP Socket accept error: " + to_string(errno)); + } + else if (retval) { + socklen_t client_len = sizeof(struct sockaddr_in); + return accept(server_socket, (struct sockaddr*)&client, &client_len); + } + else { + return -2; + } +} + void OutputUHDFeedback::ServeFeedbackThread() { set_thread_name("uhdservefeedback"); - int server_sock = -1; try { - if ((server_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { + if ((m_server_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { throw std::runtime_error("Can't create TCP socket"); } @@ -166,31 +195,32 @@ void OutputUHDFeedback::ServeFeedbackThread() addr.sin_port = htons(m_port); addr.sin_addr.s_addr = htonl(INADDR_ANY); - if (bind(server_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + if (bind(m_server_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { throw std::runtime_error("Can't bind TCP socket"); } - if (listen(server_sock, 1) < 0) { + if (listen(m_server_sock, 1) < 0) { throw std::runtime_error("Can't listen TCP socket"); } while (m_running) { struct sockaddr_in client; - socklen_t client_len = sizeof(client); - int client_sock = accept(server_sock, - (struct sockaddr*)&client, &client_len); + int client_sock = accept_with_timeout(m_server_sock, 1000, &client); - if (client_sock < 0) { + if (client_sock == -1) { throw runtime_error("Could not establish new connection"); } + else if (client_sock == -2) { + continue; + } while (m_running) { uint8_t request_version = 0; - int read = recv(client_sock, &request_version, 1, 0); + ssize_t read = recv(client_sock, &request_version, 1, 0); if (!read) break; // done reading if (read < 0) { etiLog.level(info) << - "DPD Feedback Server Client read request verson failed"; + "DPD Feedback Server Client read request version failed"; } if (request_version != 1) { @@ -209,7 +239,7 @@ void OutputUHDFeedback::ServeFeedbackThread() // We are ready to issue the request now { boost::mutex::scoped_lock lock(burstRequest.mutex); - burstRequest.frame_length = num_samples; + burstRequest.num_samples = num_samples; burstRequest.state = BurstRequestState::SaveTransmitFrame; lock.unlock(); @@ -225,6 +255,15 @@ void OutputUHDFeedback::ServeFeedbackThread() burstRequest.state = BurstRequestState::None; lock.unlock(); + if (send(client_sock, + &burstRequest.num_samples, + sizeof(burstRequest.num_samples), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send num_samples failed"; + break; + } + if (send(client_sock, &burstRequest.tx_second, sizeof(burstRequest.tx_second), @@ -243,7 +282,45 @@ void OutputUHDFeedback::ServeFeedbackThread() break; } -#warning "Send buf" + const size_t frame_bytes = burstRequest.num_samples * sizeof(complexf); + + assert(burstRequest.tx_samples.size() == frame_bytes); + if (send(client_sock, + &burstRequest.tx_samples[0], + frame_bytes, + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_frame failed"; + break; + } + + if (send(client_sock, + &burstRequest.rx_second, + sizeof(burstRequest.rx_second), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_second failed"; + break; + } + + if (send(client_sock, + &burstRequest.rx_pps, + sizeof(burstRequest.rx_pps), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_pps failed"; + break; + } + + assert(burstRequest.rx_samples.size() == frame_bytes); + if (send(client_sock, + &burstRequest.rx_samples[0], + frame_bytes, + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_frame failed"; + break; + } } close(client_sock); @@ -255,8 +332,9 @@ void OutputUHDFeedback::ServeFeedbackThread() m_running = false; - if (server_sock != -1) { - close(server_sock); + if (m_server_sock != -1) { + close(m_server_sock); + m_server_sock = -1; } } diff --git a/src/OutputUHDFeedback.h b/src/OutputUHDFeedback.h index afc06b0..32668b6 100644 --- a/src/OutputUHDFeedback.h +++ b/src/OutputUHDFeedback.h @@ -44,6 +44,7 @@ DESCRIPTION: #include #include #include +#include #include "Log.h" #include "TimestampDecoder.h" @@ -62,21 +63,23 @@ struct UHDReceiveBurstRequest { BurstRequestState state; - // In the SaveTransmit states, frame_length samples are saved into + // In the SaveTransmit states, num_samples complexf samples are saved into // the vectors - size_t frame_length; + size_t num_samples; // The timestamp of the first sample of the TX buffers uint32_t tx_second; uint32_t tx_pps; // in units of 1/16384000s + // Samples contain complexf, but since our internal representation is uint8_t + // we keep it like that std::vector tx_samples; // The timestamp of the first sample of the RX buffers uint32_t rx_second; uint32_t rx_pps; - std::vector rx_samples; + std::vector rx_samples; // Also, actually complexf }; // Serve TX samples and RX feedback samples over a TCP connection @@ -104,7 +107,8 @@ class OutputUHDFeedback { UHDReceiveBurstRequest burstRequest; - bool m_running = false; + std::atomic_bool m_running; + int m_server_sock = -1; uint16_t m_port = 0; uint32_t m_sampleRate = 0; uhd::usrp::multi_usrp::sptr m_usrp; -- cgit v1.2.3 From 201d2cd2e0431a5ea79fb69561c27555f3a03dc1 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 10:44:33 +0200 Subject: Add dpd example script to fetch samples --- dpd/README.md | 10 +++ dpd/show_spectrum.py | 99 ++++++++++++++++++++++ src/OutputUHDFeedback.cpp | 208 +++++++++++++++++++++++----------------------- 3 files changed, 214 insertions(+), 103 deletions(-) create mode 100644 dpd/README.md create mode 100755 dpd/show_spectrum.py diff --git a/dpd/README.md b/dpd/README.md new file mode 100644 index 0000000..828a483 --- /dev/null +++ b/dpd/README.md @@ -0,0 +1,10 @@ +Digital Predistortion for ODR-DabMod +==================================== + +This folder contains work in progress for digital predistortion. It requires: + +- USRP B200. +- Power amplifier. +- A feedback connection from the power amplifier output, at an appropriate power level for the B200. + Usually this is done with a directional coupler. +- ODR-DabMod with enabled dpd_port, and with a samplerate of 8192000 samples per second. diff --git a/dpd/show_spectrum.py b/dpd/show_spectrum.py new file mode 100755 index 0000000..6c489e0 --- /dev/null +++ b/dpd/show_spectrum.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# This is an example tool that shows how to connect to ODR-DabMod's dpd TCP server +# and get samples from there. +# +# Since the TX and RX samples are not perfectly aligned, the tool has to align them properly, +# which is done in two steps: First on sample-level using a correlation, then with subsample +# accuracy using a FFT approach. +# +# It requires SciPy and matplotlib. +# +# Copyright (C) 2017 Matthias P. Braendli +# http://www.opendigitalradio.org +# Licence: The MIT License, see notice at the end of this file + +import sys +import socket +import struct +import numpy as np +import matplotlib as mp +import scipy.signal + +SIZEOF_SAMPLE = 8 # complex floats + +if len(sys.argv) != 3: + print("Usage: show_spectrum.py ") + sys.exit(1) + +port = int(sys.argv[1]) +num_samps_to_request = int(sys.argv[2]) + + +def get_samples(port, num_samps_to_request): + """Connect to ODR-DabMod, retrieve TX and RX samples, load + into numpy arrays, and return a tuple + (tx_timestamp, tx_samples, rx_timestamp, rx_samples) + where the timestamps are doubles, and the samples are numpy + arrays of complex floats, both having the same size + """ + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect(('localhost', port)) + + print("Send version"); + s.sendall(b"\x01") + + print("Send request for {} samples".format(num_samps_to_request)) + s.sendall(struct.pack("=I", num_samps_to_request)) + + print("Wait for TX metadata") + num_samps, tx_second, tx_pps = struct.unpack("=III", s.recv(12)) + tx_ts = tx_second + tx_pps / 16384000.0 + + print("Receiving {} TX samples".format(num_samps)) + txframe_bytes = s.recv(num_samps * SIZEOF_SAMPLE) + txframe = np.fromstring(txframe_bytes, dtype=np.complex64) + + print("Wait for RX metadata") + rx_second, rx_pps = struct.unpack("=II", s.recv(8)) + rx_ts = rx_second + rx_pps / 16384000.0 + + print("Receiving {} RX samples".format(num_samps)) + rxframe_bytes = s.recv(num_samps * SIZEOF_SAMPLE) + rxframe = np.fromstring(rxframe_bytes, dtype=np.complex64) + + print("Disconnecting") + s.close() + + return (tx_ts, txframe, rx_ts, rxframe) + + +tx_ts, txframe, rx_ts, rxframe = get_samples(port, num_samps_to_request) + +print("Received {} & {} frames at {} and {}".format( + len(txframe), len(rxframe), tx_ts, rx_ts)) + + +# The MIT License (MIT) +# +# Copyright (c) 2017 Matthias P. Braendli +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 8584839..09b73ba 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -38,8 +38,10 @@ DESCRIPTION: #include #include +#include #include #include +#include #include #include "OutputUHDFeedback.h" #include "Utils.h" @@ -214,113 +216,113 @@ void OutputUHDFeedback::ServeFeedbackThread() continue; } - while (m_running) { - uint8_t request_version = 0; - ssize_t read = recv(client_sock, &request_version, 1, 0); - if (!read) break; // done reading - if (read < 0) { - etiLog.level(info) << - "DPD Feedback Server Client read request version failed"; - } - - if (request_version != 1) { - etiLog.level(info) << "DPD Feedback Server wrong request version"; - break; - } - - uint32_t num_samples = 0; - read = recv(client_sock, &num_samples, 4, 0); - if (!read) break; // done reading - if (read < 0) { - etiLog.level(info) << - "DPD Feedback Server Client read num samples failed"; - } - - // We are ready to issue the request now - { - boost::mutex::scoped_lock lock(burstRequest.mutex); - burstRequest.num_samples = num_samples; - burstRequest.state = BurstRequestState::SaveTransmitFrame; - - lock.unlock(); - } - - // Wait for the result to be ready + uint8_t request_version = 0; + ssize_t read = recv(client_sock, &request_version, 1, 0); + if (!read) break; // done reading + if (read < 0) { + etiLog.level(info) << + "DPD Feedback Server Client read request version failed: " << strerror(errno); + break; + } + + if (request_version != 1) { + etiLog.level(info) << "DPD Feedback Server wrong request version"; + break; + } + + uint32_t num_samples = 0; + read = recv(client_sock, &num_samples, 4, 0); + if (!read) break; // done reading + if (read < 0) { + etiLog.level(info) << + "DPD Feedback Server Client read num samples failed"; + break; + } + + // We are ready to issue the request now + { boost::mutex::scoped_lock lock(burstRequest.mutex); - while (burstRequest.state != BurstRequestState::Acquired) { - if (not m_running) break; - burstRequest.mutex_notification.wait(lock); - } + burstRequest.num_samples = num_samples; + burstRequest.state = BurstRequestState::SaveTransmitFrame; - burstRequest.state = BurstRequestState::None; lock.unlock(); + } + + // Wait for the result to be ready + boost::mutex::scoped_lock lock(burstRequest.mutex); + while (burstRequest.state != BurstRequestState::Acquired) { + if (not m_running) break; + burstRequest.mutex_notification.wait(lock); + } + + burstRequest.state = BurstRequestState::None; + lock.unlock(); + + if (send(client_sock, + &burstRequest.num_samples, + sizeof(burstRequest.num_samples), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send num_samples failed"; + break; + } + + if (send(client_sock, + &burstRequest.tx_second, + sizeof(burstRequest.tx_second), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_second failed"; + break; + } + + if (send(client_sock, + &burstRequest.tx_pps, + sizeof(burstRequest.tx_pps), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_pps failed"; + break; + } + + const size_t frame_bytes = burstRequest.num_samples * sizeof(complexf); + + assert(burstRequest.tx_samples.size() == frame_bytes); + if (send(client_sock, + &burstRequest.tx_samples[0], + frame_bytes, + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send tx_frame failed"; + break; + } + + if (send(client_sock, + &burstRequest.rx_second, + sizeof(burstRequest.rx_second), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_second failed"; + break; + } + + if (send(client_sock, + &burstRequest.rx_pps, + sizeof(burstRequest.rx_pps), + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_pps failed"; + break; + } - if (send(client_sock, - &burstRequest.num_samples, - sizeof(burstRequest.num_samples), - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send num_samples failed"; - break; - } - - if (send(client_sock, - &burstRequest.tx_second, - sizeof(burstRequest.tx_second), - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send tx_second failed"; - break; - } - - if (send(client_sock, - &burstRequest.tx_pps, - sizeof(burstRequest.tx_pps), - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send tx_pps failed"; - break; - } - - const size_t frame_bytes = burstRequest.num_samples * sizeof(complexf); - - assert(burstRequest.tx_samples.size() == frame_bytes); - if (send(client_sock, - &burstRequest.tx_samples[0], - frame_bytes, - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send tx_frame failed"; - break; - } - - if (send(client_sock, - &burstRequest.rx_second, - sizeof(burstRequest.rx_second), - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send rx_second failed"; - break; - } - - if (send(client_sock, - &burstRequest.rx_pps, - sizeof(burstRequest.rx_pps), - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send rx_pps failed"; - break; - } - - assert(burstRequest.rx_samples.size() == frame_bytes); - if (send(client_sock, - &burstRequest.rx_samples[0], - frame_bytes, - 0) < 0) { - etiLog.level(info) << - "DPD Feedback Server Client send rx_frame failed"; - break; - } + assert(burstRequest.rx_samples.size() == frame_bytes); + if (send(client_sock, + &burstRequest.rx_samples[0], + frame_bytes, + 0) < 0) { + etiLog.level(info) << + "DPD Feedback Server Client send rx_frame failed"; + break; } close(client_sock); -- cgit v1.2.3 From a656cee6c9c230bb921c6bb6be0f0180460a96b4 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 11:23:15 +0200 Subject: DPD: handle incomplete frames --- dpd/show_spectrum.py | 31 +++++++++++++++++++++++-------- src/OutputUHD.cpp | 2 +- src/OutputUHDFeedback.cpp | 25 ++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/dpd/show_spectrum.py b/dpd/show_spectrum.py index 6c489e0..c1d5fe5 100755 --- a/dpd/show_spectrum.py +++ b/dpd/show_spectrum.py @@ -30,6 +30,15 @@ if len(sys.argv) != 3: port = int(sys.argv[1]) num_samps_to_request = int(sys.argv[2]) +def recv_exact(sock, num_bytes): + bufs = [] + while num_bytes > 0: + b = sock.recv(num_bytes) + if len(b) == 0: + break + num_bytes -= len(b) + bufs.append(b) + return b''.join(bufs) def get_samples(port, num_samps_to_request): """Connect to ODR-DabMod, retrieve TX and RX samples, load @@ -49,20 +58,26 @@ def get_samples(port, num_samps_to_request): s.sendall(struct.pack("=I", num_samps_to_request)) print("Wait for TX metadata") - num_samps, tx_second, tx_pps = struct.unpack("=III", s.recv(12)) + num_samps, tx_second, tx_pps = struct.unpack("=III", recv_exact(s, 12)) tx_ts = tx_second + tx_pps / 16384000.0 - print("Receiving {} TX samples".format(num_samps)) - txframe_bytes = s.recv(num_samps * SIZEOF_SAMPLE) - txframe = np.fromstring(txframe_bytes, dtype=np.complex64) + if num_samps > 0: + print("Receiving {} TX samples".format(num_samps)) + txframe_bytes = recv_exact(s, num_samps * SIZEOF_SAMPLE) + txframe = np.fromstring(txframe_bytes, dtype=np.complex64) + else: + txframe = np.array([], dtype=np.complex64) print("Wait for RX metadata") - rx_second, rx_pps = struct.unpack("=II", s.recv(8)) + rx_second, rx_pps = struct.unpack("=II", recv_exact(s, 8)) rx_ts = rx_second + rx_pps / 16384000.0 - print("Receiving {} RX samples".format(num_samps)) - rxframe_bytes = s.recv(num_samps * SIZEOF_SAMPLE) - rxframe = np.fromstring(rxframe_bytes, dtype=np.complex64) + if num_samps > 0: + print("Receiving {} RX samples".format(num_samps)) + rxframe_bytes = recv_exact(s, num_samps * SIZEOF_SAMPLE) + rxframe = np.fromstring(rxframe_bytes, dtype=np.complex64) + else: + txframe = np.array([], dtype=np.complex64) print("Disconnecting") s.close() diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index 6edf7df..5e9e17c 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -50,7 +50,7 @@ using namespace std; // Maximum number of frames that can wait in uwd.frames -static const size_t FRAMES_MAX_SIZE = 2; +static const size_t FRAMES_MAX_SIZE = 8; typedef std::complex complexf; diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 09b73ba..3ef5648 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -138,6 +138,8 @@ void OutputUHDFeedback::ReceiveBurstThread() if (not m_running) break; + etiLog.level(debug) << "Prepare RX stream command for " << burstRequest.num_samples; + uhd::stream_cmd_t cmd( uhd::stream_cmd_t::stream_mode_t::STREAM_MODE_NUM_SAMPS_AND_DONE); cmd.num_samps = burstRequest.num_samples; @@ -146,16 +148,28 @@ void OutputUHDFeedback::ReceiveBurstThread() double pps = burstRequest.rx_pps / 16384000.0; cmd.time_spec = uhd::time_spec_t(burstRequest.rx_second, pps); + const double usrp_time = m_usrp->get_time_now().get_real_secs(); + const double cmd_time = cmd.time_spec.get_real_secs(); + + etiLog.level(debug) << + "RX stream command ts=" << std::fixed << cmd_time << " Delta=" << cmd_time - usrp_time; + rxStream->issue_stream_cmd(cmd); uhd::rx_metadata_t md; burstRequest.rx_samples.resize(burstRequest.num_samples * sizeof(complexf)); - rxStream->recv(&burstRequest.rx_samples[0], burstRequest.num_samples, md); + size_t samples_read = rxStream->recv(&burstRequest.rx_samples[0], burstRequest.num_samples, md); + assert(samples_read <= burstRequest.num_samples); + burstRequest.rx_samples.resize(samples_read * sizeof(complexf)); // The recv might have happened at another time than requested burstRequest.rx_second = md.time_spec.get_full_secs(); burstRequest.rx_pps = md.time_spec.get_frac_secs() * 16384000.0; + etiLog.level(debug) << "Read " << samples_read << " RX feedback samples " + << "at time " << std::fixed << burstRequest.tx_second << "." << + burstRequest.tx_pps / 16384000.0; + burstRequest.state = BurstRequestState::Acquired; lock.unlock(); @@ -258,6 +272,11 @@ void OutputUHDFeedback::ServeFeedbackThread() burstRequest.state = BurstRequestState::None; lock.unlock(); + burstRequest.num_samples = std::min(burstRequest.num_samples, + std::min( + burstRequest.tx_samples.size() / sizeof(complexf), + burstRequest.rx_samples.size() / sizeof(complexf))); + if (send(client_sock, &burstRequest.num_samples, sizeof(burstRequest.num_samples), @@ -287,7 +306,7 @@ void OutputUHDFeedback::ServeFeedbackThread() const size_t frame_bytes = burstRequest.num_samples * sizeof(complexf); - assert(burstRequest.tx_samples.size() == frame_bytes); + assert(burstRequest.tx_samples.size() >= frame_bytes); if (send(client_sock, &burstRequest.tx_samples[0], frame_bytes, @@ -315,7 +334,7 @@ void OutputUHDFeedback::ServeFeedbackThread() break; } - assert(burstRequest.rx_samples.size() == frame_bytes); + assert(burstRequest.rx_samples.size() >= frame_bytes); if (send(client_sock, &burstRequest.rx_samples[0], frame_bytes, -- cgit v1.2.3 From eefe0ff989243b5af65fc6af0448fa4578fc713e Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 11:25:39 +0200 Subject: Update DPD readme and add example --- dpd/README.md | 8 ++++++++ dpd/dpd.ini | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 dpd/dpd.ini diff --git a/dpd/README.md b/dpd/README.md index 828a483..96c4fb0 100644 --- a/dpd/README.md +++ b/dpd/README.md @@ -8,3 +8,11 @@ This folder contains work in progress for digital predistortion. It requires: - A feedback connection from the power amplifier output, at an appropriate power level for the B200. Usually this is done with a directional coupler. - ODR-DabMod with enabled dpd_port, and with a samplerate of 8192000 samples per second. +- Synchronous=1 so that the USRP has the timestamping set properly. + +See dpd/dpd.ini for an example. + +TODO +---- + +Fix timestamps and test if frame data is valid. diff --git a/dpd/dpd.ini b/dpd/dpd.ini new file mode 100644 index 0000000..906827b --- /dev/null +++ b/dpd/dpd.ini @@ -0,0 +1,41 @@ +[remotecontrol] +telnet=1 +telnetport=2121 + +[log] +syslog=0 +filelog=0 +filename=/dev/stderr + +[input] +transport=tcp +source=localhost:9200 + +[modulator] +digital_gain=0.9 +rate=8192000 + +[firfilter] +enabled=0 + +[output] +output=uhd + +[uhdoutput] +device= +master_clock_rate=32768000 +type=b200 +txgain=50 +channel=13C +refclk_source=internal +pps_source=none +behaviour_refclk_lock_lost=ignore +max_gps_holdover_time=600 +dpd_port=50055 + +[delaymanagement] +; Use synchronous=1 so that the USRP time is set. This works +; even in the absence of a reference clk and PPS +synchronous=1 +mutenotimestamps=1 +offset=4.0 -- cgit v1.2.3 From 2759e2e2a86e97eeee5934d5d9a3b4c911a47229 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 14:48:39 +0200 Subject: DPD: Fix RX feedback sampling --- src/OutputUHDFeedback.cpp | 69 ++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 3ef5648..9e3aab2 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -138,8 +138,6 @@ void OutputUHDFeedback::ReceiveBurstThread() if (not m_running) break; - etiLog.level(debug) << "Prepare RX stream command for " << burstRequest.num_samples; - uhd::stream_cmd_t cmd( uhd::stream_cmd_t::stream_mode_t::STREAM_MODE_NUM_SAMPS_AND_DONE); cmd.num_samps = burstRequest.num_samples; @@ -148,6 +146,10 @@ void OutputUHDFeedback::ReceiveBurstThread() double pps = burstRequest.rx_pps / 16384000.0; cmd.time_spec = uhd::time_spec_t(burstRequest.rx_second, pps); + // We need to free the mutex while we recv(), because otherwise we block the + // TX thread + lock.unlock(); + const double usrp_time = m_usrp->get_time_now().get_real_secs(); const double cmd_time = cmd.time_spec.get_real_secs(); @@ -157,9 +159,14 @@ void OutputUHDFeedback::ReceiveBurstThread() rxStream->issue_stream_cmd(cmd); uhd::rx_metadata_t md; - burstRequest.rx_samples.resize(burstRequest.num_samples * sizeof(complexf)); - size_t samples_read = rxStream->recv(&burstRequest.rx_samples[0], burstRequest.num_samples, md); - assert(samples_read <= burstRequest.num_samples); + + std::vector buf(cmd.num_samps * sizeof(complexf)); + + const double timeout = 60; + size_t samples_read = rxStream->recv(&buf[0], cmd.num_samps, md, timeout); + + lock.lock(); + burstRequest.rx_samples = std::move(buf); burstRequest.rx_samples.resize(samples_read * sizeof(complexf)); // The recv might have happened at another time than requested @@ -197,6 +204,22 @@ static int accept_with_timeout(int server_socket, int timeout_ms, struct sockadd } } +static ssize_t sendall(int socket, const void *buffer, size_t buflen) +{ + uint8_t *buf = (uint8_t*)buffer; + while (buflen > 0) { + ssize_t sent = send(socket, buf, buflen, 0); + if (sent < 0) { + return -1; + } + else { + buf += sent; + buflen -= sent; + } + } + return buflen; +} + void OutputUHDFeedback::ServeFeedbackThread() { set_thread_name("uhdservefeedback"); @@ -277,28 +300,24 @@ void OutputUHDFeedback::ServeFeedbackThread() burstRequest.tx_samples.size() / sizeof(complexf), burstRequest.rx_samples.size() / sizeof(complexf))); - if (send(client_sock, - &burstRequest.num_samples, - sizeof(burstRequest.num_samples), - 0) < 0) { + uint32_t num_samples_32 = burstRequest.num_samples; + if (sendall(client_sock, &num_samples_32, sizeof(num_samples_32)) < 0) { etiLog.level(info) << "DPD Feedback Server Client send num_samples failed"; break; } - if (send(client_sock, + if (sendall(client_sock, &burstRequest.tx_second, - sizeof(burstRequest.tx_second), - 0) < 0) { + sizeof(burstRequest.tx_second)) < 0) { etiLog.level(info) << "DPD Feedback Server Client send tx_second failed"; break; } - if (send(client_sock, + if (sendall(client_sock, &burstRequest.tx_pps, - sizeof(burstRequest.tx_pps), - 0) < 0) { + sizeof(burstRequest.tx_pps)) < 0) { etiLog.level(info) << "DPD Feedback Server Client send tx_pps failed"; break; @@ -307,38 +326,34 @@ void OutputUHDFeedback::ServeFeedbackThread() const size_t frame_bytes = burstRequest.num_samples * sizeof(complexf); assert(burstRequest.tx_samples.size() >= frame_bytes); - if (send(client_sock, + if (sendall(client_sock, &burstRequest.tx_samples[0], - frame_bytes, - 0) < 0) { + frame_bytes) < 0) { etiLog.level(info) << "DPD Feedback Server Client send tx_frame failed"; break; } - if (send(client_sock, + if (sendall(client_sock, &burstRequest.rx_second, - sizeof(burstRequest.rx_second), - 0) < 0) { + sizeof(burstRequest.rx_second)) < 0) { etiLog.level(info) << "DPD Feedback Server Client send rx_second failed"; break; } - if (send(client_sock, + if (sendall(client_sock, &burstRequest.rx_pps, - sizeof(burstRequest.rx_pps), - 0) < 0) { + sizeof(burstRequest.rx_pps)) < 0) { etiLog.level(info) << "DPD Feedback Server Client send rx_pps failed"; break; } assert(burstRequest.rx_samples.size() >= frame_bytes); - if (send(client_sock, + if (sendall(client_sock, &burstRequest.rx_samples[0], - frame_bytes, - 0) < 0) { + frame_bytes) < 0) { etiLog.level(info) << "DPD Feedback Server Client send rx_frame failed"; break; -- cgit v1.2.3 From 403ce1709cd8204769f43a2e6cc68c0286d0fb25 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 14:49:32 +0200 Subject: Fix typo in show_spectrum.py --- dpd/show_spectrum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpd/show_spectrum.py b/dpd/show_spectrum.py index c1d5fe5..477cd99 100755 --- a/dpd/show_spectrum.py +++ b/dpd/show_spectrum.py @@ -77,7 +77,7 @@ def get_samples(port, num_samps_to_request): rxframe_bytes = recv_exact(s, num_samps * SIZEOF_SAMPLE) rxframe = np.fromstring(rxframe_bytes, dtype=np.complex64) else: - txframe = np.array([], dtype=np.complex64) + rxframe = np.array([], dtype=np.complex64) print("Disconnecting") s.close() -- cgit v1.2.3 From f69b4a1da493c73192aa0c86749bd6bcf396422d Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 15:01:08 +0200 Subject: Actually plot a spectrum in show_spectrum.py --- dpd/show_spectrum.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/dpd/show_spectrum.py b/dpd/show_spectrum.py index 477cd99..fcc682f 100755 --- a/dpd/show_spectrum.py +++ b/dpd/show_spectrum.py @@ -18,7 +18,7 @@ import sys import socket import struct import numpy as np -import matplotlib as mp +import matplotlib.pyplot as pp import scipy.signal SIZEOF_SAMPLE = 8 # complex floats @@ -90,6 +90,27 @@ tx_ts, txframe, rx_ts, rxframe = get_samples(port, num_samps_to_request) print("Received {} & {} frames at {} and {}".format( len(txframe), len(rxframe), tx_ts, rx_ts)) +print("Calculate TX and RX spectrum assuming 8192000 samples per second") +fft_size = 4096 +tx_spectrum = np.fft.fftshift(np.fft.fft(txframe, fft_size)) +tx_power = 20*np.log10(np.abs(tx_spectrum)) + +rx_spectrum = np.fft.fftshift(np.fft.fft(rxframe, fft_size)) +rx_power = 20*np.log10(np.abs(rx_spectrum)) + +sampling_rate = 8192000 +freqs = np.fft.fftshift(np.fft.fftfreq(fft_size, d=1./sampling_rate)) + +fig = pp.figure() + +fig.suptitle("TX and RX spectrum") +ax1 = fig.add_subplot(211) +ax1.set_title("TX") +ax1.plot(freqs, tx_power) +ax2 = fig.add_subplot(212) +ax2.set_title("RX") +ax2.plot(freqs, rx_power) +pp.show() # The MIT License (MIT) # -- cgit v1.2.3 From d34574c9688791a83c0b18f501546228bcb40ccb Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 15:20:57 +0200 Subject: Fix zmq_remote.py for python3 --- doc/zmq-ctrl/zmq_remote.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/zmq-ctrl/zmq_remote.py b/doc/zmq-ctrl/zmq_remote.py index dffe53e..3a54b20 100755 --- a/doc/zmq-ctrl/zmq_remote.py +++ b/doc/zmq-ctrl/zmq_remote.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python import sys import zmq @@ -18,7 +18,7 @@ message_parts = sys.argv[2:] # first do a ping test print("ping") -sock.send("ping") +sock.send(b"ping") data = sock.recv_multipart() print("Received: {}".format(len(data))) for i,part in enumerate(data): @@ -32,7 +32,7 @@ for i, part in enumerate(message_parts): print("Send {}({}): '{}'".format(i, f, part)) - sock.send(part, flags=f) + sock.send(part.encode(), flags=f) data = sock.recv_multipart() -- cgit v1.2.3 From 1deec1418cd49e27bc2f7ddd6cd22ac6607642b3 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 15:21:22 +0200 Subject: DPD: Set RX gain and frequency, add gain to RX and config --- dpd/dpd.ini | 3 ++ src/ConfigParser.cpp | 1 + src/OutputUHD.cpp | 70 +++++++++++++++++++++++++++++++---------------- src/OutputUHD.h | 1 + src/OutputUHDFeedback.cpp | 10 +++---- 5 files changed, 56 insertions(+), 29 deletions(-) diff --git a/dpd/dpd.ini b/dpd/dpd.ini index 906827b..625df73 100644 --- a/dpd/dpd.ini +++ b/dpd/dpd.ini @@ -1,6 +1,8 @@ [remotecontrol] telnet=1 telnetport=2121 +zmqctrl=1 +zmqctrlendpoint=tcp://127.0.0.1:9400 [log] syslog=0 @@ -32,6 +34,7 @@ pps_source=none behaviour_refclk_lock_lost=ignore max_gps_holdover_time=600 dpd_port=50055 +rxgain=10 [delaymanagement] ; Use synchronous=1 so that the USRP time is set. This works diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index 8892642..459811f 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -218,6 +218,7 @@ static void parse_configfile( } outputuhd_conf.txgain = pt.get("uhdoutput.txgain", 0.0); + outputuhd_conf.rxgain = pt.get("uhdoutput.rxgain", 0.0); outputuhd_conf.frequency = pt.get("uhdoutput.frequency", 0); std::string chan = pt.get("uhdoutput.channel", ""); outputuhd_conf.dabMode = mod_settings.dabMode; diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index 5e9e17c..f764fb8 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -80,6 +80,36 @@ void uhd_msg_handler(uhd::msg::type_t type, const std::string &msg) } } +static void tune_usrp_to( + uhd::usrp::multi_usrp::sptr usrp, + double lo_offset, + double frequency) +{ + if (lo_offset != 0.0) { + etiLog.level(info) << std::fixed << std::setprecision(3) << + "OutputUHD:Setting freq to " << frequency << + " with LO offset " << lo_offset << "..."; + + const auto tr = uhd::tune_request_t(frequency, lo_offset); + uhd::tune_result_t result = usrp->set_tx_freq(tr); + + etiLog.level(debug) << "OutputUHD:" << + std::fixed << std::setprecision(0) << + " Target RF: " << result.target_rf_freq << + " Actual RF: " << result.actual_rf_freq << + " Target DSP: " << result.target_dsp_freq << + " Actual DSP: " << result.actual_dsp_freq; + } + else { + //set the centre frequency + etiLog.level(info) << std::fixed << std::setprecision(3) << + "OutputUHD:Setting freq to " << frequency << "..."; + usrp->set_tx_freq(frequency); + } + + usrp->set_rx_freq(frequency); +} + // Check function for GPS TIMELOCK sensor from the ODR LEA-M8F board GPSDO bool check_gps_timelock(uhd::usrp::multi_usrp::sptr usrp) { @@ -165,6 +195,7 @@ OutputUHD::OutputUHD( /* register the parameters that can be remote controlled */ RC_ADD_PARAMETER(txgain, "UHD analog daughterboard TX gain"); + RC_ADD_PARAMETER(rxgain, "UHD analog daughterboard RX gain for DPD feedback"); RC_ADD_PARAMETER(freq, "UHD transmission frequency"); RC_ADD_PARAMETER(muting, "Mute the output by stopping the transmitter"); RC_ADD_PARAMETER(staticdelay, "Set static delay (uS) between 0 and 96000"); @@ -223,31 +254,14 @@ OutputUHD::OutputUHD( throw std::runtime_error("Cannot set USRP sample rate. Aborted."); } - if (myConf.lo_offset != 0.0) { - etiLog.level(info) << std::fixed << std::setprecision(3) << - "OutputUHD:Setting freq to " << myConf.frequency << - " with LO offset " << myConf.lo_offset << "..."; - - const auto tr = uhd::tune_request_t(myConf.frequency, myConf.lo_offset); - uhd::tune_result_t result = myUsrp->set_tx_freq(tr); - - etiLog.level(debug) << "OutputUHD:" << - std::fixed << std::setprecision(0) << - " Target RF: " << result.target_rf_freq << - " Actual RF: " << result.actual_rf_freq << - " Target DSP: " << result.target_dsp_freq << - " Actual DSP: " << result.actual_dsp_freq; - } - else { - //set the centre frequency - etiLog.level(info) << std::fixed << std::setprecision(3) << - "OutputUHD:Setting freq to " << myConf.frequency << "..."; - myUsrp->set_tx_freq(myConf.frequency); - } + tune_usrp_to(myUsrp, myConf.lo_offset, myConf.frequency); myConf.frequency = myUsrp->get_tx_freq(); etiLog.level(info) << std::fixed << std::setprecision(3) << - "OutputUHD:Actual frequency: " << myConf.frequency; + "OutputUHD:Actual TX frequency: " << myConf.frequency; + + etiLog.level(info) << std::fixed << std::setprecision(3) << + "OutputUHD:Actual RX frequency: " << myUsrp->get_tx_freq(); myUsrp->set_tx_gain(myConf.txgain); MDEBUG("OutputUHD:Actual TX Gain: %f ...\n", myUsrp->get_tx_gain()); @@ -284,6 +298,9 @@ OutputUHD::OutputUHD( SetDelayBuffer(myConf.dabMode); + myUsrp->set_rx_gain(myConf.rxgain); + MDEBUG("OutputUHD:Actual RX Gain: %f ...\n", myUsrp->get_rx_gain()); + uhdFeedback.setup(myUsrp, myConf.dpdFeedbackServerPort, myConf.sampleRate); MDEBUG("OutputUHD:UHD ready.\n"); @@ -910,9 +927,13 @@ void OutputUHD::set_parameter(const string& parameter, const string& value) ss >> myConf.txgain; myUsrp->set_tx_gain(myConf.txgain); } + else if (parameter == "rxgain") { + ss >> myConf.rxgain; + myUsrp->set_rx_gain(myConf.rxgain); + } else if (parameter == "freq") { ss >> myConf.frequency; - myUsrp->set_tx_freq(myConf.frequency); + tune_usrp_to(myUsrp, myConf.lo_offset, myConf.frequency); myConf.frequency = myUsrp->get_tx_freq(); } else if (parameter == "muting") { @@ -951,6 +972,9 @@ const string OutputUHD::get_parameter(const string& parameter) const if (parameter == "txgain") { ss << myConf.txgain; } + else if (parameter == "rxgain") { + ss << myConf.rxgain; + } else if (parameter == "freq") { ss << myConf.frequency; } diff --git a/src/OutputUHD.h b/src/OutputUHD.h index 1246fc5..c966c7e 100644 --- a/src/OutputUHD.h +++ b/src/OutputUHD.h @@ -189,6 +189,7 @@ struct OutputUHDConfig { double frequency = 0.0; double lo_offset = 0.0; double txgain = 0.0; + double rxgain = 0.0; bool enableSync = false; bool muteNoTimestamps = false; unsigned dabMode = 0; diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 9e3aab2..788b0a9 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -153,9 +153,6 @@ void OutputUHDFeedback::ReceiveBurstThread() const double usrp_time = m_usrp->get_time_now().get_real_secs(); const double cmd_time = cmd.time_spec.get_real_secs(); - etiLog.level(debug) << - "RX stream command ts=" << std::fixed << cmd_time << " Delta=" << cmd_time - usrp_time; - rxStream->issue_stream_cmd(cmd); uhd::rx_metadata_t md; @@ -173,9 +170,10 @@ void OutputUHDFeedback::ReceiveBurstThread() burstRequest.rx_second = md.time_spec.get_full_secs(); burstRequest.rx_pps = md.time_spec.get_frac_secs() * 16384000.0; - etiLog.level(debug) << "Read " << samples_read << " RX feedback samples " - << "at time " << std::fixed << burstRequest.tx_second << "." << - burstRequest.tx_pps / 16384000.0; + etiLog.level(debug) << "DPD: acquired " << samples_read << " RX feedback samples " << + "at time " << burstRequest.tx_second << " + " << + std::fixed << burstRequest.tx_pps / 16384000.0 << + " Delta=" << cmd_time - usrp_time; burstRequest.state = BurstRequestState::Acquired; -- cgit v1.2.3 From 5733a5c9a1a070ebd30ce55e181d5f0d8917de60 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 16:57:53 +0200 Subject: Set RX antenna to RX2 --- src/OutputUHD.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index f764fb8..1a137d3 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -298,6 +298,9 @@ OutputUHD::OutputUHD( SetDelayBuffer(myConf.dabMode); + myUsrp->set_rx_antenna("RX2"); + MDEBUG("OutputUHD:Set RX Antenna: %s ...\n", myUsrp->get_rx_antenna().c_str()); + myUsrp->set_rx_gain(myConf.rxgain); MDEBUG("OutputUHD:Actual RX Gain: %f ...\n", myUsrp->get_rx_gain()); -- cgit v1.2.3 From d57fe2c74f2c9e0a76f4b2c577942837dfac0866 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 17:20:29 +0200 Subject: DPD: Set RX sample rate --- dpd/README.md | 6 ++++-- dpd/dpd.ini | 4 ++-- src/OutputUHD.cpp | 3 +++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/dpd/README.md b/dpd/README.md index 96c4fb0..ec7cec2 100644 --- a/dpd/README.md +++ b/dpd/README.md @@ -8,11 +8,13 @@ This folder contains work in progress for digital predistortion. It requires: - A feedback connection from the power amplifier output, at an appropriate power level for the B200. Usually this is done with a directional coupler. - ODR-DabMod with enabled dpd_port, and with a samplerate of 8192000 samples per second. -- Synchronous=1 so that the USRP has the timestamping set properly. +- Synchronous=1 so that the USRP has the timestamping set properly, internal refclk and pps + are sufficient for this example. +- A live mux source with TIST enabled. See dpd/dpd.ini for an example. TODO ---- -Fix timestamps and test if frame data is valid. +Implement a PA model that updates the predistorter. diff --git a/dpd/dpd.ini b/dpd/dpd.ini index 625df73..910f251 100644 --- a/dpd/dpd.ini +++ b/dpd/dpd.ini @@ -14,7 +14,7 @@ transport=tcp source=localhost:9200 [modulator] -digital_gain=0.9 +digital_gain=0.8 rate=8192000 [firfilter] @@ -34,7 +34,7 @@ pps_source=none behaviour_refclk_lock_lost=ignore max_gps_holdover_time=600 dpd_port=50055 -rxgain=10 +rxgain=0 [delaymanagement] ; Use synchronous=1 so that the USRP time is set. This works diff --git a/src/OutputUHD.cpp b/src/OutputUHD.cpp index 1a137d3..c2f985b 100644 --- a/src/OutputUHD.cpp +++ b/src/OutputUHD.cpp @@ -298,6 +298,9 @@ OutputUHD::OutputUHD( SetDelayBuffer(myConf.dabMode); + myUsrp->set_rx_rate(myConf.sampleRate); + MDEBUG("OutputUHD:Actual RX Rate: %f sps...\n", myUsrp->get_rx_rate()); + myUsrp->set_rx_antenna("RX2"); MDEBUG("OutputUHD:Set RX Antenna: %s ...\n", myUsrp->get_rx_antenna().c_str()); -- cgit v1.2.3 From a34f81d00b9a3f247fa6bd2e31d2c95d4ce7c12f Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 12 May 2017 17:53:03 +0200 Subject: Add animation support to show_spectrum --- dpd/show_spectrum.py | 99 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 26 deletions(-) diff --git a/dpd/show_spectrum.py b/dpd/show_spectrum.py index fcc682f..b9e0180 100755 --- a/dpd/show_spectrum.py +++ b/dpd/show_spectrum.py @@ -19,16 +19,30 @@ import socket import struct import numpy as np import matplotlib.pyplot as pp -import scipy.signal +from matplotlib.animation import FuncAnimation +import argparse SIZEOF_SAMPLE = 8 # complex floats -if len(sys.argv) != 3: - print("Usage: show_spectrum.py ") - sys.exit(1) +def main(): + parser = argparse.ArgumentParser(description="Plot the spectrum of ODR-DabMod's DPD feedback") + parser.add_argument('--samps', default='10240', help='Number of samples to request at once', + required=False) + parser.add_argument('--port', default='50055', + help='port to connect to ODR-DabMod DPD (default: 50055)', + required=False) -port = int(sys.argv[1]) -num_samps_to_request = int(sys.argv[2]) + parser.add_argument('--animated', action='store_true', help='Enable real-time animation') + + cli_args = parser.parse_args() + + port = int(cli_args.port) + num_samps_to_request = int(cli_args.samps) + + if cli_args.animated: + plot_spectrum_animated(port, num_samps_to_request) + else: + plot_spectrum_once(port, num_samps_to_request) def recv_exact(sock, num_bytes): bufs = [] @@ -84,33 +98,66 @@ def get_samples(port, num_samps_to_request): return (tx_ts, txframe, rx_ts, rxframe) +sampling_rate = 8192000 +fft_size = 4096 +freqs = np.fft.fftshift(np.fft.fftfreq(fft_size, d=1./sampling_rate)) -tx_ts, txframe, rx_ts, rxframe = get_samples(port, num_samps_to_request) +def plot_spectrum_once(port, num_samps_to_request): + tx_ts, txframe, rx_ts, rxframe = get_samples(port, num_samps_to_request) -print("Received {} & {} frames at {} and {}".format( - len(txframe), len(rxframe), tx_ts, rx_ts)) + print("Received {} & {} frames at {} and {}".format( + len(txframe), len(rxframe), tx_ts, rx_ts)) -print("Calculate TX and RX spectrum assuming 8192000 samples per second") -fft_size = 4096 -tx_spectrum = np.fft.fftshift(np.fft.fft(txframe, fft_size)) -tx_power = 20*np.log10(np.abs(tx_spectrum)) + print("Calculate TX and RX spectrum assuming 8192000 samples per second") + tx_spectrum = np.fft.fftshift(np.fft.fft(txframe, fft_size)) + tx_power = 20*np.log10(np.abs(tx_spectrum)) -rx_spectrum = np.fft.fftshift(np.fft.fft(rxframe, fft_size)) -rx_power = 20*np.log10(np.abs(rx_spectrum)) + rx_spectrum = np.fft.fftshift(np.fft.fft(rxframe, fft_size)) + rx_power = 20*np.log10(np.abs(rx_spectrum)) -sampling_rate = 8192000 -freqs = np.fft.fftshift(np.fft.fftfreq(fft_size, d=1./sampling_rate)) + fig = pp.figure() + + fig.suptitle("TX and RX spectrum") + ax1 = fig.add_subplot(211) + ax1.set_title("TX") + ax1.plot(freqs, tx_power, 'r') + ax2 = fig.add_subplot(212) + ax2.set_title("RX") + ax2.plot(freqs, rx_power, 'b') + pp.show() + +def plot_spectrum_animated(port, num_samps_to_request): + fig, axes = pp.subplots(2, sharex=True) + line1, = axes[0].plot(freqs, np.ones(len(freqs)), 'r', animated=True) + axes[0].set_title("TX") + line2, = axes[1].plot(freqs, np.ones(len(freqs)), 'b', animated=True) + axes[1].set_title("RX") + lines = [line1, line2] + + axes[0].set_ylim(-30, 50) + axes[1].set_ylim(-60, 40) + + def update(frame): + tx_ts, txframe, rx_ts, rxframe = get_samples(port, num_samps_to_request) + + print("Received {} & {} frames at {} and {}".format( + len(txframe), len(rxframe), tx_ts, rx_ts)) + + print("Calculate TX and RX spectrum assuming 8192000 samples per second") + tx_spectrum = np.fft.fftshift(np.fft.fft(txframe, fft_size)) + tx_power = 20*np.log10(np.abs(tx_spectrum)) + + rx_spectrum = np.fft.fftshift(np.fft.fft(rxframe, fft_size)) + rx_power = 20*np.log10(np.abs(rx_spectrum)) + + lines[0].set_ydata(tx_power) + lines[1].set_ydata(rx_power) + return lines -fig = pp.figure() + ani = FuncAnimation(fig, update, blit=True) + pp.show() -fig.suptitle("TX and RX spectrum") -ax1 = fig.add_subplot(211) -ax1.set_title("TX") -ax1.plot(freqs, tx_power) -ax2 = fig.add_subplot(212) -ax2.set_title("RX") -ax2.plot(freqs, rx_power) -pp.show() +main() # The MIT License (MIT) # -- cgit v1.2.3 From 7e536b0169cd606dac6c2f4241a520e1c68bfa4a Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sat, 13 May 2017 11:06:57 +0200 Subject: DPD: Move power calculation into function --- dpd/show_spectrum.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/dpd/show_spectrum.py b/dpd/show_spectrum.py index b9e0180..83ecd8d 100755 --- a/dpd/show_spectrum.py +++ b/dpd/show_spectrum.py @@ -98,11 +98,7 @@ def get_samples(port, num_samps_to_request): return (tx_ts, txframe, rx_ts, rxframe) -sampling_rate = 8192000 -fft_size = 4096 -freqs = np.fft.fftshift(np.fft.fftfreq(fft_size, d=1./sampling_rate)) - -def plot_spectrum_once(port, num_samps_to_request): +def get_spectrum(port, num_samps_to_request): tx_ts, txframe, rx_ts, rxframe = get_samples(port, num_samps_to_request) print("Received {} & {} frames at {} and {}".format( @@ -114,7 +110,15 @@ def plot_spectrum_once(port, num_samps_to_request): rx_spectrum = np.fft.fftshift(np.fft.fft(rxframe, fft_size)) rx_power = 20*np.log10(np.abs(rx_spectrum)) + return tx_power, rx_power + + +sampling_rate = 8192000 +fft_size = 4096 +freqs = np.fft.fftshift(np.fft.fftfreq(fft_size, d=1./sampling_rate)) +def plot_spectrum_once(port, num_samps_to_request): + tx_power, rx_power = get_spectrum(port, num_samps_to_request) fig = pp.figure() fig.suptitle("TX and RX spectrum") @@ -138,17 +142,7 @@ def plot_spectrum_animated(port, num_samps_to_request): axes[1].set_ylim(-60, 40) def update(frame): - tx_ts, txframe, rx_ts, rxframe = get_samples(port, num_samps_to_request) - - print("Received {} & {} frames at {} and {}".format( - len(txframe), len(rxframe), tx_ts, rx_ts)) - - print("Calculate TX and RX spectrum assuming 8192000 samples per second") - tx_spectrum = np.fft.fftshift(np.fft.fft(txframe, fft_size)) - tx_power = 20*np.log10(np.abs(tx_spectrum)) - - rx_spectrum = np.fft.fftshift(np.fft.fft(rxframe, fft_size)) - rx_power = 20*np.log10(np.abs(rx_spectrum)) + tx_power, rx_power = get_spectrum(port, num_samps_to_request) lines[0].set_ydata(tx_power) lines[1].set_ydata(rx_power) -- cgit v1.2.3 From acec874ecd00b9a1f87b0d1405f6fa1e6503d2b7 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sat, 13 May 2017 15:27:35 +0200 Subject: DPD: calculate spectrum in complex double --- dpd/show_spectrum.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dpd/show_spectrum.py b/dpd/show_spectrum.py index 83ecd8d..e92c1d0 100755 --- a/dpd/show_spectrum.py +++ b/dpd/show_spectrum.py @@ -101,6 +101,10 @@ def get_samples(port, num_samps_to_request): def get_spectrum(port, num_samps_to_request): tx_ts, txframe, rx_ts, rxframe = get_samples(port, num_samps_to_request) + # convert to complex doubles for more dynamic range + txframe = txframe.astype(np.complex128) + rxframe = rxframe.astype(np.complex128) + print("Received {} & {} frames at {} and {}".format( len(txframe), len(rxframe), tx_ts, rx_ts)) -- cgit v1.2.3 From a2957184946e48bd4644325a044698fbe0c24ee7 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sat, 13 May 2017 15:27:56 +0200 Subject: Print info about DPD port --- src/OutputUHDFeedback.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/OutputUHDFeedback.cpp b/src/OutputUHDFeedback.cpp index 788b0a9..2a99e6b 100644 --- a/src/OutputUHDFeedback.cpp +++ b/src/OutputUHDFeedback.cpp @@ -240,6 +240,8 @@ void OutputUHDFeedback::ServeFeedbackThread() throw std::runtime_error("Can't listen TCP socket"); } + etiLog.level(info) << "DPD Feedback server listening on port " << m_port; + while (m_running) { struct sockaddr_in client; int client_sock = accept_with_timeout(m_server_sock, 1000, &client); -- cgit v1.2.3 From bfe0a3fc254bc3117955e64532d8ff7a0de0b1fe Mon Sep 17 00:00:00 2001 From: andreas128 Date: Mon, 29 May 2017 22:25:41 +0100 Subject: Add store_received with alignment --- dpd/store_received.py | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100755 dpd/store_received.py diff --git a/dpd/store_received.py b/dpd/store_received.py new file mode 100755 index 0000000..902f607 --- /dev/null +++ b/dpd/store_received.py @@ -0,0 +1,157 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# This is an example tool that shows how to connect to ODR-DabMod's dpd TCP server +# and get samples from there. +# +# Since the TX and RX samples are not perfectly aligned, the tool has to align them properly, +# which is done in two steps: First on sample-level using a correlation, then with subsample +# accuracy using a FFT approach. +# +# It requires SciPy and matplotlib. +# +# Copyright (C) 2017 Matthias P. Braendli +# http://www.opendigitalradio.org +# Licence: The MIT License, see notice at the end of this file + +import sys +import socket +import struct +import numpy as np +import matplotlib.pyplot as pp +from matplotlib.animation import FuncAnimation +import argparse +import os +import time +import src.dab_util as du + +SIZEOF_SAMPLE = 8 # complex floats + +def main(): + parser = argparse.ArgumentParser(description="Plot the spectrum of ODR-DabMod's DPD feedback") + parser.add_argument('--samps', default='10240', help='Number of samples to request at once', + required=False) + parser.add_argument('--port', default='50055', + help='port to connect to ODR-DabMod DPD (default: 50055)', + required=False) + parser.add_argument('--out_dir', default='/tmp/record', help='Output directory', + required=False) + parser.add_argument('--count', default='1', help='Number of recordings', + required=False) + parser.add_argument('--verbose', type=int, default=0, help='Level of verbosity', + required=False) + + parser.add_argument('--animated', action='store_true', help='Enable real-time animation') + + + cli_args = parser.parse_args() + + if not os.path.isdir(cli_args.out_dir): + os.mkdir(cli_args.out_dir) + + port = int(cli_args.port) + num_samps_to_request = int(cli_args.samps) + + for i in range(int(cli_args.count)): + if i>0: + time.sleep(0.1) + + tx_ts, txframe, rx_ts, rxframe = get_samples(port, num_samps_to_request) + + txframe_aligned, rxframe_aligned = du.subsample_align(txframe, rxframe) + + if cli_args.verbose >= 1: + n_up = 32 + lag = du.lag_upsampling(txframe, rxframe, n_up) + lag_aligned = du.lag_upsampling(txframe_aligned, rxframe_aligned, n_up) + print("Lag from %d times oversampled signal:" % n_up) + print("Before alignment: %.2f" % lag) + print("After alignment: %.2f" % lag_aligned) + print("") + + txframe_aligned.tofile("%s/%d_tx_record.iq" % (cli_args.out_dir, i)) + rxframe_aligned.tofile("%s/%d_rx_record.iq" % (cli_args.out_dir, i)) + + +def recv_exact(sock, num_bytes): + bufs = [] + while num_bytes > 0: + b = sock.recv(num_bytes) + if len(b) == 0: + break + num_bytes -= len(b) + bufs.append(b) + return b''.join(bufs) + +def get_samples(port, num_samps_to_request): + """Connect to ODR-DabMod, retrieve TX and RX samples, load + into numpy arrays, and return a tuple + (tx_timestamp, tx_samples, rx_timestamp, rx_samples) + where the timestamps are doubles, and the samples are numpy + arrays of complex floats, both having the same size + """ + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect(('localhost', port)) + + print("Send version"); + s.sendall(b"\x01") + + print("Send request for {} samples".format(num_samps_to_request)) + s.sendall(struct.pack("=I", num_samps_to_request)) + + print("Wait for TX metadata") + num_samps, tx_second, tx_pps = struct.unpack("=III", recv_exact(s, 12)) + tx_ts = tx_second + tx_pps / 16384000.0 + + if num_samps > 0: + print("Receiving {} TX samples".format(num_samps)) + txframe_bytes = recv_exact(s, num_samps * SIZEOF_SAMPLE) + txframe = np.fromstring(txframe_bytes, dtype=np.complex64) + else: + txframe = np.array([], dtype=np.complex64) + + print("Wait for RX metadata") + rx_second, rx_pps = struct.unpack("=II", recv_exact(s, 8)) + rx_ts = rx_second + rx_pps / 16384000.0 + + if num_samps > 0: + print("Receiving {} RX samples".format(num_samps)) + rxframe_bytes = recv_exact(s, num_samps * SIZEOF_SAMPLE) + rxframe = np.fromstring(rxframe_bytes, dtype=np.complex64) + else: + rxframe = np.array([], dtype=np.complex64) + + print("Disconnecting") + s.close() + + return (tx_ts, txframe, rx_ts, rxframe) + + +sampling_rate = 8192000 +fft_size = 4096 +freqs = np.fft.fftshift(np.fft.fftfreq(fft_size, d=1./sampling_rate)) + +main() + +# The MIT License (MIT) +# +# Copyright (c) 2017 Matthias P. Braendli +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. -- cgit v1.2.3 From 418eb33d0948bb12b7b2ed2179d43ad66258aa72 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Mon, 29 May 2017 22:51:19 +0100 Subject: Configure txgrain --- dpd/dpd.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpd/dpd.ini b/dpd/dpd.ini index 910f251..5e809e5 100644 --- a/dpd/dpd.ini +++ b/dpd/dpd.ini @@ -27,7 +27,7 @@ output=uhd device= master_clock_rate=32768000 type=b200 -txgain=50 +txgain=75 channel=13C refclk_source=internal pps_source=none -- cgit v1.2.3