diff options
author | Achilleas Anastasopoulos <anastas@umich.edu> | 2015-04-22 11:00:50 -0400 |
---|---|---|
committer | Johnathan Corgan <johnathan@corganlabs.com> | 2015-04-26 15:35:45 -0700 |
commit | abb3e0643ae245f08b7e7bcdaf3b8e8f0c312908 (patch) | |
tree | d629fa7c3a05708289ffb6a01dc87a2d9937aa6f /gr-blocks/lib/peak_detector2_fb_impl.cc | |
parent | c0a88bebb5ac163dd774f113b3eef71ed30221ea (diff) |
blocks: better implementation of peak_detector2. Address #783.
In the original form, work function in peak_detector2 block sometimes
returns -1. For more details, see the mailing list thread:
- http://lists.gnu.org/archive/html/discuss-gnuradio/2015-04/msg00197.html
Diffstat (limited to 'gr-blocks/lib/peak_detector2_fb_impl.cc')
-rw-r--r-- | gr-blocks/lib/peak_detector2_fb_impl.cc | 82 |
1 files changed, 45 insertions, 37 deletions
diff --git a/gr-blocks/lib/peak_detector2_fb_impl.cc b/gr-blocks/lib/peak_detector2_fb_impl.cc index 7ff7f542ec..e631768e55 100644 --- a/gr-blocks/lib/peak_detector2_fb_impl.cc +++ b/gr-blocks/lib/peak_detector2_fb_impl.cc @@ -43,8 +43,8 @@ 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))), + 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) { @@ -61,53 +61,61 @@ namespace gr { { float *iptr = (float *)input_items[0]; 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]; memset(optr, 0, noutput_items*sizeof(char)); - for(int i = 0; i < noutput_items; i++) { - if(!d_found) { - // Have not yet detected presence of peak + if(d_found==false) { // have not crossed threshold yet + 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_look_ahead_remaining = d_look_ahead; d_peak_val = -(float)INFINITY; - } - else { - d_avg = d_alpha*iptr[i] + (1.0f - d_alpha)*d_avg; + //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); + return i; } } - else { - // Detected presence of peak + //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++) { + 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; - } - else if(d_look_ahead_remaining <= 0) { - optr[d_peak_ind] = 1; - d_found = false; - d_avg = iptr[i]; + d_peak_ind =i; + //if (DEB) printf("peak found at i=%d, val=%f\n",i,d_peak_val); } - - // Have not yet located peak, loop and keep searching. - d_look_ahead_remaining--; } - - // Every iteration of the loop, write debugging signal out if - // connected: - if(output_items.size() == 2) { - float *sigout = (float *)output_items[1]; - sigout[i] = d_avg; - } - } // loop - - if(!d_found) - return noutput_items; - - // else if detected presence, keep searching during the next call to work. - int tmp = d_peak_ind; - d_peak_ind = 1; - - return tmp - 1; + 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); + 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); + return 0; // ask for more + } } } /* namespace blocks */ |