diff options
author | Andrej Rode <andrej.rode@ettus.com> | 2017-02-09 11:16:16 -0800 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2018-06-20 19:02:32 -0500 |
commit | 22e24497a510c174e6de7718ad918a423d1973dd (patch) | |
tree | 436bc3d08451dbb2cc70f4c11c611cdd4ff23f78 /host/examples | |
parent | 91a5518443f4ff938f67a2f1bd1b09b24bceecd5 (diff) | |
download | uhd-22e24497a510c174e6de7718ad918a423d1973dd.tar.gz uhd-22e24497a510c174e6de7718ad918a423d1973dd.tar.bz2 uhd-22e24497a510c174e6de7718ad918a423d1973dd.zip |
python: Initial commit of Python API
Initial commit of the Python API using Boost.Python. Bind the
MultiUSRP API for use in Python. Bindings intended to provide as
complete coverage as possible.
- Wrap most multi_usrp calls
- Adding multi channel send/recv examples in examples/python
- Adding setuptools support
- Initial attempt at binding the UHD types and filters
Diffstat (limited to 'host/examples')
-rw-r--r-- | host/examples/CMakeLists.txt | 4 | ||||
-rw-r--r-- | host/examples/python/CMakeLists.txt | 23 | ||||
-rwxr-xr-x | host/examples/python/pyuhd_rx_to_file.py | 46 | ||||
-rwxr-xr-x | host/examples/python/pyuhd_tx_waveforms.py | 64 |
4 files changed, 137 insertions, 0 deletions
diff --git a/host/examples/CMakeLists.txt b/host/examples/CMakeLists.txt index 56d1e4f1f..e49fcdb78 100644 --- a/host/examples/CMakeLists.txt +++ b/host/examples/CMakeLists.txt @@ -102,3 +102,7 @@ IF(ENABLE_C_API) ENDFOREACH(example ${C_API_EXAMPLES}) ENDIF(HAVE_C99_STRUCTDECL) ENDIF(ENABLE_C_API) + +IF(ENABLE_PYTHON_API) + ADD_SUBDIRECTORY(python) +ENDIF(ENABLE_PYTHON_API) diff --git a/host/examples/python/CMakeLists.txt b/host/examples/python/CMakeLists.txt new file mode 100644 index 000000000..628bf10a2 --- /dev/null +++ b/host/examples/python/CMakeLists.txt @@ -0,0 +1,23 @@ +# +# Copyright 2010-2012,2015 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 <http://www.gnu.org/licenses/>. +# + +SET(python_examples + pyuhd_rx_to_file.py + pyuhd_tx_waveforms.py +) + +UHD_INSTALL(PROGRAMS ${python_examples} DESTINATION ${PKG_LIB_DIR}/examples COMPONENT examples) diff --git a/host/examples/python/pyuhd_rx_to_file.py b/host/examples/python/pyuhd_rx_to_file.py new file mode 100755 index 000000000..0b4956b33 --- /dev/null +++ b/host/examples/python/pyuhd_rx_to_file.py @@ -0,0 +1,46 @@ +#! /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 <http://www.gnu.org/licenses/>. +# + + +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 new file mode 100755 index 000000000..d7d431f44 --- /dev/null +++ b/host/examples/python/pyuhd_tx_waveforms.py @@ -0,0 +1,64 @@ +#! /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 <http://www.gnu.org/licenses/>. +# + +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() |