Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blks2impl / stream_to_vector_decimator.py
1 #
2 # Copyright 2008 Free Software Foundation, Inc.
3 #
4 # This file is part of GNU Radio
5 #
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10 #
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20 #
21
22 from gnuradio import gr
23
24 class stream_to_vector_decimator(gr.hier_block2):
25     """
26     Convert the stream to a vector, decimate the vector stream to achieve the vector rate.
27     """
28
29     def __init__(self, item_size, sample_rate, vec_rate, vec_len):
30         """
31         Create the block chain.
32         @param item_size the number of bytes per sample
33         @param sample_rate the rate of incoming samples
34         @param vec_rate the rate of outgoing vectors (same units as sample_rate)
35         @param vec_len the length of the outgoing vectors in items
36         """
37         self._vec_rate = vec_rate
38         self._vec_len = vec_len
39         self._sample_rate = sample_rate
40
41         gr.hier_block2.__init__(self, "stream_to_vector_decimator",
42                                 gr.io_signature(1, 1, item_size),         # Input signature
43                                 gr.io_signature(1, 1, item_size*vec_len)) # Output signature
44
45         s2v = gr.stream_to_vector(item_size, vec_len)
46         self.one_in_n = gr.keep_one_in_n(item_size*vec_len, 1)
47         self._update_decimator()
48         self.connect(self, s2v, self.one_in_n, self)
49
50     def set_sample_rate(self, sample_rate):
51         """
52         Set the new sampling rate and update the decimator.
53         @param sample_rate the new rate
54         """
55         self._sample_rate = sample_rate
56         self._update_decimator()
57
58     def set_vec_rate(self, vec_rate):
59         """
60         Set the new vector rate and update the decimator.
61         @param vec_rate the new rate
62         """
63         self._vec_rate = vec_rate
64         self._update_decimator()
65
66     def set_decimation(self, decim):
67         """
68         Set the decimation parameter directly.
69         @param decim the new decimation
70         """
71         self._decim = max(1, int(round(decim)))
72         self.one_in_n.set_n(self._decim)
73
74     def _update_decimator(self):
75         self._decim = max(1, int(round(self._sample_rate/self._vec_len/self._vec_rate)))
76         self.one_in_n.set_n(self._decim)
77
78     def decimation(self):
79         """
80         Returns the actual decimation.
81         """
82         return self._decim
83
84     def sample_rate(self):
85         """
86         Returns configured sample rate.
87         """
88         return self._sample_rate
89
90     def frame_rate(self):
91         """
92         Returns actual frame rate
93         """
94         return self._sample_rate/self._vec_len/self._decim