From e74cf7635ba3360b5b7002a2f7317941f65ffa16 Mon Sep 17 00:00:00 2001 From: Paul David Date: Tue, 2 May 2017 14:10:05 -0400 Subject: python: Separating exposed Python data structures - Separating exposed Python data structures into logical sections - Exposes all of the multi_usrp API - Adds a layer of Python for documentation and adding helper methods - Adds improvements and fixes to the MultiUSRP object - Includes additional exposed data structures (like time_spec_t, etc.) - Add code to release the Python GIL during long C++ calls --- host/examples/python/CMakeLists.txt | 21 +++------- host/examples/python/pyuhd_rx_to_file.py | 46 --------------------- host/examples/python/pyuhd_tx_waveforms.py | 64 ------------------------------ host/examples/python/rx_to_file.py | 46 +++++++++++++++++++++ host/examples/python/tx_waveforms.py | 58 +++++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 126 deletions(-) delete mode 100755 host/examples/python/pyuhd_rx_to_file.py delete mode 100755 host/examples/python/pyuhd_tx_waveforms.py create mode 100755 host/examples/python/rx_to_file.py create mode 100755 host/examples/python/tx_waveforms.py (limited to 'host/examples') diff --git a/host/examples/python/CMakeLists.txt b/host/examples/python/CMakeLists.txt index 628bf10a2..4b75f57bd 100644 --- a/host/examples/python/CMakeLists.txt +++ b/host/examples/python/CMakeLists.txt @@ -1,23 +1,12 @@ # -# Copyright 2010-2012,2015 Ettus Research LLC +# Copyright 2017-2018 Ettus Research, a National Instruments Company # -# This program 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. -# -# This program 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 this program. If not, see . +# SPDX-License-Identifier: GPL-3.0-or-later # SET(python_examples - pyuhd_rx_to_file.py - pyuhd_tx_waveforms.py + rx_to_file.py + tx_waveforms.py ) -UHD_INSTALL(PROGRAMS ${python_examples} DESTINATION ${PKG_LIB_DIR}/examples COMPONENT examples) +UHD_INSTALL(PROGRAMS ${python_examples} DESTINATION ${PKG_LIB_DIR}/examples/python COMPONENT examples) diff --git a/host/examples/python/pyuhd_rx_to_file.py b/host/examples/python/pyuhd_rx_to_file.py deleted file mode 100755 index 0b4956b33..000000000 --- a/host/examples/python/pyuhd_rx_to_file.py +++ /dev/null @@ -1,46 +0,0 @@ -#! /usr/bin/env python -# -# Copyright 2017 Ettus Research LLC -# -# This program 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. -# -# This program 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 this program. If not, see . -# - - -import numpy as np -import uhd -import argparse - -def parse_args(): - parser = argparse.ArgumentParser() - parser.add_argument("-a", "--args", default="", type=str) - parser.add_argument("-o", "--output-file", type=str, required=True) - parser.add_argument("-f", "--freq", type=float, required=True) - parser.add_argument("-r", "--rate", default=1e6, type=float) - parser.add_argument("-d", "--duration", default=5.0, type=float) - parser.add_argument("-c", "--channels", default=0, nargs="+", type=int) - parser.add_argument("-g", "--gain", type=int, default=10) - return parser.parse_args() - -def main(): - args = parse_args() - usrp = uhd.multi_usrp(args.args) - num_samps = int(np.ceil(args.duration*args.rate)) - if not isinstance(args.channels, list): - args.channels = [args.channels] - samps = usrp.recv_num_samps(num_samps, args.freq, args.rate, args.channels, args.gain) - with open(args.output_file, 'wb') as f: - np.save(f, samps, allow_pickle=False, fix_imports=False) - -if __name__ == "__main__": - main() diff --git a/host/examples/python/pyuhd_tx_waveforms.py b/host/examples/python/pyuhd_tx_waveforms.py deleted file mode 100755 index d7d431f44..000000000 --- a/host/examples/python/pyuhd_tx_waveforms.py +++ /dev/null @@ -1,64 +0,0 @@ -#! /usr/bin/env python -# -# Copyright 2017 Ettus Research LLC -# -# This program 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. -# -# This program 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 this program. If not, see . -# - -import numpy as np -import uhd -import argparse - - -waveforms = { - "sine": lambda n, tone_offset, rate: np.exp(n * 2j * np.pi * tone_offset / rate), - "square": lambda n, tone_offset, rate: np.sign(waveforms["sine"](n, tone_offset, rate)), - "const": lambda n, tone_offset, rate: 1 + 1j, - "ramp": lambda n, tone_offset, rate: 2*(n*(tone_offset/rate) - np.floor(float(0.5 + n*(tone_offset/rate)))) -} - - -def parse_args(): - parser = argparse.ArgumentParser() - parser.add_argument("-a", "--args", default="", type=str) - parser.add_argument( - "-w", "--waveform", default="sine", choices=waveforms.keys(), type=str) - parser.add_argument("-f", "--freq", type=float, required=True) - parser.add_argument("-r", "--rate", default=1e6, type=float) - parser.add_argument("-d", "--duration", default=5.0, type=float) - parser.add_argument("-c", "--channels", default=0, nargs="+", type=int) - parser.add_argument("-g", "--gain", type=int, default=10) - parser.add_argument("--wave-freq", default=1e4, type=float) - parser.add_argument("--wave-ampl", default=0.3, type=float) - return parser.parse_args() - - -def main(): - args = parse_args() - usrp = uhd.multi_usrp(args.args) - if not isinstance(args.channels, list): - args.channels = [args.channels] - data = np.array( - map(lambda n: args.wave_ampl * waveforms[args.waveform](n, args.wave_freq, args.rate), - np.arange( - int(10 * np.floor(args.rate / args.wave_freq)), - dtype=np.complex64)), - dtype=np.complex64) # One period - - usrp.send_waveform(data, args.duration, args.freq, args.rate, - args.channels, args.gain) - - -if __name__ == "__main__": - main() diff --git a/host/examples/python/rx_to_file.py b/host/examples/python/rx_to_file.py new file mode 100755 index 000000000..7017a08c2 --- /dev/null +++ b/host/examples/python/rx_to_file.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# +# Copyright 2017-2018 Ettus Research, a National Instruments Company +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +""" +RX samples to file using Python API +""" + +import argparse +import numpy as np +import uhd + + +def parse_args(): + """Parse the command line arguments""" + parser = argparse.ArgumentParser() + parser.add_argument("-a", "--args", default="", type=str) + parser.add_argument("-o", "--output-file", type=str, required=True) + parser.add_argument("-f", "--freq", type=float, required=True) + parser.add_argument("-r", "--rate", default=1e6, type=float) + parser.add_argument("-d", "--duration", default=5.0, type=float) + parser.add_argument("-c", "--channels", default=0, nargs="+", type=int) + parser.add_argument("-g", "--gain", type=int, default=10) + parser.add_argument("-n", "--numpy", default=False, action="store_true", + help="Save output file in NumPy format (default: No)") + return parser.parse_args() + + +def main(): + """RX samples and write to file""" + args = parse_args() + usrp = uhd.usrp.MultiUSRP(args.args) + num_samps = int(np.ceil(args.duration*args.rate)) + if not isinstance(args.channels, list): + args.channels = [args.channels] + samps = usrp.recv_num_samps(num_samps, args.freq, args.rate, args.channels, args.gain) + with open(args.output_file, 'wb') as out_file: + if args.numpy: + np.save(out_file, samps, allow_pickle=False, fix_imports=False) + else: + samps.tofile(out_file) + +if __name__ == "__main__": + main() diff --git a/host/examples/python/tx_waveforms.py b/host/examples/python/tx_waveforms.py new file mode 100755 index 000000000..6db50fa2b --- /dev/null +++ b/host/examples/python/tx_waveforms.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# +# Copyright 2017-2018 Ettus Research, a National Instruments Company +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +""" +Generate and TX samples using a set of waveforms, and waveform characteristics +""" + +import argparse +import numpy as np +import uhd + +waveforms = { + "sine": lambda n, tone_offset, rate: np.exp(n * 2j * np.pi * tone_offset / rate), + "square": lambda n, tone_offset, rate: np.sign(waveforms["sine"](n, tone_offset, rate)), + "const": lambda n, tone_offset, rate: 1 + 1j, + "ramp": lambda n, tone_offset, rate: + 2*(n*(tone_offset/rate) - np.floor(float(0.5 + n*(tone_offset/rate)))) +} + + +def parse_args(): + """Parse the command line arguments""" + parser = argparse.ArgumentParser() + parser.add_argument("-a", "--args", default="", type=str) + parser.add_argument( + "-w", "--waveform", default="sine", choices=waveforms.keys(), type=str) + parser.add_argument("-f", "--freq", type=float, required=True) + parser.add_argument("-r", "--rate", default=1e6, type=float) + parser.add_argument("-d", "--duration", default=5.0, type=float) + parser.add_argument("-c", "--channels", default=0, nargs="+", type=int) + parser.add_argument("-g", "--gain", type=int, default=10) + parser.add_argument("--wave-freq", default=1e4, type=float) + parser.add_argument("--wave-ampl", default=0.3, type=float) + return parser.parse_args() + + +def main(): + """TX samples based on input arguments""" + args = parse_args() + usrp = uhd.usrp.MultiUSRP(args.args) + if not isinstance(args.channels, list): + args.channels = [args.channels] + data = np.array( + list(map(lambda n: args.wave_ampl * waveforms[args.waveform](n, args.wave_freq, args.rate), + np.arange( + int(10 * np.floor(args.rate / args.wave_freq)), + dtype=np.complex64))), + dtype=np.complex64) # One period + + usrp.send_waveform(data, args.duration, args.freq, args.rate, + args.channels, args.gain) + + +if __name__ == "__main__": + main() -- cgit v1.2.3