1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012-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 "plateau_detector_fb_impl.h"
28 #include <gnuradio/io_signature.h>
29 
30 namespace gr {
31 namespace blocks {
32 
make(int max_len,float threshold)33 plateau_detector_fb::sptr plateau_detector_fb::make(int max_len, float threshold)
34 {
35     return gnuradio::get_initial_sptr(new plateau_detector_fb_impl(max_len, threshold));
36 }
37 
plateau_detector_fb_impl(int max_len,float threshold)38 plateau_detector_fb_impl::plateau_detector_fb_impl(int max_len, float threshold)
39     : block("plateau_detector_fb",
40             io_signature::make(1, 1, sizeof(float)),
41             io_signature::make(1, 1, sizeof(char))),
42       d_max_len(max_len),
43       d_threshold(threshold)
44 {
45 }
46 
~plateau_detector_fb_impl()47 plateau_detector_fb_impl::~plateau_detector_fb_impl() {}
48 
forecast(int,gr_vector_int & ninput_items_required)49 void plateau_detector_fb_impl::forecast(int, gr_vector_int& ninput_items_required)
50 {
51     ninput_items_required[0] = 2 * d_max_len;
52 }
53 
general_work(int noutput_items,gr_vector_int & ninput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)54 int plateau_detector_fb_impl::general_work(int noutput_items,
55                                            gr_vector_int& ninput_items,
56                                            gr_vector_const_void_star& input_items,
57                                            gr_vector_void_star& output_items)
58 {
59     // thread-safe protection from ::set_threshold
60     gr::thread::scoped_lock l(d_setlock);
61 
62     const float* in = (const float*)input_items[0];
63     unsigned char* out = (unsigned char*)output_items[0];
64     int flank_start;
65     noutput_items = std::min(noutput_items, ninput_items[0]);
66     memset((void*)out, 0x00, noutput_items);
67     int i;
68     for (i = 0; i < noutput_items; i++) {
69         if (in[i] >= d_threshold) {
70             if (noutput_items - i <
71                 2 * d_max_len) { // If we can't finish, come back later
72                 break;
73             }
74             flank_start = i;
75             while (i < noutput_items && in[i] >= d_threshold)
76                 i++;
77             if ((i - flank_start) > 1) { // 1 Sample is not a plateau
78                 out[flank_start + (i - flank_start) / 2] = 1;
79                 i = std::min(i + d_max_len, noutput_items - 1);
80             }
81         }
82     }
83 
84     this->consume_each(i);
85     return i;
86 }
87 
set_threshold(float threshold)88 void plateau_detector_fb_impl::set_threshold(float threshold)
89 {
90     // thread-safe protection from ::set_threshold
91     gr::thread::scoped_lock l(d_setlock);
92     d_threshold = threshold;
93 }
94 
threshold() const95 float plateau_detector_fb_impl::threshold() const { return d_threshold; }
96 
97 } /* namespace blocks */
98 } /* namespace gr */
99