Re-implemented variable sink so that it can pass vector-aligned samples.
authorjblum <jblum@221aa14e-8319-0410-a670-987f0aec2ac5>
Tue, 19 May 2009 23:56:09 +0000 (23:56 +0000)
committerjblum <jblum@221aa14e-8319-0410-a670-987f0aec2ac5>
Tue, 19 May 2009 23:56:09 +0000 (23:56 +0000)
Created backend grc blks2 block to sample the stream and set callback.
Renamed variable sink and moved it into the sinks category.
Added example of variable sink reading taps from a stream and setting them to a FIR filter.

git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@11062 221aa14e-8319-0410-a670-987f0aec2ac5

grc/data/platforms/python/block_tree.xml
grc/data/platforms/python/blocks/Makefile.am
grc/data/platforms/python/blocks/blks2_variable_sink_x.xml [new file with mode: 0644]
grc/data/platforms/python/blocks/variable_sink.xml [deleted file]
grc/examples/Makefile.am
grc/examples/simple/var_sink_taps.grc [new file with mode: 0644]
grc/src/grc_gnuradio/blks2/Makefile.am
grc/src/grc_gnuradio/blks2/__init__.py
grc/src/grc_gnuradio/blks2/variable_sink.py [new file with mode: 0644]

index 33bca4a6692142797edc987394b3efbb7417e2c2..37b27e569ee822fca38442a7d6b7a093f6bf23cb 100644 (file)
@@ -23,6 +23,7 @@
        </cat>
        <cat>
                <name>Sinks</name>
+               <block>blks2_variable_sink_x</block>
                <block>gr_vector_sink_x</block>
                <block>gr_null_sink</block>
                <block>gr_file_sink</block>
                <block>variable_slider</block>
                <block>variable_chooser</block>
                <block>variable_text_box</block>
-               <block>variable_sink</block>
                <block>parameter</block>
        </cat>
        <cat>
index 131b1d630d76099a35ef546e3e538f945223f8ac..179a5407aee3c846b6819ad331f47ef2e7de85cf 100644 (file)
@@ -53,6 +53,7 @@ dist_ourdata_DATA = \
        blks2_stream_to_vector_decimator.xml \
        blks2_synthesis_filterbank.xml \
        blks2_valve.xml \
+       blks2_variable_sink_x.xml \
        blks2_wfm_rcv.xml \
        blks2_wfm_rcv_pll.xml \
        blks2_wfm_tx.xml \
@@ -205,7 +206,6 @@ dist_ourdata_DATA = \
        usrp_simple_source_x.xml \
        variable.xml \
        variable_chooser.xml \
-       variable_sink.xml \
        variable_slider.xml \
        variable_text_box.xml \
        wxgui_constellationsink2.xml \
diff --git a/grc/data/platforms/python/blocks/blks2_variable_sink_x.xml b/grc/data/platforms/python/blocks/blks2_variable_sink_x.xml
new file mode 100644 (file)
index 0000000..5709c9f
--- /dev/null
@@ -0,0 +1,77 @@
+<?xml version="1.0"?>
+<!--
+###################################################
+##Variable Sink: Custom blks2 block
+###################################################
+ -->
+<block>
+       <name>Variable Sink</name>
+       <key>blks2_variable_sink_x</key>
+       <import>from grc_gnuradio import blks2 as grc_blks2</import>
+       <make>grc_blks2.variable_sink_$(type.fcn)(
+       vlen=$vlen,
+       decim=$decim,
+       callback=self.set_$(variable()),
+)</make>
+       <callback>set_decim($decim)</callback>
+       <param>
+               <name>Type</name>
+               <key>type</key>
+               <type>enum</type>
+               <option>
+                       <name>Complex</name>
+                       <key>complex</key>
+                       <opt>fcn:c</opt>
+               </option>
+               <option>
+                       <name>Float</name>
+                       <key>float</key>
+                       <opt>fcn:f</opt>
+               </option>
+               <option>
+                       <name>Int</name>
+                       <key>int</key>
+                       <opt>fcn:i</opt>
+               </option>
+               <option>
+                       <name>Short</name>
+                       <key>short</key>
+                       <opt>fcn:s</opt>
+               </option>
+               <option>
+                       <name>Byte</name>
+                       <key>byte</key>
+                       <opt>fcn:b</opt>
+               </option>
+       </param>
+       <param>
+               <name>Variable</name>
+               <key>variable</key>
+               <value></value>
+               <type>string</type>
+       </param>
+       <param>
+               <name>Decimation</name>
+               <key>decim</key>
+               <value>1</value>
+               <type>int</type>
+       </param>
+       <param>
+               <name>Vec Length</name>
+               <key>vlen</key>
+               <value>1</value>
+               <type>int</type>
+       </param>
+       <check>$vlen &gt; 0</check>
+       <sink>
+               <name>in</name>
+               <type>$type</type>
+               <vlen>$vlen</vlen>
+       </sink>
+       <doc>
+Read samples from the input stream and \
+write one in every decimation samples to the variable.
+
+The variable must be the id of an existing variable block.
+       </doc>
+</block>
diff --git a/grc/data/platforms/python/blocks/variable_sink.xml b/grc/data/platforms/python/blocks/variable_sink.xml
deleted file mode 100644 (file)
index 426bd34..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-<?xml version="1.0"?>
-<!--
-###################################################
-##Variable Sink: Custom blks2 block
-###################################################
- -->
-<block>
-       <name>Variable Sink</name>
-       <key>variable_sink</key>
-       <import>from gnuradio import gr</import>
-       <import>import threading</import>
-       <import>import time</import>
-       <make>gr.vector_sink_$(type.fcn)()
-def _$(id)_run():
-       while True:
-               time.sleep(1.0/$samp_rate)
-               data = self.$(id).data()
-#if $vlen() == 0
-               if data:
-                       self.set_$(variable())(data[-1])
-                       self.$(id).clear()
-#else
-               if len(data) &gt;= $vlen:
-                       self.set_$(variable())(data[-($vlen):])
-                       self.$(id).clear()
-#end if
-threading.Thread(target=_$(id)_run).start()</make>
-       <param>
-               <name>Type</name>
-               <key>type</key>
-               <type>enum</type>
-               <option>
-                       <name>Complex</name>
-                       <key>complex</key>
-                       <opt>fcn:c</opt>
-               </option>
-               <option>
-                       <name>Float</name>
-                       <key>float</key>
-                       <opt>fcn:f</opt>
-               </option>
-               <option>
-                       <name>Int</name>
-                       <key>int</key>
-                       <opt>fcn:i</opt>
-               </option>
-               <option>
-                       <name>Short</name>
-                       <key>short</key>
-                       <opt>fcn:s</opt>
-               </option>
-               <option>
-                       <name>Byte</name>
-                       <key>byte</key>
-                       <opt>fcn:b</opt>
-               </option>
-       </param>
-       <param>
-               <name>Variable</name>
-               <key>variable</key>
-               <value></value>
-               <type>string</type>
-       </param>
-       <param>
-               <name>Sample Rate</name>
-               <key>samp_rate</key>
-               <value>10</value>
-               <type>real</type>
-       </param>
-       <param>
-               <name>Vec Length</name>
-               <key>vlen</key>
-               <value>0</value>
-               <type>int</type>
-       </param>
-       <check>$vlen &gt;= 0</check>
-       <sink>
-               <name>in</name>
-               <type>$type</type>
-       </sink>
-       <doc>
-Read samples at from the input stream and write each sample to the variable.
-
-The variable must be the id of an existing variable block.
-
-When the vector length is 0, the variable will be set to numbers. \
-When the vector length is > 0, the variable will be set to vectors.
-       </doc>
-</block>
index 9a2fbd53d8a9eb0f718920fdbe1e7840aab40f21..95f352a6461f32421246e4abfa832f6e483d45b2 100644 (file)
@@ -30,7 +30,8 @@ dist_audiodata_DATA = \
 simpledatadir = $(grc_examples_prefix)/simple
 dist_simpledata_DATA = \
        simple/ber_simulation.grc \
-       simple/dpsk_loopback.grc
+       simple/dpsk_loopback.grc \
+       simple/var_sink_taps.grc
 
 trellisdatadir = $(grc_examples_prefix)/trellis
 dist_trellisdata_DATA = \
diff --git a/grc/examples/simple/var_sink_taps.grc b/grc/examples/simple/var_sink_taps.grc
new file mode 100644 (file)
index 0000000..0720785
--- /dev/null
@@ -0,0 +1,488 @@
+<?xml version='1.0' encoding='ASCII'?>
+<flow_graph>
+  <timestamp>Tue May 19 16:45:51 2009</timestamp>
+  <block>
+    <key>options</key>
+    <param>
+      <key>id</key>
+      <value>var_sink_taps</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>title</key>
+      <value>Variable Sink + Taps</value>
+    </param>
+    <param>
+      <key>author</key>
+      <value>Example</value>
+    </param>
+    <param>
+      <key>description</key>
+      <value>gnuradio flow graph</value>
+    </param>
+    <param>
+      <key>window_size</key>
+      <value>1280, 1024</value>
+    </param>
+    <param>
+      <key>generate_options</key>
+      <value>wx_gui</value>
+    </param>
+    <param>
+      <key>category</key>
+      <value>Custom</value>
+    </param>
+    <param>
+      <key>realtime_scheduling</key>
+      <value></value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(10, 10)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>gr_noise_source_x</key>
+    <param>
+      <key>id</key>
+      <value>gr_noise_source_x_0</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>type</key>
+      <value>complex</value>
+    </param>
+    <param>
+      <key>noise_type</key>
+      <value>gr.GR_GAUSSIAN</value>
+    </param>
+    <param>
+      <key>amp</key>
+      <value>1</value>
+    </param>
+    <param>
+      <key>seed</key>
+      <value>42</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(619, 36)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>gr_fir_filter_xxx</key>
+    <param>
+      <key>id</key>
+      <value>gr_fir_filter_xxx_0</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>type</key>
+      <value>ccc</value>
+    </param>
+    <param>
+      <key>decim</key>
+      <value>1</value>
+    </param>
+    <param>
+      <key>taps</key>
+      <value>dest_taps</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(831, 47)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>gr_throttle</key>
+    <param>
+      <key>id</key>
+      <value>gr_throttle_0</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>type</key>
+      <value>complex</value>
+    </param>
+    <param>
+      <key>samples_per_second</key>
+      <value>samp_rate</value>
+    </param>
+    <param>
+      <key>vlen</key>
+      <value>1</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(999, 198)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>270</value>
+    </param>
+  </block>
+  <block>
+    <key>wxgui_fftsink2</key>
+    <param>
+      <key>id</key>
+      <value>wxgui_fftsink2_0</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>type</key>
+      <value>complex</value>
+    </param>
+    <param>
+      <key>title</key>
+      <value>FFT Plot</value>
+    </param>
+    <param>
+      <key>samp_rate</key>
+      <value>samp_rate</value>
+    </param>
+    <param>
+      <key>baseband_freq</key>
+      <value>0</value>
+    </param>
+    <param>
+      <key>y_per_div</key>
+      <value>10</value>
+    </param>
+    <param>
+      <key>y_divs</key>
+      <value>10</value>
+    </param>
+    <param>
+      <key>ref_level</key>
+      <value>50</value>
+    </param>
+    <param>
+      <key>fft_size</key>
+      <value>1024</value>
+    </param>
+    <param>
+      <key>fft_rate</key>
+      <value>30</value>
+    </param>
+    <param>
+      <key>peak_hold</key>
+      <value>False</value>
+    </param>
+    <param>
+      <key>average</key>
+      <value>False</value>
+    </param>
+    <param>
+      <key>avg_alpha</key>
+      <value>0</value>
+    </param>
+    <param>
+      <key>grid_pos</key>
+      <value></value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(629, 184)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>180</value>
+    </param>
+  </block>
+  <block>
+    <key>import</key>
+    <param>
+      <key>id</key>
+      <value>import_0</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>import</key>
+      <value>from gnuradio.gr import firdes</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(330, 120)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>variable</key>
+    <param>
+      <key>id</key>
+      <value>dest_taps</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>value</key>
+      <value>[0]</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(836, 223)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>variable</key>
+    <param>
+      <key>id</key>
+      <value>samp_rate</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>value</key>
+      <value>32000</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(831, 130)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>variable</key>
+    <param>
+      <key>id</key>
+      <value>source_taps</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>value</key>
+      <value>firdes.low_pass(1, samp_rate, 4000, 2000)</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(397, 191)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>variable</key>
+    <param>
+      <key>id</key>
+      <value>taps_rate</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>value</key>
+      <value>10</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(268, 189)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>gr_vector_source_x</key>
+    <param>
+      <key>id</key>
+      <value>gr_vector_source_x_0</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>type</key>
+      <value>complex</value>
+    </param>
+    <param>
+      <key>vector</key>
+      <value>source_taps</value>
+    </param>
+    <param>
+      <key>repeat</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>vlen</key>
+      <value>len(source_taps)</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(205, 33)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>note</key>
+    <param>
+      <key>id</key>
+      <value>note_0</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>note</key>
+      <value>Pass the FIR taps via a variable sink.</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(14, 141)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>gr_throttle</key>
+    <param>
+      <key>id</key>
+      <value>gr_throttle</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>type</key>
+      <value>complex</value>
+    </param>
+    <param>
+      <key>samples_per_second</key>
+      <value>taps_rate</value>
+    </param>
+    <param>
+      <key>vlen</key>
+      <value>len(source_taps)</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(440, 41)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>0</value>
+    </param>
+  </block>
+  <block>
+    <key>blks2_variable_sink_x</key>
+    <param>
+      <key>id</key>
+      <value>blks2_variable_sink_x_0</value>
+    </param>
+    <param>
+      <key>_enabled</key>
+      <value>True</value>
+    </param>
+    <param>
+      <key>type</key>
+      <value>complex</value>
+    </param>
+    <param>
+      <key>variable</key>
+      <value>dest_taps</value>
+    </param>
+    <param>
+      <key>decim</key>
+      <value>1</value>
+    </param>
+    <param>
+      <key>vlen</key>
+      <value>len(source_taps)</value>
+    </param>
+    <param>
+      <key>_coordinate</key>
+      <value>(305, 283)</value>
+    </param>
+    <param>
+      <key>_rotation</key>
+      <value>180</value>
+    </param>
+  </block>
+  <connection>
+    <source_block_id>gr_noise_source_x_0</source_block_id>
+    <sink_block_id>gr_fir_filter_xxx_0</sink_block_id>
+    <source_key>0</source_key>
+    <sink_key>0</sink_key>
+  </connection>
+  <connection>
+    <source_block_id>gr_fir_filter_xxx_0</source_block_id>
+    <sink_block_id>gr_throttle_0</sink_block_id>
+    <source_key>0</source_key>
+    <sink_key>0</sink_key>
+  </connection>
+  <connection>
+    <source_block_id>gr_throttle_0</source_block_id>
+    <sink_block_id>wxgui_fftsink2_0</sink_block_id>
+    <source_key>0</source_key>
+    <sink_key>0</sink_key>
+  </connection>
+  <connection>
+    <source_block_id>gr_vector_source_x_0</source_block_id>
+    <sink_block_id>gr_throttle</sink_block_id>
+    <source_key>0</source_key>
+    <sink_key>0</sink_key>
+  </connection>
+  <connection>
+    <source_block_id>gr_throttle</source_block_id>
+    <sink_block_id>blks2_variable_sink_x_0</sink_block_id>
+    <source_key>0</source_key>
+    <sink_key>0</sink_key>
+  </connection>
+</flow_graph>
index 396fc5f9d56209e50018c526c7bd100227d42132..7db1d5c8c15804787c64249802f41a37454b90ef 100644 (file)
@@ -1,5 +1,5 @@
 #
-# Copyright 2008 Free Software Foundation, Inc.
+# Copyright 2008, 2009 Free Software Foundation, Inc.
 # 
 # This file is part of GNU Radio
 # 
@@ -28,4 +28,5 @@ ourpython_PYTHON = \
        error_rate.py \
        packet.py \
        probe.py \
-       selector.py
+       selector.py \
+       variable_sink.py
index 185230ab002d41a7a07161c553e1b24740d95e6c..a28498a33496335a40bda6804b87e78b6a772a05 100644 (file)
@@ -24,3 +24,4 @@ from packet import options, packet_encoder, packet_decoder, \
        packet_demod_b, packet_demod_s, packet_demod_i, packet_demod_f, packet_demod_c
 from error_rate import error_rate
 from probe import probe_function, probe_avg_mag_sqrd_c, probe_avg_mag_sqrd_f, probe_density_b, probe_mpsk_snr_c
+from variable_sink import variable_sink_b, variable_sink_s, variable_sink_i, variable_sink_f, variable_sink_c
diff --git a/grc/src/grc_gnuradio/blks2/variable_sink.py b/grc/src/grc_gnuradio/blks2/variable_sink.py
new file mode 100644 (file)
index 0000000..cad3b8b
--- /dev/null
@@ -0,0 +1,64 @@
+#
+# Copyright 2009 Free Software Foundation, Inc.
+#
+# This file is part of GNU Radio
+#
+# GNU Radio 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, or (at your option)
+# any later version.
+#
+# GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+
+from gnuradio import gr
+import threading
+import numpy
+
+class _variable_sink_base(gr.hier_block2, threading.Thread):
+       """
+       The thread polls the message queue for values and writes to a callback.
+       """
+
+       def __init__(self, vlen, decim, callback):
+               self._vlen = vlen
+               self._callback = callback
+               self._item_size = self._size*self._vlen
+               #init hier block
+               gr.hier_block2.__init__(
+                       self, 'variable_sink',
+                       gr.io_signature(1, 1, self._item_size),
+                       gr.io_signature(0, 0, 0),
+               )
+               #create blocks
+               self._decimator = gr.keep_one_in_n(self._item_size, decim)
+               self._msgq = gr.msg_queue(2)
+               message_sink = gr.message_sink(self._item_size, self._msgq, False)
+               #connect
+               self.connect(self, self._decimator, message_sink)
+               #setup thread
+               threading.Thread.__init__(self)
+               self.setDaemon(True)
+               self.start() 
+
+       def set_decim(self, decim): self._decimator.set_n(decim)
+
+       def run(self):
+               while True: #truncate to item size, convert to array, callback
+                       msg = self._msgq.delete_head().to_string()[-self._item_size:]
+                       arr = map(self._cast, numpy.fromstring(msg, self._numpy))
+                       self._callback(self._vlen > 1 and arr or arr[0])
+
+class variable_sink_b(_variable_sink_base): _numpy, _size, _cast = numpy.int8, gr.sizeof_char, int
+class variable_sink_s(_variable_sink_base): _numpy, _size, _cast = numpy.int16, gr.sizeof_short, int
+class variable_sink_i(_variable_sink_base): _numpy, _size, _cast = numpy.int32, gr.sizeof_int, int
+class variable_sink_f(_variable_sink_base): _numpy, _size, _cast = numpy.float32, gr.sizeof_float, float
+class variable_sink_c(_variable_sink_base): _numpy, _size, _cast = numpy.complex64, gr.sizeof_gr_complex, complex