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