1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2010,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "bin_statistics_f_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <string.h>
30 
31 namespace gr {
32 namespace blocks {
33 
make(unsigned int vlen,msg_queue::sptr msgq,feval_dd * tune,size_t tune_delay,size_t dwell_delay)34 bin_statistics_f::sptr bin_statistics_f::make(unsigned int vlen,
35                                               msg_queue::sptr msgq,
36                                               feval_dd* tune,
37                                               size_t tune_delay,
38                                               size_t dwell_delay)
39 {
40     return gnuradio::get_initial_sptr(
41         new bin_statistics_f_impl(vlen, msgq, tune, tune_delay, dwell_delay));
42 }
43 
bin_statistics_f_impl(unsigned int vlen,msg_queue::sptr msgq,feval_dd * tune,size_t tune_delay,size_t dwell_delay)44 bin_statistics_f_impl::bin_statistics_f_impl(unsigned int vlen,
45                                              msg_queue::sptr msgq,
46                                              feval_dd* tune,
47                                              size_t tune_delay,
48                                              size_t dwell_delay)
49     : sync_block("bin_statistics_f",
50                  io_signature::make(1, 1, sizeof(float) * vlen),
51                  io_signature::make(0, 0, 0)),
52       d_vlen(vlen),
53       d_msgq(msgq),
54       d_tune(tune),
55       d_tune_delay(tune_delay),
56       d_dwell_delay(dwell_delay),
57       d_center_freq(0),
58       d_delay(0),
59       d_max(vlen)
60 {
61     enter_init();
62 }
63 
~bin_statistics_f_impl()64 bin_statistics_f_impl::~bin_statistics_f_impl() {}
65 
enter_init()66 void bin_statistics_f_impl::enter_init()
67 {
68     d_state = ST_INIT;
69     d_delay = 0;
70 }
71 
enter_tune_delay()72 void bin_statistics_f_impl::enter_tune_delay()
73 {
74     d_state = ST_TUNE_DELAY;
75     d_delay = d_tune_delay;
76     d_center_freq = d_tune->calleval(0);
77 }
78 
enter_dwell_delay()79 void bin_statistics_f_impl::enter_dwell_delay()
80 {
81     d_state = ST_DWELL_DELAY;
82     d_delay = d_dwell_delay;
83     reset_stats();
84 }
85 
leave_dwell_delay()86 void bin_statistics_f_impl::leave_dwell_delay() { send_stats(); }
87 
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)88 int bin_statistics_f_impl::work(int noutput_items,
89                                 gr_vector_const_void_star& input_items,
90                                 gr_vector_void_star& output_items)
91 {
92     const float* input = (const float*)input_items[0];
93     size_t vlen = d_max.size();
94 
95     int n = 0;
96     int t;
97 
98     while (n < noutput_items) {
99         switch (d_state) {
100 
101         case ST_INIT:
102             enter_tune_delay();
103             break;
104 
105         case ST_TUNE_DELAY:
106             t = std::min(noutput_items - n, int(d_delay));
107             n += t;
108             d_delay -= t;
109             if (d_delay == 0)
110                 enter_dwell_delay();
111             break;
112 
113         case ST_DWELL_DELAY:
114             t = std::min(noutput_items - n, int(d_delay));
115             for (int i = 0; i < t; i++) {
116                 accrue_stats(&input[n * vlen]);
117                 n++;
118             }
119             d_delay -= t;
120             if (d_delay == 0) {
121                 leave_dwell_delay();
122                 enter_tune_delay();
123             }
124             break;
125 
126         default:
127             assert(0);
128         }
129     }
130 
131     return noutput_items;
132 }
133 
134 //////////////////////////////////////////////////////////////////////////
135 //           virtual methods for gathering stats
136 //////////////////////////////////////////////////////////////////////////
137 
reset_stats()138 void bin_statistics_f_impl::reset_stats()
139 {
140     for (size_t i = 0; i < vlen(); i++) {
141         d_max[i] = 0;
142     }
143 }
144 
accrue_stats(const float * input)145 void bin_statistics_f_impl::accrue_stats(const float* input)
146 {
147     for (size_t i = 0; i < vlen(); i++) {
148         d_max[i] = std::max(d_max[i], input[i]); // compute per bin maxima
149     }
150 }
151 
send_stats()152 void bin_statistics_f_impl::send_stats()
153 {
154     if (msgq()->full_p()) // if the queue is full, don't block, drop the data...
155         return;
156 
157     // build & send a message
158     message::sptr msg = message::make(0, center_freq(), vlen(), vlen() * sizeof(float));
159     memcpy(msg->msg(), &d_max[0], vlen() * sizeof(float));
160     msgq()->insert_tail(msg);
161 }
162 
163 } /* namespace blocks */
164 } /* namespace gr */
165