switch source package format to 3.0 quilt
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / blks2impl / logpwrfft.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, window
23 from stream_to_vector_decimator import stream_to_vector_decimator
24 import math
25
26 class _logpwrfft_base(gr.hier_block2):
27     """
28     Create a log10(abs(fft)) stream chain, with real or complex input.
29     """
30
31     def __init__(self, sample_rate, fft_size, ref_scale, frame_rate, avg_alpha, average, win=None):
32         """
33         Create an log10(abs(fft)) stream chain.
34         Provide access to the setting the filter and sample rate.
35         @param sample_rate        Incoming stream sample rate
36         @param fft_size                Number of FFT bins
37         @param ref_scale        Sets 0 dB value input amplitude
38         @param frame_rate        Output frame rate
39         @param avg_alpha        FFT averaging (over time) constant [0.0-1.0]
40         @param average                Whether to average [True, False]
41         @param win              the window taps generation function
42         """
43         gr.hier_block2.__init__(self, self._name,
44                                 gr.io_signature(1, 1, self._item_size),          # Input signature
45                                 gr.io_signature(1, 1, gr.sizeof_float*fft_size)) # Output signature
46
47         self._sd = stream_to_vector_decimator(item_size=self._item_size,
48                                               sample_rate=sample_rate,
49                                               vec_rate=frame_rate,
50                                               vec_len=fft_size)
51
52         if win is None: win = window.blackmanharris
53         fft_window = win(fft_size)
54         fft = self._fft_block[0](fft_size, True, fft_window)
55         window_power = sum(map(lambda x: x*x, fft_window))
56
57         c2mag = gr.complex_to_mag(fft_size)
58         self._avg = gr.single_pole_iir_filter_ff(1.0, fft_size)
59         self._log = gr.nlog10_ff(20, fft_size,
60                                  -20*math.log10(fft_size)              # Adjust for number of bins
61                                  -10*math.log10(window_power/fft_size) # Adjust for windowing loss
62                                  -20*math.log10(ref_scale/2)+3.0)      # Adjust for reference scale
63         self.connect(self, self._sd, fft, c2mag, self._avg, self._log, self)
64
65         self._average = average
66         self._avg_alpha = avg_alpha
67         self.set_avg_alpha(avg_alpha)
68         self.set_average(average)
69
70     def set_decimation(self, decim):
71         """
72         Set the decimation on stream decimator.
73         @param decim the new decimation
74         """
75         self._sd.set_decimation(decim)
76
77     def set_vec_rate(self, vec_rate):
78         """
79         Set the vector rate on stream decimator.
80         @param vec_rate the new vector rate
81         """
82         self._sd.set_vec_rate(vec_rate)
83
84     def set_sample_rate(self, sample_rate):
85         """
86         Set the new sampling rate
87         @param sample_rate the new rate
88         """
89         self._sd.set_sample_rate(sample_rate)
90
91     def set_average(self, average):
92         """
93         Set the averaging filter on/off.
94         @param average true to set averaging on
95         """
96         self._average = average
97         if self._average:
98             self._avg.set_taps(self._avg_alpha)
99         else:
100             self._avg.set_taps(1.0)
101
102     def set_avg_alpha(self, avg_alpha):
103         """
104         Set the average alpha and set the taps if average was on.
105         @param avg_alpha the new iir filter tap
106         """
107         self._avg_alpha = avg_alpha
108         self.set_average(self._average)
109
110     def sample_rate(self):
111         """
112         Return the current sample rate.
113         """
114         return self._sd.sample_rate()
115
116     def decimation(self):
117         """
118         Return the current decimation.
119         """
120         return self._sd.decimation()
121
122     def frame_rate(self):
123         """
124         Return the current frame rate.
125         """
126         return self._sd.frame_rate()
127
128     def average(self):
129         """
130         Return whether or not averaging is being performed.
131         """
132         return self._average
133
134     def avg_alpha(self):
135         """
136         Return averaging filter constant.
137         """
138         return self._avg_alpha
139
140 class logpwrfft_f(_logpwrfft_base):
141         """
142         Create an fft block chain, with real input.
143         """
144         _name = "logpwrfft_f"
145         _item_size = gr.sizeof_float
146         _fft_block = (gr.fft_vfc, )
147
148 class logpwrfft_c(_logpwrfft_base):
149         """
150         Create an fft block chain, with complex input.
151         """
152         _name = "logpwrfft_c"
153         _item_size = gr.sizeof_gr_complex
154         _fft_block = (gr.fft_vcc, )
155