Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_peak_detector2_fb.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 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 <gr_peak_detector2_fb.h>
28 #include <gr_io_signature.h>
29 #include <string.h>
30
31 gr_peak_detector2_fb_sptr
32 gr_make_peak_detector2_fb (float threshold_factor_rise,
33                            int look_ahead, float alpha)
34 {
35   return gr_peak_detector2_fb_sptr (new gr_peak_detector2_fb (threshold_factor_rise, 
36                                   look_ahead, alpha));
37 }
38
39 gr_peak_detector2_fb::gr_peak_detector2_fb (float threshold_factor_rise, 
40                                             int look_ahead, float alpha)
41   : gr_sync_block ("peak_detector2_fb",
42                    gr_make_io_signature (1, 1, sizeof(float)),
43                    gr_make_io_signature2 (1, 2, sizeof(char), sizeof(float))),
44     d_threshold_factor_rise(threshold_factor_rise), 
45     d_look_ahead(look_ahead), d_alpha(alpha), d_avg(0.0f), d_found(false)
46 {
47 }
48
49 int
50 gr_peak_detector2_fb::work (int noutput_items,
51                             gr_vector_const_void_star &input_items,
52                             gr_vector_void_star &output_items) {
53   float *iptr = (float *) input_items[0];
54   char *optr = (char *) output_items[0];
55   
56   assert(noutput_items >= 2);
57
58   memset(optr, 0, noutput_items*sizeof(char));
59
60   for (int i = 0; i < noutput_items; i++) {
61
62     if (!d_found) {
63       // Have not yet detected presence of peak
64       if (iptr[i] > d_avg * (1.0f + d_threshold_factor_rise)) {
65         d_found = true;
66         d_look_ahead_remaining = d_look_ahead;
67         d_peak_val = -(float)INFINITY;
68       } 
69       else {
70         d_avg = d_alpha*iptr[i] + (1.0f - d_alpha)*d_avg;
71       }
72     } 
73     else {
74       // Detected presence of peak
75       if (iptr[i] > d_peak_val) {
76         d_peak_val = iptr[i];
77         d_peak_ind = i;
78       } 
79       else if (d_look_ahead_remaining <= 0) {
80         optr[d_peak_ind] = 1;
81         d_found = false;
82         d_avg = iptr[i];
83       }
84       
85       // Have not yet located peak, loop and keep searching.
86       d_look_ahead_remaining--;
87     }
88     
89     // Every iteration of the loop, write debugging signal out if
90     // connected:
91     if (output_items.size() == 2) {
92       float *sigout = (float *) output_items[1];
93       sigout[i] = d_avg;
94     }
95   } // loop
96   
97   if (!d_found) 
98     return noutput_items;
99   
100   // else if detected presence, keep searching during the next call to work.
101   int tmp = d_peak_ind;
102   d_peak_ind = 1;
103   
104   return tmp - 1; 
105 }
106
107