From 79ea83d6b352a7865ff6143ba77fcc0956c6452f Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 30 Aug 2010 12:33:13 -0700 Subject: uhd: fixed short conversion (IQ swap) and added test between short/float --- host/lib/transport/convert_types_impl.hpp | 27 +++++++--- host/lib/transport/gen_convert_types.py | 2 +- host/test/convert_types_test.cpp | 84 ++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 8 deletions(-) diff --git a/host/lib/transport/convert_types_impl.hpp b/host/lib/transport/convert_types_impl.hpp index fdc859883..90618dec6 100644 --- a/host/lib/transport/convert_types_impl.hpp +++ b/host/lib/transport/convert_types_impl.hpp @@ -42,36 +42,51 @@ typedef boost::uint32_t item32_t; /*********************************************************************** * Convert complex short buffer to items32 **********************************************************************/ +static UHD_INLINE item32_t sc16_to_item32(sc16_t num){ + boost::uint16_t real = num.real(); + boost::uint16_t imag = num.imag(); + return (item32_t(real) << 16) | (item32_t(imag) << 0); +} + static UHD_INLINE void sc16_to_item32_nswap( const sc16_t *input, item32_t *output, size_t nsamps ){ - std::memcpy(output, input, nsamps*sizeof(item32_t)); + for (size_t i = 0; i < nsamps; i++){ + output[i] = sc16_to_item32(input[i]); + } } static UHD_INLINE void sc16_to_item32_bswap( const sc16_t *input, item32_t *output, size_t nsamps ){ - const item32_t *item32_input = (const item32_t *)input; for (size_t i = 0; i < nsamps; i++){ - output[i] = uhd::byteswap(item32_input[i]); + output[i] = uhd::byteswap(sc16_to_item32(input[i])); } } /*********************************************************************** * Convert items32 buffer to complex short **********************************************************************/ +static UHD_INLINE sc16_t item32_to_sc16(item32_t item){ + return sc16_t( + boost::int16_t(item >> 16), + boost::int16_t(item >> 0) + ); +} + static UHD_INLINE void item32_to_sc16_nswap( const item32_t *input, sc16_t *output, size_t nsamps ){ - std::memcpy(output, input, nsamps*sizeof(item32_t)); + for (size_t i = 0; i < nsamps; i++){ + output[i] = item32_to_sc16(input[i]); + } } static UHD_INLINE void item32_to_sc16_bswap( const item32_t *input, sc16_t *output, size_t nsamps ){ - item32_t *item32_output = (item32_t *)output; for (size_t i = 0; i < nsamps; i++){ - item32_output[i] = uhd::byteswap(input[i]); + output[i] = item32_to_sc16(uhd::byteswap(input[i])); } } diff --git a/host/lib/transport/gen_convert_types.py b/host/lib/transport/gen_convert_types.py index 951b634d9..17d8ffde0 100755 --- a/host/lib/transport/gen_convert_types.py +++ b/host/lib/transport/gen_convert_types.py @@ -95,7 +95,7 @@ void transport::convert_otw_type_to_io_type( size_t num_samps ){ switch(get_pred(io_type, otw_type)){ - #for $pred in range(4) + #for $pred in range(2**$ph.nbits) case $pred: #set $out_type = $ph.get_host_type($pred) #set $in_type = $ph.get_dev_type($pred) diff --git a/host/test/convert_types_test.cpp b/host/test/convert_types_test.cpp index d132a708b..2148302b6 100644 --- a/host/test/convert_types_test.cpp +++ b/host/test/convert_types_test.cpp @@ -27,7 +27,7 @@ using namespace uhd; //typedefs for complex types -typedef std::complex sc16_t; +typedef std::complex sc16_t; typedef std::complex fc32_t; //extract pointer to POD since using &vector.front() throws in MSVC @@ -158,3 +158,85 @@ BOOST_AUTO_TEST_CASE(test_convert_types_le_fc32){ test_convert_types_fc32(nsamps, io_type, otw_type); } } + +/*********************************************************************** + * Test float to short conversion loopback + **********************************************************************/ +BOOST_AUTO_TEST_CASE(test_convert_types_fc32_to_sc16){ + io_type_t io_type_in(io_type_t::COMPLEX_FLOAT32); + io_type_t io_type_out(io_type_t::COMPLEX_INT16); + + otw_type_t otw_type; + otw_type.byteorder = otw_type_t::BO_NATIVE; + otw_type.width = 16; + + const size_t nsamps = 13; + std::vector input(nsamps); + BOOST_FOREACH(fc32_t &in, input) in = fc32_t( + (std::rand()/float(RAND_MAX/2)) - 1, + (std::rand()/float(RAND_MAX/2)) - 1 + ); + + //convert float to dev + std::vector tmp(nsamps); + transport::convert_io_type_to_otw_type( + pod2ptr(input), io_type_in, + pod2ptr(tmp), otw_type, + nsamps + ); + + //convert dev to short + std::vector output(nsamps); + transport::convert_otw_type_to_io_type( + pod2ptr(tmp), otw_type, + pod2ptr(output), io_type_out, + nsamps + ); + + //test that the inputs and outputs match + for (size_t i = 0; i < nsamps; i++){ + BOOST_CHECK_CLOSE_FRACTION(input[i].real(), output[i].real()/float(32767), float(0.01)); + BOOST_CHECK_CLOSE_FRACTION(input[i].imag(), output[i].imag()/float(32767), float(0.01)); + } +} + +/*********************************************************************** + * Test short to float conversion loopback + **********************************************************************/ +BOOST_AUTO_TEST_CASE(test_convert_types_sc16_to_fc32){ + io_type_t io_type_in(io_type_t::COMPLEX_INT16); + io_type_t io_type_out(io_type_t::COMPLEX_FLOAT32); + + otw_type_t otw_type; + otw_type.byteorder = otw_type_t::BO_NATIVE; + otw_type.width = 16; + + const size_t nsamps = 13; + std::vector input(nsamps); + BOOST_FOREACH(sc16_t &in, input) in = sc16_t( + std::rand()-(RAND_MAX/2), + std::rand()-(RAND_MAX/2) + ); + + //convert short to dev + std::vector tmp(nsamps); + transport::convert_io_type_to_otw_type( + pod2ptr(input), io_type_in, + pod2ptr(tmp), otw_type, + nsamps + ); + + //convert dev to float + std::vector output(nsamps); + transport::convert_otw_type_to_io_type( + pod2ptr(tmp), otw_type, + pod2ptr(output), io_type_out, + nsamps + ); + + //test that the inputs and outputs match + for (size_t i = 0; i < nsamps; i++){ + BOOST_CHECK_CLOSE_FRACTION(input[i].real()/float(32767), output[i].real(), float(0.01)); + BOOST_CHECK_CLOSE_FRACTION(input[i].imag()/float(32767), output[i].imag(), float(0.01)); + } +} -- cgit v1.2.3 From 99494305b35adf5c4f89b4888192c723f32d9da2 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 30 Aug 2010 16:00:35 -0700 Subject: uhd: added interleave/de-interleave type conversion routines --- host/include/uhd/transport/CMakeLists.txt | 1 + host/include/uhd/transport/convert_types.hpp | 37 ++++++++++ host/include/uhd/transport/convert_types.ipp | 43 +++++++++++ host/lib/transport/gen_convert_types.py | 105 ++++++++++++++++++++++----- 4 files changed, 166 insertions(+), 20 deletions(-) create mode 100644 host/include/uhd/transport/convert_types.ipp diff --git a/host/include/uhd/transport/CMakeLists.txt b/host/include/uhd/transport/CMakeLists.txt index 93e9a6485..01856454f 100644 --- a/host/include/uhd/transport/CMakeLists.txt +++ b/host/include/uhd/transport/CMakeLists.txt @@ -22,6 +22,7 @@ INSTALL(FILES bounded_buffer.hpp bounded_buffer.ipp convert_types.hpp + convert_types.ipp if_addrs.hpp udp_simple.hpp udp_zero_copy.hpp diff --git a/host/include/uhd/transport/convert_types.hpp b/host/include/uhd/transport/convert_types.hpp index a4d999240..dc7fa6c1a 100644 --- a/host/include/uhd/transport/convert_types.hpp +++ b/host/include/uhd/transport/convert_types.hpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace uhd{ namespace transport{ @@ -39,6 +40,23 @@ UHD_API void convert_io_type_to_otw_type( size_t num_samps ); +/*! + * Convert IO samples to OWT samples + interleave. + * + * \param io_buffs buffers containing samples + * \param io_type the type of these samples + * \param otw_buff memory to write converted samples + * \param otw_type the type of these samples + * \param nsamps_per_io_buff samples per io_buff + */ +UHD_API void convert_io_type_to_otw_type( + const std::vector &io_buffs, + const io_type_t &io_type, + void *otw_buff, + const otw_type_t &otw_type, + size_t nsamps_per_io_buff +); + /*! * Convert OTW samples to IO samples. * @@ -54,6 +72,25 @@ UHD_API void convert_otw_type_to_io_type( size_t num_samps ); +/*! + * Convert OTW samples to IO samples + de-interleave. + * + * \param otw_buff memory containing samples + * \param otw_type the type of these samples + * \param io_buffs buffers to write converted samples + * \param io_type the type of these samples + * \param nsamps_per_io_buff samples per io_buff + */ +UHD_API void convert_otw_type_to_io_type( + const void *otw_buff, + const otw_type_t &otw_type, + std::vector &io_buffs, + const io_type_t &io_type, + size_t nsamps_per_io_buff +); + }} //namespace +#include + #endif /* INCLUDED_UHD_TRANSPORT_CONVERT_TYPES_HPP */ diff --git a/host/include/uhd/transport/convert_types.ipp b/host/include/uhd/transport/convert_types.ipp new file mode 100644 index 000000000..914ca6f17 --- /dev/null +++ b/host/include/uhd/transport/convert_types.ipp @@ -0,0 +1,43 @@ +// +// Copyright 2010 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 . +// + +#ifndef INCLUDED_UHD_TRANSPORT_CONVERT_TYPES_IPP +#define INCLUDED_UHD_TRANSPORT_CONVERT_TYPES_IPP + +UHD_INLINE void uhd::transport::convert_io_type_to_otw_type( + const void *io_buff, const io_type_t &io_type, + void *otw_buff, const otw_type_t &otw_type, + size_t num_samps +){ + std::vector buffs(1, io_buff); + return uhd::transport::convert_io_type_to_otw_type( + buffs, io_type, otw_buff, otw_type, num_samps + ); +} + +UHD_INLINE void uhd::transport::convert_otw_type_to_io_type( + const void *otw_buff, const otw_type_t &otw_type, + void *io_buff, const io_type_t &io_type, + size_t num_samps +){ + std::vector buffs(1, io_buff); + return uhd::transport::convert_otw_type_to_io_type( + otw_buff, otw_type, buffs, io_type, num_samps + ); +} + +#endif /* INCLUDED_UHD_TRANSPORT_CONVERT_TYPES_IPP */ diff --git a/host/lib/transport/gen_convert_types.py b/host/lib/transport/gen_convert_types.py index 17d8ffde0..adbd22868 100755 --- a/host/lib/transport/gen_convert_types.py +++ b/host/lib/transport/gen_convert_types.py @@ -36,7 +36,8 @@ using namespace uhd; **********************************************************************/ UHD_INLINE boost::uint8_t get_pred( const io_type_t &io_type, - const otw_type_t &otw_type + const otw_type_t &otw_type, + size_t num_chans ){ boost::uint8_t pred = 0; @@ -63,6 +64,14 @@ UHD_INLINE boost::uint8_t get_pred( default: throw std::runtime_error("unhandled io type id"); } + switch(num_chans){ + case 1: pred |= $ph.chan1_p; break; + case 2: pred |= $ph.chan2_p; break; + case 3: pred |= $ph.chan3_p; break; + case 4: pred |= $ph.chan4_p; break; + default: throw std::runtime_error("unhandled number of channels"); + } + return pred; } @@ -70,17 +79,37 @@ UHD_INLINE boost::uint8_t get_pred( * Convert host type to device type **********************************************************************/ void transport::convert_io_type_to_otw_type( - const void *io_buff, const io_type_t &io_type, - void *otw_buff, const otw_type_t &otw_type, - size_t num_samps + const std::vector &io_buffs, + const io_type_t &io_type, + void *otw_buff, + const otw_type_t &otw_type, + size_t nsamps_per_io_buff ){ - switch(get_pred(io_type, otw_type)){ + switch(get_pred(io_type, otw_type, io_buffs.size())){ #for $pred in range(2**$ph.nbits) case $pred: #set $out_type = $ph.get_dev_type($pred) #set $in_type = $ph.get_host_type($pred) - #set $converter = '_'.join([$in_type, 'to', $out_type, $ph.get_swap_type($pred)]) - $(converter)((const $(in_type)_t *)io_buff, ($(out_type)_t *)otw_buff, num_samps); + #set $num_chans = $ph.get_num_chans($pred) + #set $converter = '_'.join([$in_type, 'to', $out_type]) + #if $num_chans == 1 + $(converter)_$ph.get_swap_type($pred)( + reinterpret_cast(io_buffs.front()), + reinterpret_cast<$(out_type)_t *>(otw_buff), + nsamps_per_io_buff + ); + #else + for (size_t i = 0; i < nsamps_per_io_buff; i++){ + #for $j in range($num_chans) + reinterpret_cast<$(out_type)_t *>(otw_buff)[i*$num_chans + $j] = + #if $ph.get_swap_type($pred) == 'bswap' + uhd::byteswap($(converter)(reinterpret_cast(io_buffs[$j])[i])); + #else + $(converter)(reinterpret_cast(io_buffs[$j])[i]); + #end if + #end for + } + #end if break; #end for } @@ -90,17 +119,37 @@ void transport::convert_io_type_to_otw_type( * Convert device type to host type **********************************************************************/ void transport::convert_otw_type_to_io_type( - const void *otw_buff, const otw_type_t &otw_type, - void *io_buff, const io_type_t &io_type, - size_t num_samps + const void *otw_buff, + const otw_type_t &otw_type, + std::vector &io_buffs, + const io_type_t &io_type, + size_t nsamps_per_io_buff ){ - switch(get_pred(io_type, otw_type)){ + switch(get_pred(io_type, otw_type, io_buffs.size())){ #for $pred in range(2**$ph.nbits) case $pred: #set $out_type = $ph.get_host_type($pred) #set $in_type = $ph.get_dev_type($pred) - #set $converter = '_'.join([$in_type, 'to', $out_type, $ph.get_swap_type($pred)]) - $(converter)((const $(in_type)_t *)otw_buff, ($(out_type)_t *)io_buff, num_samps); + #set $num_chans = $ph.get_num_chans($pred) + #set $converter = '_'.join([$in_type, 'to', $out_type]) + #if $num_chans == 1 + $(converter)_$ph.get_swap_type($pred)( + reinterpret_cast(otw_buff), + reinterpret_cast<$(out_type)_t *>(io_buffs.front()), + nsamps_per_io_buff + ); + #else + for (size_t i = 0; i < nsamps_per_io_buff; i++){ + #for $j in range($num_chans) + reinterpret_cast<$(out_type)_t *>(io_buffs[$j])[i] = + #if $ph.get_swap_type($pred) == 'bswap' + $(converter)(uhd::byteswap(reinterpret_cast(otw_buff)[i*$num_chans + $j])); + #else + $(converter)(reinterpret_cast(otw_buff)[i*$num_chans + $j]); + #end if + #end for + } + #end if break; #end for } @@ -118,27 +167,43 @@ class ph: item32_p = 0b00000 sc16_p = 0b00010 fc32_p = 0b00000 + chan1_p = 0b00000 + chan2_p = 0b00100 + chan3_p = 0b01000 + chan4_p = 0b01100 - nbits = 2 #see above + nbits = 4 #see above @staticmethod - def has(pred, flag): return (pred & flag) == flag + def has(pred, mask, flag): return (pred & mask) == flag @staticmethod def get_swap_type(pred): - if ph.has(pred, ph.bswap_p): return 'bswap' - if ph.has(pred, ph.nswap_p): return 'nswap' + mask = 0b1 + if ph.has(pred, mask, ph.bswap_p): return 'bswap' + if ph.has(pred, mask, ph.nswap_p): return 'nswap' raise NotImplementedError @staticmethod def get_dev_type(pred): - if ph.has(pred, ph.item32_p): return 'item32' + mask = 0b0 + if ph.has(pred, mask, ph.item32_p): return 'item32' raise NotImplementedError @staticmethod def get_host_type(pred): - if ph.has(pred, ph.sc16_p): return 'sc16' - if ph.has(pred, ph.fc32_p): return 'fc32' + mask = 0b10 + if ph.has(pred, mask, ph.sc16_p): return 'sc16' + if ph.has(pred, mask, ph.fc32_p): return 'fc32' + raise NotImplementedError + + @staticmethod + def get_num_chans(pred): + mask = 0b1100 + if ph.has(pred, mask, ph.chan1_p): return 1 + if ph.has(pred, mask, ph.chan2_p): return 2 + if ph.has(pred, mask, ph.chan3_p): return 3 + if ph.has(pred, mask, ph.chan4_p): return 4 raise NotImplementedError if __name__ == '__main__': -- cgit v1.2.3