diff --git a/include/ieee802-11/CMakeLists.txt b/include/ieee802-11/CMakeLists.txt index 159c3c6..c7e375e 100644 --- a/include/ieee802-11/CMakeLists.txt +++ b/include/ieee802-11/CMakeLists.txt @@ -70,6 +70,7 @@ list(APPEND include_sources ${generated_includes} api.h chunks_to_symbols.h + constellations.h decode_mac.h decode_signal.h equalize_symbols.h diff --git a/include/ieee802-11/constellations.h b/include/ieee802-11/constellations.h new file mode 100644 index 0000000..bcc79a5 --- /dev/null +++ b/include/ieee802-11/constellations.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2016 Bastian Bloessl + * + * 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_IEEE802_11_CONSTELLATIONS_H +#define INCLUDED_IEEE802_11_CONSTELLATIONS_H + +#include +#include + +namespace gr { +namespace ieee802_11 { + +class IEEE802_11_API constellation_bpsk : virtual public digital::constellation +{ +public: + typedef boost::shared_ptr sptr; + static sptr make(); + +protected: + constellation_bpsk(); +}; + +class IEEE802_11_API constellation_qpsk : virtual public digital::constellation +{ +public: + typedef boost::shared_ptr sptr; + static sptr make(); + +protected: + constellation_qpsk(); +}; + +class IEEE802_11_API constellation_16qam : virtual public digital::constellation +{ +public: + typedef boost::shared_ptr sptr; + static sptr make(); + +protected: + constellation_16qam(); +}; + +class IEEE802_11_API constellation_64qam : virtual public digital::constellation +{ +public: + typedef boost::shared_ptr sptr; + static sptr make(); + +protected: + constellation_64qam(); +}; + +} // namespace ieee802_11 +} // namespace gr + +#endif /* INCLUDED_IEEE802_11_CONSTELLATIONS_H */ + diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index fca0ec2..c2abad8 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -95,7 +95,7 @@ add_dependencies(ieee802_11_generated_sources ieee802_11_generated_includes) list(APPEND gr_ieee802_11_sources ${generated_sources} chunks_to_symbols_impl.cc - constellations.cc + constellations_impl.cc decode_mac.cc decode_signal.cc equalize_symbols.cc diff --git a/lib/constellations_impl.cc b/lib/constellations_impl.cc new file mode 100644 index 0000000..6f7792d --- /dev/null +++ b/lib/constellations_impl.cc @@ -0,0 +1,235 @@ +/* + * Copyright (C) 2016 Bastian Bloessl + * + * 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 . + */ +#include "constellations_impl.h" + +using namespace gr::ieee802_11; + +constellation_bpsk::sptr +constellation_bpsk::make() { + return constellation_bpsk::sptr(new constellation_bpsk_impl()); +} + +constellation_bpsk::constellation_bpsk() {} + +constellation_bpsk_impl::constellation_bpsk_impl() { + d_constellation.resize(2); + d_constellation[0] = gr_complex(-1, 0); + d_constellation[1] = gr_complex(1, 0); + d_rotational_symmetry = 2; + d_dimensionality = 1; + calc_arity(); +} + +constellation_bpsk_impl::~constellation_bpsk_impl() { +} + +unsigned int +constellation_bpsk_impl::decision_maker(const gr_complex *sample) { + return (real(*sample) > 0); +} + + +/**********************************************************/ + +constellation_qpsk::sptr +constellation_qpsk::make() { + return constellation_qpsk::sptr(new constellation_qpsk_impl()); +} + +constellation_qpsk::constellation_qpsk() {} + +constellation_qpsk_impl::constellation_qpsk_impl() { + const float level = sqrt(float(0.5)); + d_constellation.resize(4); + // Gray-coded + d_constellation[0] = gr_complex(-level, -level); + d_constellation[1] = gr_complex( level, -level); + d_constellation[2] = gr_complex(-level, level); + d_constellation[3] = gr_complex( level, level); + + d_rotational_symmetry = 4; + d_dimensionality = 1; + calc_arity(); +} + +constellation_qpsk_impl::~constellation_qpsk_impl() { +} + +unsigned int +constellation_qpsk_impl::decision_maker(const gr_complex *sample) { + return 2*(imag(*sample)>0) + (real(*sample)>0); +} + + +/**********************************************************/ + +constellation_16qam::sptr +constellation_16qam::make() { + return constellation_16qam::sptr(new constellation_16qam_impl()); +} + +constellation_16qam::constellation_16qam() {} + +constellation_16qam_impl::constellation_16qam_impl() +{ + const float level = sqrt(float(0.1)); + d_constellation.resize(16); + + d_constellation[ 0] = gr_complex(-3*level,-3*level); + d_constellation[ 1] = gr_complex( 3*level,-3*level); + d_constellation[ 2] = gr_complex(-1*level,-3*level); + d_constellation[ 3] = gr_complex( 1*level,-3*level); + d_constellation[ 4] = gr_complex(-3*level, 3*level); + d_constellation[ 5] = gr_complex( 3*level, 3*level); + d_constellation[ 6] = gr_complex(-1*level, 3*level); + d_constellation[ 7] = gr_complex( 1*level, 3*level); + d_constellation[ 8] = gr_complex(-3*level,-1*level); + d_constellation[ 9] = gr_complex( 3*level,-1*level); + d_constellation[10] = gr_complex(-1*level,-1*level); + d_constellation[11] = gr_complex( 1*level,-1*level); + d_constellation[12] = gr_complex(-3*level, 1*level); + d_constellation[13] = gr_complex( 3*level, 1*level); + d_constellation[14] = gr_complex(-1*level, 1*level); + d_constellation[15] = gr_complex( 1*level, 1*level); + + d_rotational_symmetry = 4; + d_dimensionality = 1; + calc_arity(); +} + +constellation_16qam_impl::~constellation_16qam_impl() { +} + +unsigned int +constellation_16qam_impl::decision_maker(const gr_complex *sample) +{ + unsigned int ret = 0; + const float level = sqrt(float(0.1)); + float re = sample->real(); + float im = sample->imag(); + + ret |= re > 0; + ret |= (std::abs(re) < (2*level)) << 1; + ret |= (im > 0) << 2; + ret |= (std::abs(im) < (2*level)) << 3; + + return ret; +} + + +/**********************************************************/ + +constellation_64qam::sptr +constellation_64qam::make() { + return constellation_64qam::sptr(new constellation_64qam_impl()); +} + +constellation_64qam::constellation_64qam() {} + +constellation_64qam_impl::constellation_64qam_impl() { + const float level = sqrt(float(1/42.0)); + d_constellation.resize(64); + + d_constellation[ 0] = gr_complex(-7*level,-7*level); + d_constellation[ 1] = gr_complex( 7*level,-7*level); + d_constellation[ 2] = gr_complex(-1*level,-7*level); + d_constellation[ 3] = gr_complex( 1*level,-7*level); + d_constellation[ 4] = gr_complex(-5*level,-7*level); + d_constellation[ 5] = gr_complex( 5*level,-7*level); + d_constellation[ 6] = gr_complex(-3*level,-7*level); + d_constellation[ 7] = gr_complex( 3*level,-7*level); + d_constellation[ 8] = gr_complex(-7*level, 7*level); + d_constellation[ 9] = gr_complex( 7*level, 7*level); + d_constellation[10] = gr_complex(-1*level, 7*level); + d_constellation[11] = gr_complex( 1*level, 7*level); + d_constellation[12] = gr_complex(-5*level, 7*level); + d_constellation[13] = gr_complex( 5*level, 7*level); + d_constellation[14] = gr_complex(-3*level, 7*level); + d_constellation[15] = gr_complex( 3*level, 7*level); + d_constellation[16] = gr_complex(-7*level,-1*level); + d_constellation[17] = gr_complex( 7*level,-1*level); + d_constellation[18] = gr_complex(-1*level,-1*level); + d_constellation[19] = gr_complex( 1*level,-1*level); + d_constellation[20] = gr_complex(-5*level,-1*level); + d_constellation[21] = gr_complex( 5*level,-1*level); + d_constellation[22] = gr_complex(-3*level,-1*level); + d_constellation[23] = gr_complex( 3*level,-1*level); + d_constellation[24] = gr_complex(-7*level, 1*level); + d_constellation[25] = gr_complex( 7*level, 1*level); + d_constellation[26] = gr_complex(-1*level, 1*level); + d_constellation[27] = gr_complex( 1*level, 1*level); + d_constellation[28] = gr_complex(-5*level, 1*level); + d_constellation[29] = gr_complex( 5*level, 1*level); + d_constellation[30] = gr_complex(-3*level, 1*level); + d_constellation[31] = gr_complex( 3*level, 1*level); + d_constellation[32] = gr_complex(-7*level,-5*level); + d_constellation[33] = gr_complex( 7*level,-5*level); + d_constellation[34] = gr_complex(-1*level,-5*level); + d_constellation[35] = gr_complex( 1*level,-5*level); + d_constellation[36] = gr_complex(-5*level,-5*level); + d_constellation[37] = gr_complex( 5*level,-5*level); + d_constellation[38] = gr_complex(-3*level,-5*level); + d_constellation[39] = gr_complex( 3*level,-5*level); + d_constellation[40] = gr_complex(-7*level, 5*level); + d_constellation[41] = gr_complex( 7*level, 5*level); + d_constellation[42] = gr_complex(-1*level, 5*level); + d_constellation[43] = gr_complex( 1*level, 5*level); + d_constellation[44] = gr_complex(-5*level, 5*level); + d_constellation[45] = gr_complex( 5*level, 5*level); + d_constellation[46] = gr_complex(-3*level, 5*level); + d_constellation[47] = gr_complex( 3*level, 5*level); + d_constellation[48] = gr_complex(-7*level,-3*level); + d_constellation[49] = gr_complex( 7*level,-3*level); + d_constellation[50] = gr_complex(-1*level,-3*level); + d_constellation[51] = gr_complex( 1*level,-3*level); + d_constellation[52] = gr_complex(-5*level,-3*level); + d_constellation[53] = gr_complex( 5*level,-3*level); + d_constellation[54] = gr_complex(-3*level,-3*level); + d_constellation[55] = gr_complex( 3*level,-3*level); + d_constellation[56] = gr_complex(-7*level, 3*level); + d_constellation[57] = gr_complex( 7*level, 3*level); + d_constellation[58] = gr_complex(-1*level, 3*level); + d_constellation[59] = gr_complex( 1*level, 3*level); + d_constellation[60] = gr_complex(-5*level, 3*level); + d_constellation[61] = gr_complex( 5*level, 3*level); + d_constellation[62] = gr_complex(-3*level, 3*level); + d_constellation[63] = gr_complex( 3*level, 3*level); + + d_rotational_symmetry = 4; + d_dimensionality = 1; + calc_arity(); +} + +constellation_64qam_impl::~constellation_64qam_impl() { +} + +unsigned int +constellation_64qam_impl::decision_maker(const gr_complex *sample) { + unsigned int ret = 0; + const float level = sqrt(float(1/42.0)); + float re = sample->real(); + float im = sample->imag(); + + ret |= re > 0; + ret |= (std::abs(re) < (4*level)) << 1; + ret |= (std::abs(re) < (6*level) && std::abs(re) > (2*level)) << 2; + ret |= (im > 0) << 3; + ret |= (std::abs(im) < (4*level)) << 4; + ret |= (std::abs(im) < (6*level) && std::abs(im) > (2*level)) << 5; + + return ret; +} diff --git a/lib/constellations_impl.h b/lib/constellations_impl.h new file mode 100644 index 0000000..7519d0b --- /dev/null +++ b/lib/constellations_impl.h @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2016 Bastian Bloessl + * + * 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_IEEE802_11_CONSTELLATIONS_IMPL_H +#define INCLUDED_IEEE802_11_CONSTELLATIONS_IMPL_H + +#include + +namespace gr { +namespace ieee802_11 { + +class constellation_bpsk_impl : public constellation_bpsk +{ +public: + constellation_bpsk_impl(); + ~constellation_bpsk_impl(); + + unsigned int decision_maker(const gr_complex *sample); +}; + + +class constellation_qpsk_impl : public constellation_qpsk +{ +public: + constellation_qpsk_impl(); + ~constellation_qpsk_impl(); + + unsigned int decision_maker(const gr_complex *sample); +}; + + + +class constellation_16qam_impl : public constellation_16qam +{ +public: + constellation_16qam_impl(); + ~constellation_16qam_impl(); + + unsigned int decision_maker(const gr_complex *sample); +}; + + + + +class constellation_64qam_impl : public constellation_64qam +{ +public: + constellation_64qam_impl(); + ~constellation_64qam_impl(); + + unsigned int decision_maker(const gr_complex *sample); +}; + + +} // namespace ieee802_11 +} // namespace gr + +#endif /* INCLUDED_IEEE802_11_CONSTELLATIONS_IMPL_H */ diff --git a/swig/ieee802_11_swig.i b/swig/ieee802_11_swig.i index 2995854..2eba124 100644 --- a/swig/ieee802_11_swig.i +++ b/swig/ieee802_11_swig.i @@ -25,6 +25,7 @@ #include "gnuradio/digital/packet_header_default.h" #include "ieee802-11/chunks_to_symbols.h" +#include "ieee802-11/constellations.h" #include "ieee802-11/ether_encap.h" #include "ieee802-11/moving_average_cc.h" #include "ieee802-11/moving_average_ff.h" @@ -41,7 +42,13 @@ %include "gnuradio/digital/packet_header_default.h" +%ignore gr::digital::constellation_bpsk; +%ignore gr::digital::constellation_qpsk; +%ignore gr::digital::constellation_16qam; +%include "gnuradio/digital/constellation.h" + %include "ieee802-11/chunks_to_symbols.h" +%include "ieee802-11/constellations.h" %include "ieee802-11/ether_encap.h" %include "ieee802-11/moving_average_cc.h" %include "ieee802-11/moving_average_ff.h" @@ -71,5 +78,29 @@ GR_SWIG_BLOCK_MAGIC2(ieee802_11, sync_short); %template(signal_field_sptr) boost::shared_ptr; %pythoncode %{ signal_field_sptr.__repr__ = lambda self: "" -signal_field = signal_field .make; +signal_field = signal_field.make; +%} + +%template(constellation_bpsk_sptr) boost::shared_ptr; +%pythoncode %{ +constellation_bpsk_sptr.__repr__ = lambda self: "" +constellation_bpsk = constellation_bpsk.make; +%} + +%template(constellation_qpsk_sptr) boost::shared_ptr; +%pythoncode %{ +constellation_qpsk_sptr.__repr__ = lambda self: "" +constellation_qpsk = constellation_qpsk.make; +%} + +%template(constellation_16qam_sptr) boost::shared_ptr; +%pythoncode %{ +constellation_16qam_sptr.__repr__ = lambda self: "" +constellation_16qam = constellation_16qam.make; +%} + +%template(constellation_64qam_sptr) boost::shared_ptr; +%pythoncode %{ +constellation_64qam_sptr.__repr__ = lambda self: "" +constellation_64qam = constellation_64qam.make; %} diff --git a/utils/16-qam.png b/utils/16-qam.png new file mode 100644 index 0000000..ad62b30 Binary files /dev/null and b/utils/16-qam.png differ diff --git a/utils/64-qam.png b/utils/64-qam.png new file mode 100644 index 0000000..2b7a12a Binary files /dev/null and b/utils/64-qam.png differ diff --git a/utils/constellations.py b/utils/constellations.py new file mode 100644 index 0000000..422fae8 --- /dev/null +++ b/utils/constellations.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python + +import sys +import numpy as np +import matplotlib as mpl +import matplotlib.pyplot as plt + +import ieee802_11 + +############################################### +# 16-QAM +############################################### + +c = ieee802_11.constellation_16qam() +a = np.array(c.points()) +p = np.average(np.abs(a)**2) +level = .1**.5 + +print "16-QAM, average power: {0:.4f}".format(p) + +plt.scatter(a.real, a.imag) +for i, x in enumerate(a): + s = "{0:04b}".format(i)[::-1] + plt.text(x.real, x.imag+0.1, s, ha='center') + +plt.show() + +####################### +# test decison maker +####################### + +N = 1000 +data = np.random.randint(0, 16, N) +orig_const = a[data] +noisy_const = orig_const + np.random.sample(N) * 2 * level - level +\ + np.random.sample(N) * 2j * level - level * 1j + +rx = np.array(map(lambda x: c.decision_maker_v([x]), noisy_const)) +rx_const = a[rx] + +if any(rx != data): + print "16-QAM: data does not match." +else: + print "16-QAM: points decoded successfully." + +plt.scatter(a.real, a.imag) +plt.scatter(noisy_const.real, noisy_const.imag, marker='x') +for d, a, b in zip(rx, rx_const, noisy_const): + plt.plot([a.real, b.real], [a.imag, b.imag], color=mpl.cm.hsv(d/16.0)) + +plt.show() + + + +############################################### +# 64-QAM +############################################### + +c = ieee802_11.constellation_64qam() +a = np.array(c.points()) +p = np.average(np.abs(a)**2) +level = (1.0/42)**.5 + +print "64-QAM, average power: {0:.4f}".format(p) + +plt.scatter(a.real, a.imag) +for i, x in enumerate(a): + s = "{0:06b}".format(i)[::-1] + plt.text(x.real, x.imag+0.06, s, ha='center') + +plt.show() + +####################### +# test decison maker +####################### + +N = 1000 +data = np.random.randint(0, 64, N) +orig_const = a[data] +noisy_const = orig_const + np.random.sample(N) * 2 * level - level +\ + np.random.sample(N) * 2j * level - level * 1j + +rx = np.array(map(lambda x: c.decision_maker_v([x]), noisy_const)) +rx_const = a[rx] + +if any(rx != data): + print "16-QAM: data does not match." +else: + print "16-QAM: points decoded successfully." + +plt.scatter(a.real, a.imag) +plt.scatter(noisy_const.real, noisy_const.imag, marker='x') +for d, a, b in zip(rx, rx_const, noisy_const): + plt.plot([a.real, b.real], [a.imag, b.imag], color=mpl.cm.hsv(d/64.0)) + +plt.show()