3 * Copyright 2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
30 #include <gr_io_signature.h>
33 gr_make_@BASE_NAME@ (float threshold_factor_rise,
34 float threshold_factor_fall,
35 int look_ahead, float alpha)
37 return @SPTR_NAME@ (new @NAME@ (threshold_factor_rise,
38 threshold_factor_fall,
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 (char))),
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)
55 @NAME@::work (int noutput_items,
56 gr_vector_const_void_star &input_items,
57 gr_vector_void_star &output_items)
59 @I_TYPE@ *iptr = (@I_TYPE@ *) input_items[0];
60 char *optr = (char *) output_items[0];
62 memset(optr, 0, noutput_items*sizeof(char));
64 @I_TYPE@ peak_val = -(@I_TYPE@)INFINITY;
66 unsigned char state = 0;
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) {
76 d_avg = (d_avg_alpha)*iptr[i] + (1-d_avg_alpha)*d_avg;
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) {
85 d_avg = (d_avg_alpha)*iptr[i] + (1-d_avg_alpha)*d_avg;
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;
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);
103 //printf("Leave in State 0, produced %d\n",noutput_items);
104 return noutput_items;
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);