diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-05-12 10:44:33 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2017-05-12 10:44:33 +0200 |
commit | 201d2cd2e0431a5ea79fb69561c27555f3a03dc1 (patch) | |
tree | b3814f6f91ba6b9594abf7d89f88cf022ab889ae | |
parent | 5ac10e37d07cfe723ea4b396f08563889dff5a2b (diff) | |
download | dabmod-201d2cd2e0431a5ea79fb69561c27555f3a03dc1.tar.gz dabmod-201d2cd2e0431a5ea79fb69561c27555f3a03dc1.tar.bz2 dabmod-201d2cd2e0431a5ea79fb69561c27555f3a03dc1.zip |
Add dpd example script to fetch samples
-rw-r--r-- | dpd/README.md | 10 | ||||
-rwxr-xr-x | dpd/show_spectrum.py | 99 | ||||
-rw-r--r-- | src/OutputUHDFeedback.cpp | 208 |
3 files changed, 214 insertions, 103 deletions
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 <port> <number of samples to request>") + 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 <vector> #include <complex> +#include <cstring> #include <uhd/types/stream_cmd.hpp> #include <sys/socket.h> +#include <errno.h> #include <poll.h> #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); |