summaryrefslogtreecommitdiff
path: root/gr-blocks/lib/peak_detector2_fb_impl.cc
diff options
context:
space:
mode:
Diffstat (limited to 'gr-blocks/lib/peak_detector2_fb_impl.cc')
-rw-r--r--gr-blocks/lib/peak_detector2_fb_impl.cc83
1 files changed, 53 insertions, 30 deletions
diff --git a/gr-blocks/lib/peak_detector2_fb_impl.cc b/gr-blocks/lib/peak_detector2_fb_impl.cc
index e631768e55..dc13e66dbe 100644
--- a/gr-blocks/lib/peak_detector2_fb_impl.cc
+++ b/gr-blocks/lib/peak_detector2_fb_impl.cc
@@ -1,6 +1,6 @@
/* -*- c++ -*- */
/*
- * Copyright 2007,2010,2013 Free Software Foundation, Inc.
+ * Copyright 2007,2010,2013,2015 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
@@ -43,17 +43,48 @@ namespace gr {
peak_detector2_fb_impl::peak_detector2_fb_impl(float threshold_factor_rise,
int look_ahead, float alpha)
: sync_block("peak_detector2_fb",
- io_signature::make(1, 1, sizeof(float)),
- io_signature::make2(1, 2, sizeof(char), sizeof(float))),
- d_threshold_factor_rise(threshold_factor_rise),
- d_look_ahead(look_ahead), d_alpha(alpha), d_avg(0.0f), d_found(false)
+ io_signature::make(1, 1, sizeof(float)),
+ io_signature::make2(1, 2, sizeof(char), sizeof(float))),
+ d_avg(0.0f), d_found(false)
{
+ set_threshold_factor_rise(threshold_factor_rise);
+ set_look_ahead(look_ahead);
+ set_alpha(alpha);
}
peak_detector2_fb_impl::~peak_detector2_fb_impl()
{
}
+ void
+ peak_detector2_fb_impl::set_threshold_factor_rise(float thr)
+ {
+ gr::thread::scoped_lock lock(d_setlock);
+ d_threshold_factor_rise = thr;
+ invalidate();
+ }
+
+ void
+ peak_detector2_fb_impl::set_look_ahead(int look)
+ {
+ gr::thread::scoped_lock lock(d_setlock);
+ d_look_ahead = look;
+ invalidate();
+ }
+
+ void
+ peak_detector2_fb_impl::set_alpha(float alpha)
+ {
+ d_alpha = alpha;
+ }
+
+ void
+ peak_detector2_fb_impl::invalidate()
+ {
+ d_found = false;
+ set_output_multiple(1);
+ }
+
int
peak_detector2_fb_impl::work(int noutput_items,
gr_vector_const_void_star &input_items,
@@ -63,57 +94,49 @@ namespace gr {
char *optr = (char *)output_items[0];
float *sigout;
- //bool DEB=false;
-
- //if (DEB) printf("\nEnter work(): noutput_items= %d \n", noutput_items);
-
if(output_items.size() == 2)
- sigout = (float *)output_items[1];
+ sigout = (float *)output_items[1];
memset(optr, 0, noutput_items*sizeof(char));
- if(d_found==false) { // have not crossed threshold yet
- for(int i=0;i<noutput_items;i++) {
+ gr::thread::scoped_lock lock(d_setlock);
+
+ // have not crossed threshold yet
+ if(d_found==false) {
+ for(int i = 0; i < noutput_items; i++) {
d_avg = d_alpha*iptr[i] + (1.0f - d_alpha)*d_avg;
if(output_items.size() == 2)
sigout[i]=d_avg;
if(iptr[i] > d_avg * (1.0f + d_threshold_factor_rise)) {
d_found = true;
d_peak_val = -(float)INFINITY;
- //if (DEB) printf("crossed threshold at i=%d with value=%f\n",i,iptr[i]);
- set_output_multiple(d_look_ahead); // this is done so that the block eventually returns if there are not enough inputs.
- //if (DEB) printf("output multiple set at %d\n",output_multiple());
- //if (DEB) printf("return %d items\n",i);
+ set_output_multiple(d_look_ahead);
return i;
}
}
- //if (DEB) printf("output multiple is %d\n",output_multiple());
- //if (DEB) printf("returning (below threshold) items =%d\n",noutput_items);
return noutput_items;
} // end d_found==false
- else if(noutput_items>=d_look_ahead) { // can complete in this call
- //if (DEB) printf("Can complete in this call\n");
- for(int i=0;i<d_look_ahead;i++) {
+
+ // can complete in this call
+ else if(noutput_items >= d_look_ahead) {
+ for(int i = 0; i < d_look_ahead; i++) {
d_avg = d_alpha*iptr[i] + (1.0f - d_alpha)*d_avg;
if(output_items.size() == 2)
sigout[i]=d_avg;
if(iptr[i] > d_peak_val) {
d_peak_val = iptr[i];
d_peak_ind =i;
- //if (DEB) printf("peak found at i=%d, val=%f\n",i,d_peak_val);
}
}
optr[d_peak_ind] = 1;
- //if (DEB) printf("PEAK=%f\n",d_peak_val);
- d_found = false; // start searching again
- set_output_multiple(1);
- //if (DEB) printf("output multiple set at %d\n",output_multiple());
- //if (DEB) printf("returning (above threshold and finished searching) items =%d\n",d_look_ahead);
+
+ // restart the search
+ invalidate();
return d_look_ahead;
} // end can complete in this call
- else { // cannot complete in this call
- //if (DEB) printf("CANNOT BE HERE!!!!!!!!!!!!!!!!!!!!!\n");
- //if (DEB) printf("returning (above threshold, but not enough inputs). New lookahead=%d\n",d_look_ahead);
+
+ // cannot complete in this call
+ else {
return 0; // ask for more
}
}