added gr.complex_to_mag_squared block
authoreb <eb@221aa14e-8319-0410-a670-987f0aec2ac5>
Sun, 10 Sep 2006 19:21:33 +0000 (19:21 +0000)
committereb <eb@221aa14e-8319-0410-a670-987f0aec2ac5>
Sun, 10 Sep 2006 19:21:33 +0000 (19:21 +0000)
git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@3513 221aa14e-8319-0410-a670-987f0aec2ac5

gnuradio-core/src/lib/general/gr_complex_to_xxx.cc
gnuradio-core/src/lib/general/gr_complex_to_xxx.h
gnuradio-core/src/lib/general/gr_complex_to_xxx.i
gnuradio-core/src/python/gnuradio/gr/qa_complex_to_xxx.py

index 727f44f073a1888c407cfd6f7785117f3764050e..222357cd0d977f4c6fe1fb37588f8de9a168dfbe 100644 (file)
@@ -169,6 +169,39 @@ gr_complex_to_mag::work (int noutput_items,
 
 // ----------------------------------------------------------------
 
+gr_complex_to_mag_squared_sptr
+gr_make_complex_to_mag_squared (unsigned int vlen)
+{
+  return gr_complex_to_mag_squared_sptr (new gr_complex_to_mag_squared (vlen));
+}
+
+gr_complex_to_mag_squared::gr_complex_to_mag_squared (unsigned int vlen)
+  : gr_sync_block ("complex_to_mag_squared",
+                  gr_make_io_signature (1, 1, sizeof (gr_complex) * vlen),
+                  gr_make_io_signature (1, 1, sizeof (float) * vlen)),
+    d_vlen(vlen)
+{
+}
+
+int
+gr_complex_to_mag_squared::work (int noutput_items,
+                                gr_vector_const_void_star &input_items,
+                                gr_vector_void_star &output_items)
+{
+  const gr_complex *in = (const gr_complex *) input_items[0];
+  float *out = (float *) output_items[0];
+  int noi = noutput_items * d_vlen;
+
+  for (int i = 0; i < noi; i++){
+    const float __x = in[i].real();
+    const float __y = in[i].imag();
+    out[i] = __x * __x + __y * __y;
+  }
+  return noutput_items;
+}
+
+// ----------------------------------------------------------------
+
 gr_complex_to_arg_sptr
 gr_make_complex_to_arg (unsigned int vlen)
 {
index 02a9fc8fd9b81eeab1f45ea64d5ec414bb52ce00..d9acdb8abb5b2717bb4dfcd40c1897fa14a2b8f9 100644 (file)
@@ -30,18 +30,21 @@ class gr_complex_to_float;
 class gr_complex_to_real;
 class gr_complex_to_imag;
 class gr_complex_to_mag;
+class gr_complex_to_mag_squared;
 class gr_complex_to_arg;
 
 typedef boost::shared_ptr<gr_complex_to_float> gr_complex_to_float_sptr;
 typedef boost::shared_ptr<gr_complex_to_real> gr_complex_to_real_sptr;
 typedef boost::shared_ptr<gr_complex_to_imag> gr_complex_to_imag_sptr;
 typedef boost::shared_ptr<gr_complex_to_mag> gr_complex_to_mag_sptr;
+typedef boost::shared_ptr<gr_complex_to_mag_squared> gr_complex_to_mag_squared_sptr;
 typedef boost::shared_ptr<gr_complex_to_arg> gr_complex_to_arg_sptr;
 
 gr_complex_to_float_sptr gr_make_complex_to_float (unsigned int vlen=1);
 gr_complex_to_real_sptr gr_make_complex_to_real (unsigned int vlen=1);
 gr_complex_to_imag_sptr gr_make_complex_to_imag (unsigned int vlen=1);
 gr_complex_to_mag_sptr gr_make_complex_to_mag (unsigned int vlen=1);
+gr_complex_to_mag_squared_sptr gr_make_complex_to_mag_squared (unsigned int vlen=1);
 gr_complex_to_arg_sptr gr_make_complex_to_arg (unsigned int vlen=1);
 
 /*!
@@ -116,6 +119,24 @@ class gr_complex_to_mag : public gr_sync_block
                    gr_vector_void_star &output_items);
 };
 
+/*!
+ * \brief complex in, magnitude squared out (float)
+ * \ingroup converter
+ * \param vlen vector len (default 1)
+ */
+class gr_complex_to_mag_squared : public gr_sync_block
+{
+  friend gr_complex_to_mag_squared_sptr gr_make_complex_to_mag_squared (unsigned int vlen);
+  gr_complex_to_mag_squared (unsigned int vlen);
+
+  unsigned int d_vlen;
+
+ public:
+  virtual int work (int noutput_items,
+                   gr_vector_const_void_star &input_items,
+                   gr_vector_void_star &output_items);
+};
+
 /*!
  * \brief complex in, angle out (float) 
  * \ingroup converter
index 06f1020a02ab735c58d83d3f1b236e5f7bb2bc60..e6d00cc8bc11cc623ea57c44d94ef3b992c375a7 100644 (file)
@@ -39,8 +39,8 @@ gr_complex_to_imag_sptr gr_make_complex_to_imag (unsigned int vlen=1);
 class gr_complex_to_imag : public gr_sync_block
 {
   gr_complex_to_imag (unsigned int vlen);
-}
-  ;
+};
+
 GR_SWIG_BLOCK_MAGIC(gr,complex_to_mag);
 gr_complex_to_mag_sptr gr_make_complex_to_mag (unsigned int vlen=1);
 class gr_complex_to_mag : public gr_sync_block
@@ -48,6 +48,13 @@ class gr_complex_to_mag : public gr_sync_block
   gr_complex_to_mag (unsigned int vlen);
 };
 
+GR_SWIG_BLOCK_MAGIC(gr,complex_to_mag_squared);
+gr_complex_to_mag_squared_sptr gr_make_complex_to_mag_squared (unsigned int vlen=1);
+class gr_complex_to_mag_squared : public gr_sync_block
+{
+  gr_complex_to_mag_squared (unsigned int vlen);
+};
+
 GR_SWIG_BLOCK_MAGIC(gr,complex_to_arg);
 gr_complex_to_arg_sptr gr_make_complex_to_arg (unsigned int vlen=1);
 class gr_complex_to_arg : public gr_sync_block
index 4bc1933500578508d0309d0e861726a8be4adf3a..f92cf82b60650ae019ff6a3f5eccfa85914ec9a4 100755 (executable)
@@ -96,6 +96,18 @@ class test_complex_ops (gr_unittest.TestCase):
         actual_result = dst.data ()
         self.assertFloatTuplesAlmostEqual (expected_result, actual_result,5)
 
+    def test_complex_to_mag_squared (self):
+        src_data = (0, 1, -1, 3+4j, -3-4j, -3+4j)
+        expected_result = (0, 1, 1, 25, 25, 25)
+        src = gr.vector_source_c (src_data)
+        op = gr.complex_to_mag_squared ()
+        dst = gr.vector_sink_f ()
+        self.fg.connect (src, op)
+        self.fg.connect (op, dst)
+        self.fg.run ()
+        actual_result = dst.data ()
+        self.assertFloatTuplesAlmostEqual (expected_result, actual_result,5)
+
     def test_complex_to_arg (self):
         pi = math.pi
         expected_result = (0, pi/6, pi/4, pi/2, 3*pi/4, 7*pi/8,