Merged r4518:5130 from developer branch n4hy/ofdm into trunk, passes distcheck.
[debian/gnuradio] / gnuradio-core / src / lib / gengen / gr_peak_detector_XX.cc.t
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 2, 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 // @WARNING@
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <@NAME@.h>
30 #include <gr_io_signature.h>
31
32 @SPTR_NAME@
33 gr_make_@BASE_NAME@ (float threshold_factor_rise,
34                      float threshold_factor_fall,
35                      int look_ahead, float alpha)
36 {
37   return @SPTR_NAME@ (new @NAME@ (threshold_factor_rise, 
38                                   threshold_factor_fall,
39                                   look_ahead, alpha));
40 }
41
42 @NAME@::@NAME@ (float threshold_factor_rise, 
43                 float threshold_factor_fall,
44                 int look_ahead, float alpha)
45   : gr_sync_block ("@BASE_NAME@",
46                    gr_make_io_signature (1, 1, sizeof (@I_TYPE@)),
47                    gr_make_io_signature (1, 1, sizeof (@O_TYPE@))),
48     d_threshold_factor_rise(threshold_factor_rise), 
49     d_threshold_factor_fall(threshold_factor_fall),
50     d_look_ahead(look_ahead), d_avg_alpha(alpha), d_avg(0), d_found(0)
51 {
52 }
53
54 int
55 @NAME@::work (int noutput_items,
56               gr_vector_const_void_star &input_items,
57               gr_vector_void_star &output_items)
58 {
59   @I_TYPE@ *iptr = (@I_TYPE@ *) input_items[0];
60   @O_TYPE@ *optr = (@O_TYPE@ *) output_items[0];
61
62   memset(optr, 0, noutput_items*sizeof(@O_TYPE@));
63
64   @I_TYPE@ peak_val = -(@I_TYPE@)INFINITY;
65   int peak_ind = 0;
66   unsigned char state = 0;
67   int i = 0;
68
69   //printf("noutput_items %d\n",noutput_items);
70   while(i < noutput_items) {
71     if(state == 0) {  // below threshold
72       if(iptr[i] > d_avg*d_threshold_factor_rise) {
73         state = 1;
74       }
75       else {
76         d_avg = (d_avg_alpha)*iptr[i] + (1-d_avg_alpha)*d_avg;
77         i++;
78       }
79     }
80     else if(state == 1) {  // above threshold, have not found peak
81       //printf("Entered State 1: %f  i: %d  noutput_items: %d\n", iptr[i], i, noutput_items);
82       if(iptr[i] > peak_val) {
83         peak_val = iptr[i];
84         peak_ind = i;
85         d_avg = (d_avg_alpha)*iptr[i] + (1-d_avg_alpha)*d_avg;
86         i++;
87       }
88       else if (iptr[i] > d_avg*d_threshold_factor_fall) {
89         d_avg = (d_avg_alpha)*iptr[i] + (1-d_avg_alpha)*d_avg;
90         i++;
91       }
92       else {
93         optr[peak_ind] = (@O_TYPE@)1;
94         state = 0;
95         peak_val = -(@I_TYPE@)INFINITY;
96         //printf("Leaving  State 1: Peak: %f  Peak Ind: %d   i: %d  noutput_items: %d\n", 
97         //peak_val, peak_ind, i, noutput_items);
98       }
99     }
100   }
101
102   if(state == 0) {
103     //printf("Leave in State 0, produced %d\n",noutput_items);
104     return noutput_items;
105   }
106   else {   // only return up to passing the threshold
107     //printf("Leave in State 1, only produced %d of %d\n",peak_ind,noutput_items);
108     return peak_ind;
109   }
110 }