Statistics
| Branch: | Tag: | Revision:

root / gnuradio-core / src / lib / general / gr_peak_detector2_fb.cc @ 9b2855a4

History | View | Annotate | Download (3 kB)

1 ce165145 trondeau
/* -*- c++ -*- */
2 ce165145 trondeau
/*
3 0a9b999b Eric Blossom
 * Copyright 2007,2010 Free Software Foundation, Inc.
4 f919f9dc Tom Rondeau
 *
5 ce165145 trondeau
 * This file is part of GNU Radio
6 f919f9dc Tom Rondeau
 *
7 ce165145 trondeau
 * GNU Radio is free software; you can redistribute it and/or modify
8 ce165145 trondeau
 * it under the terms of the GNU General Public License as published by
9 ce165145 trondeau
 * the Free Software Foundation; either version 3, or (at your option)
10 ce165145 trondeau
 * any later version.
11 f919f9dc Tom Rondeau
 *
12 ce165145 trondeau
 * GNU Radio is distributed in the hope that it will be useful,
13 ce165145 trondeau
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ce165145 trondeau
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ce165145 trondeau
 * GNU General Public License for more details.
16 f919f9dc Tom Rondeau
 *
17 ce165145 trondeau
 * You should have received a copy of the GNU General Public License
18 ce165145 trondeau
 * along with GNU Radio; see the file COPYING.  If not, write to
19 ce165145 trondeau
 * the Free Software Foundation, Inc., 51 Franklin Street,
20 ce165145 trondeau
 * Boston, MA 02110-1301, USA.
21 ce165145 trondeau
 */
22 ce165145 trondeau
23 ce165145 trondeau
#ifdef HAVE_CONFIG_H
24 ce165145 trondeau
#include "config.h"
25 ce165145 trondeau
#endif
26 ce165145 trondeau
27 ce165145 trondeau
#include <gr_peak_detector2_fb.h>
28 ce165145 trondeau
#include <gr_io_signature.h>
29 38ea3a57 eb
#include <string.h>
30 ce165145 trondeau
31 ce165145 trondeau
gr_peak_detector2_fb_sptr
32 ce165145 trondeau
gr_make_peak_detector2_fb (float threshold_factor_rise,
33 ce165145 trondeau
                           int look_ahead, float alpha)
34 ce165145 trondeau
{
35 f919f9dc Tom Rondeau
  return gnuradio::get_initial_sptr(new gr_peak_detector2_fb (threshold_factor_rise,
36 ce165145 trondeau
                                  look_ahead, alpha));
37 ce165145 trondeau
}
38 ce165145 trondeau
39 f919f9dc Tom Rondeau
gr_peak_detector2_fb::gr_peak_detector2_fb (float threshold_factor_rise,
40 ce165145 trondeau
                                            int look_ahead, float alpha)
41 ce165145 trondeau
  : gr_sync_block ("peak_detector2_fb",
42 ce165145 trondeau
                   gr_make_io_signature (1, 1, sizeof(float)),
43 ce165145 trondeau
                   gr_make_io_signature2 (1, 2, sizeof(char), sizeof(float))),
44 f919f9dc Tom Rondeau
    d_threshold_factor_rise(threshold_factor_rise),
45 ce165145 trondeau
    d_look_ahead(look_ahead), d_alpha(alpha), d_avg(0.0f), d_found(false)
46 ce165145 trondeau
{
47 ce165145 trondeau
}
48 ce165145 trondeau
49 ce165145 trondeau
int
50 ce165145 trondeau
gr_peak_detector2_fb::work (int noutput_items,
51 ce165145 trondeau
                            gr_vector_const_void_star &input_items,
52 ce165145 trondeau
                            gr_vector_void_star &output_items) {
53 ce165145 trondeau
  float *iptr = (float *) input_items[0];
54 ce165145 trondeau
  char *optr = (char *) output_items[0];
55 f919f9dc Tom Rondeau
56 ce165145 trondeau
  assert(noutput_items >= 2);
57 ce165145 trondeau
58 ce165145 trondeau
  memset(optr, 0, noutput_items*sizeof(char));
59 ce165145 trondeau
60 ce165145 trondeau
  for (int i = 0; i < noutput_items; i++) {
61 ce165145 trondeau
62 ce165145 trondeau
    if (!d_found) {
63 ce165145 trondeau
      // Have not yet detected presence of peak
64 ce165145 trondeau
      if (iptr[i] > d_avg * (1.0f + d_threshold_factor_rise)) {
65 ce165145 trondeau
        d_found = true;
66 ce165145 trondeau
        d_look_ahead_remaining = d_look_ahead;
67 ce165145 trondeau
        d_peak_val = -(float)INFINITY;
68 f919f9dc Tom Rondeau
      }
69 ce165145 trondeau
      else {
70 ce165145 trondeau
        d_avg = d_alpha*iptr[i] + (1.0f - d_alpha)*d_avg;
71 ce165145 trondeau
      }
72 f919f9dc Tom Rondeau
    }
73 ce165145 trondeau
    else {
74 ce165145 trondeau
      // Detected presence of peak
75 ce165145 trondeau
      if (iptr[i] > d_peak_val) {
76 ce165145 trondeau
        d_peak_val = iptr[i];
77 ce165145 trondeau
        d_peak_ind = i;
78 f919f9dc Tom Rondeau
      }
79 ce165145 trondeau
      else if (d_look_ahead_remaining <= 0) {
80 ce165145 trondeau
        optr[d_peak_ind] = 1;
81 ce165145 trondeau
        d_found = false;
82 ce165145 trondeau
        d_avg = iptr[i];
83 ce165145 trondeau
      }
84 f919f9dc Tom Rondeau
85 ce165145 trondeau
      // Have not yet located peak, loop and keep searching.
86 ce165145 trondeau
      d_look_ahead_remaining--;
87 ce165145 trondeau
    }
88 f919f9dc Tom Rondeau
89 ce165145 trondeau
    // Every iteration of the loop, write debugging signal out if
90 ce165145 trondeau
    // connected:
91 ce165145 trondeau
    if (output_items.size() == 2) {
92 ce165145 trondeau
      float *sigout = (float *) output_items[1];
93 ce165145 trondeau
      sigout[i] = d_avg;
94 ce165145 trondeau
    }
95 ce165145 trondeau
  } // loop
96 f919f9dc Tom Rondeau
97 f919f9dc Tom Rondeau
  if (!d_found)
98 ce165145 trondeau
    return noutput_items;
99 f919f9dc Tom Rondeau
100 ce165145 trondeau
  // else if detected presence, keep searching during the next call to work.
101 ce165145 trondeau
  int tmp = d_peak_ind;
102 ce165145 trondeau
  d_peak_ind = 1;
103 f919f9dc Tom Rondeau
104 f919f9dc Tom Rondeau
  return tmp - 1;
105 ce165145 trondeau
}
106 ce165145 trondeau