Skip to content

Commit

Permalink
receiver: add peak_detector from gnuradio 3.7.10
Browse files Browse the repository at this point in the history
  • Loading branch information
MLsmd committed Aug 28, 2017
1 parent 6228a4b commit a2c413e
Show file tree
Hide file tree
Showing 11 changed files with 455 additions and 4 deletions.
3 changes: 2 additions & 1 deletion grc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ install(FILES
dab_reed_solomon_encode_bb.xml
dab_mp4_encode_sb.xml
dab_mp2_encode_sb.xml
dab_valve_ff.xml DESTINATION share/gnuradio/grc/blocks
dab_valve_ff.xml
dab_peak_detector_fb.xml DESTINATION share/gnuradio/grc/blocks
)
40 changes: 40 additions & 0 deletions grc/dab_peak_detector_fb.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0"?>
<block>
<name>peak_detector_fb</name>
<key>dab_peak_detector_fb</key>
<category>dab</category>
<import>import dab</import>
<make>dab.peak_detector_fb($threshold_factor_rise, $threshold_factor_fall, $look_ahead, $alpha)</make>
<param>
<name>TH Factor Rise</name>
<key>threshold_factor_rise</key>
<value>0.25</value>
<type>real</type>
</param>
<param>
<name>TH Factor Fall</name>
<key>threshold_factor_fall</key>
<value>0.40</value>
<type>real</type>
</param>
<param>
<name>Look Ahead</name>
<key>look_ahead</key>
<value>10</value>
<type>int</type>
</param>
<param>
<name>Alpha</name>
<key>alpha</key>
<value>0.001</value>
<type>real</type>
</param>
<sink>
<name>in</name>
<type>float</type>
</sink>
<source>
<name>out</name>
<type>byte</type>
</source>
</block>
3 changes: 2 additions & 1 deletion include/dab/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ install(FILES
reed_solomon_encode_bb.h
mp4_encode_sb.h
mp2_encode_sb.h
valve_ff.h DESTINATION include/dab
valve_ff.h
peak_detector_fb.h DESTINATION include/dab
)
115 changes: 115 additions & 0 deletions include/dab/peak_detector_fb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* -*- c++ -*- */
/*
* Copyright 2017 <+YOU OR YOUR COMPANY+>.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/


#ifndef INCLUDED_DAB_PEAK_DETECTOR_FB_H
#define INCLUDED_DAB_PEAK_DETECTOR_FB_H

#include <dab/api.h>
#include <gnuradio/sync_block.h>

namespace gr {
namespace dab {

/*!
* \brief Detect the peak of a signal
* \ingroup peak_detectors_blk
*
* \details
* If a peak is detected, this block outputs a 1,
* or it outputs 0's.
*/
class DAB_API peak_detector_fb : virtual public gr::sync_block
{
public:
typedef boost::shared_ptr<peak_detector_fb> sptr;

/*!
* Make a peak detector block.
*
* \param threshold_factor_rise The threshold factor determins
* when a peak has started. An average of the signal is
* calculated and when the value of the signal goes over
* threshold_factor_rise*average, we start looking for a
* peak.
* \param threshold_factor_fall The threshold factor determins
* when a peak has ended. An average of the signal is
* calculated and when the value of the signal goes
* below threshold_factor_fall*average, we stop looking
* for a peak.
* \param look_ahead The look-ahead value is used when the
* threshold is found to look if there another peak
* within this step range. If there is a larger value,
* we set that as the peak and look ahead again. This is
* continued until the highest point is found with This
* look-ahead range.
* \param alpha The gain value of a moving average filter
*/
static sptr make(float threshold_factor_rise = 0.25,
float threshold_factor_fall = 0.40,
int look_ahead = 10,
float alpha = 0.001);

/*! \brief Set the threshold factor value for the rise time
* \param thr new threshold factor
*/
virtual void set_threshold_factor_rise(float thr) = 0;

/*! \brief Set the threshold factor value for the fall time
* \param thr new threshold factor
*/
virtual void set_threshold_factor_fall(float thr) = 0;

/*! \brief Set the look-ahead factor
* \param look new look-ahead factor
*/
virtual void set_look_ahead(int look) = 0;

/*! \brief Set the running average alpha
* \param alpha new alpha for running average
*/
virtual void set_alpha(float alpha) = 0;

/*! \brief Get the threshold factor value for the rise time
* \return threshold factor
*/
virtual float threshold_factor_rise() = 0;

/*! \brief Get the threshold factor value for the fall time
* \return threshold factor
*/
virtual float threshold_factor_fall() = 0;

/*! \brief Get the look-ahead factor value
* \return look-ahead factor
*/
virtual int look_ahead() = 0;

/*! \brief Get the alpha value of the running average
* \return alpha
*/
virtual float alpha() = 0;
};

} // namespace dab
} // namespace gr

#endif /* INCLUDED_DAB_PEAK_DETECTOR_FB_H */

3 changes: 2 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ list(APPEND dab_sources
${LIBTOOLAME-DAB_SOURCE_DIR}/psycho_n1.c
${LIBTOOLAME-DAB_SOURCE_DIR}/encode_new.c
${LIBTOOLAME-DAB_SOURCE_DIR}/portableio.c
valve_ff_impl.cc )
valve_ff_impl.cc
peak_detector_fb_impl.cc )


set(dab_sources "${dab_sources}" PARENT_SCOPE)
Expand Down
121 changes: 121 additions & 0 deletions lib/peak_detector_fb_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* -*- c++ -*- */
/* This is a version of the GNU Radio peak_detector_fb block before commit 9d9ea63c45b5f314eb344a69340ef49e8edafdfa.
*
* Copyright 2007,2010,2013 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include "peak_detector_fb_impl.h"
#include <string.h>

namespace gr {
namespace dab {

peak_detector_fb::sptr
peak_detector_fb::make(float threshold_factor_rise, float threshold_factor_fall, int look_ahead, float alpha)
{
return gnuradio::get_initial_sptr
(new peak_detector_fb_impl(threshold_factor_rise, threshold_factor_fall, look_ahead, alpha));
}

/*
* The private constructor
*/
peak_detector_fb_impl::peak_detector_fb_impl(float threshold_factor_rise, float threshold_factor_fall, int look_ahead, float alpha)
: gr::sync_block("peak_detector_fb",
gr::io_signature::make(1, 1, sizeof(float)),
gr::io_signature::make(1, 1, sizeof(char))),
d_threshold_factor_rise(threshold_factor_rise),
d_threshold_factor_fall(threshold_factor_fall),
d_look_ahead(look_ahead), d_avg_alpha(alpha), d_avg(0), d_found(0)
{}

/*
* Our virtual destructor.
*/
peak_detector_fb_impl::~peak_detector_fb_impl()
{
}

int
peak_detector_fb_impl::work(int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
float *iptr = (float*)input_items[0];
char *optr = (char*)output_items[0];

memset(optr, 0, noutput_items*sizeof(char));

float peak_val = -(float)INFINITY;
int peak_ind = 0;
unsigned char state = 0;
int i = 0;

//printf("noutput_items %d\n",noutput_items);
while(i < noutput_items) {
if(state == 0) { // below threshold
if(iptr[i] > d_avg*d_threshold_factor_rise) {
state = 1;
}
else {
d_avg = (d_avg_alpha)*iptr[i] + (1-d_avg_alpha)*d_avg;
i++;
}
}
else if(state == 1) { // above threshold, have not found peak
//printf("Entered State 1: %f i: %d noutput_items: %d\n", iptr[i], i, noutput_items);
if(iptr[i] > peak_val) {
peak_val = iptr[i];
peak_ind = i;
d_avg = (d_avg_alpha)*iptr[i] + (1-d_avg_alpha)*d_avg;
i++;
}
else if(iptr[i] > d_avg*d_threshold_factor_fall) {
d_avg = (d_avg_alpha)*iptr[i] + (1-d_avg_alpha)*d_avg;
i++;
}
else {
optr[peak_ind] = 1;
state = 0;
peak_val = -(float)INFINITY;
//printf("Leaving State 1: Peak: %f Peak Ind: %d i: %d noutput_items: %d\n",
//peak_val, peak_ind, i, noutput_items);
}
}
}

if(state == 0) {
//printf("Leave in State 0, produced %d\n",noutput_items);
return noutput_items;
}
else { // only return up to passing the threshold
//printf("Leave in State 1, only produced %d of %d\n",peak_ind,noutput_items);
return peak_ind+1;
}
}

} /* namespace dab */
} /* namespace gr */

65 changes: 65 additions & 0 deletions lib/peak_detector_fb_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* -*- c++ -*- */
/* This is a version of the GNU Radio peak_detector_fb block before commit 9d9ea63c45b5f314eb344a69340ef49e8edafdfa.
*
* Copyright 2007,2010,2013 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/

#ifndef INCLUDED_DAB_PEAK_DETECTOR_FB_IMPL_H
#define INCLUDED_DAB_PEAK_DETECTOR_FB_IMPL_H

#include <dab/peak_detector_fb.h>

namespace gr {
namespace dab {

class peak_detector_fb_impl : public peak_detector_fb
{
private:
float d_threshold_factor_rise;
float d_threshold_factor_fall;
int d_look_ahead;
float d_avg_alpha;
float d_avg;
unsigned char d_found;

public:
peak_detector_fb_impl(float threshold_factor_rise, float threshold_factor_fall, int look_ahead, float alpha);
~peak_detector_fb_impl();

void set_threshold_factor_rise(float thr) { d_threshold_factor_rise = thr; }
void set_threshold_factor_fall(float thr) { d_threshold_factor_fall = thr; }
void set_look_ahead(int look) { d_look_ahead = look; }
void set_alpha(float alpha) { d_avg_alpha = alpha; }
float threshold_factor_rise() { return d_threshold_factor_rise; }
float threshold_factor_fall() { return d_threshold_factor_fall; }
int look_ahead() { return d_look_ahead; }
float alpha() { return d_avg_alpha; }

// Where all the action really happens
int work(int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
};

} // namespace dab
} // namespace gr

#endif /* INCLUDED_DAB_PEAK_DETECTOR_FB_IMPL_H */

1 change: 1 addition & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ GR_ADD_TEST(qa_reed_solomon_encode_bb ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURC
GR_ADD_TEST(qa_mp4_encode_sb ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_mp4_encode_sb.py)
GR_ADD_TEST(qa_mp2_encode_sb ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_mp2_encode_sb.py)
GR_ADD_TEST(qa_valve_ff ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_valve_ff.py)
GR_ADD_TEST(qa_peak_detector_fb ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_peak_detector_fb.py)
4 changes: 3 additions & 1 deletion python/detect_null.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

# Andreas Mueller, 2008
# [email protected]
# Moritz Luca Schmid, 2017
# [email protected]

from gnuradio import gr, blocks
from dab import dab_swig
Expand Down Expand Up @@ -56,7 +58,7 @@ def __init__(self, length, debug=False):
self.ns_invert = blocks.multiply_const_ff(-1)

# peak detector on the inverted, summed up signal -> we get the nulls (i.e. the position of the start of a frame)
self.ns_peak_detect = blocks.peak_detector_fb(0.6,0.7,10,0.0001) # mostly found by try and error -> remember that the values are negative!
self.ns_peak_detect = dab_swig.peak_detector_fb(0.6,0.7,10,0.0001) # mostly found by try and error -> remember that the values are negative!

# connect it all
self.connect(self, self.ns_c2magsquared, self.ns_moving_sum, self.ns_invert, self.ns_peak_detect, self)
Expand Down
Loading

0 comments on commit a2c413e

Please sign in to comment.