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