2 # Copyright 2008 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
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)
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.
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.
22 from gnuradio import gr
24 class stream_to_vector_decimator(gr.hier_block2):
26 Convert the stream to a vector, decimate the vector stream to achieve the vector rate.
29 def __init__(self, item_size, sample_rate, vec_rate, vec_len):
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
37 self._vec_rate = vec_rate
38 self._vec_len = vec_len
39 self._sample_rate = sample_rate
41 gr.hier_block2.__init__(self, "stream_decimator_block",
42 gr.io_signature(1, 1, item_size), # Input signature
43 gr.io_signature(1, 1, item_size*vec_len)) # Output signature
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)
50 def set_sample_rate(self, sample_rate):
52 Set the new sampling rate and update the decimator.
53 @param sample_rate the new rate
55 self._sample_rate = sample_rate
56 self._update_decimator()
58 def set_vec_rate(self, vec_rate):
60 Set the new vector rate and update the decimator.
61 @param vec_rate the new rate
63 self._vec_rate = vec_rate
64 self._update_decimator()
66 def _update_decimator(self):
67 self._decim = max(1, int(self._sample_rate/self._vec_len/self._vec_rate))
68 self.one_in_n.set_n(self._decim)
70 def sample_rate(self):
72 Returns configured sample rate.
74 return self._sample_rate
78 Returns actual frame rate
80 return self._sample_rate/self._vec_len/self._decim