2016-01-27 21:54:42 +01:00
|
|
|
/* -*- c++ -*- */
|
|
|
|
/*
|
|
|
|
* gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
|
|
|
|
*
|
2017-04-27 18:02:01 +02:00
|
|
|
* Copyright (C) 2016,2017
|
|
|
|
* Libre Space Foundation <http://librespacefoundation.org/>
|
2016-01-27 21:54:42 +01:00
|
|
|
*
|
|
|
|
* This program 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 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2016-02-27 23:45:41 +01:00
|
|
|
#include <limits>
|
2016-01-27 21:54:42 +01:00
|
|
|
#include <gnuradio/io_signature.h>
|
|
|
|
#include <satnogs/log.h>
|
2016-02-27 23:45:41 +01:00
|
|
|
#include <satnogs/utils.h>
|
2017-04-27 18:02:01 +02:00
|
|
|
#include <boost/math/common_factor.hpp>
|
2016-01-27 21:54:42 +01:00
|
|
|
#include "cw_to_symbol_impl.h"
|
2017-04-27 18:02:01 +02:00
|
|
|
#include <volk/volk.h>
|
2016-01-27 21:54:42 +01:00
|
|
|
|
|
|
|
namespace gr
|
|
|
|
{
|
|
|
|
namespace satnogs
|
|
|
|
{
|
|
|
|
|
|
|
|
cw_to_symbol::sptr
|
2017-04-08 21:18:40 +02:00
|
|
|
cw_to_symbol::make (double sampling_rate, float threshold, float conf_level,
|
2017-04-27 18:02:01 +02:00
|
|
|
size_t wpm)
|
2016-01-27 21:54:42 +01:00
|
|
|
{
|
|
|
|
return gnuradio::get_initial_sptr (
|
2017-04-27 18:02:01 +02:00
|
|
|
new cw_to_symbol_impl (sampling_rate, threshold, conf_level, wpm));
|
2016-01-27 21:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The private constructor
|
|
|
|
*/
|
|
|
|
cw_to_symbol_impl::cw_to_symbol_impl (double sampling_rate, float threshold,
|
2017-04-27 18:02:01 +02:00
|
|
|
float conf_level, size_t wpm) :
|
2017-04-08 21:18:40 +02:00
|
|
|
gr::sync_block ("cw_to_symbol",
|
|
|
|
gr::io_signature::make (1, 1, sizeof(float)),
|
|
|
|
gr::io_signature::make (0, 0, 0)),
|
|
|
|
d_sampling_rate (sampling_rate),
|
|
|
|
d_act_thrshld (threshold),
|
|
|
|
d_confidence_level (conf_level),
|
2017-11-01 20:59:25 +01:00
|
|
|
d_dot_samples ((1.2 / wpm) * sampling_rate),
|
2017-04-27 18:02:01 +02:00
|
|
|
d_window_size(0),
|
|
|
|
d_window_cnt(0),
|
|
|
|
d_dot_windows_num(0),
|
|
|
|
d_dec_state (NO_SYNC),
|
2017-08-06 22:48:34 +02:00
|
|
|
d_prev_space_symbol (true)
|
2016-01-27 21:54:42 +01:00
|
|
|
{
|
2017-08-06 22:48:34 +02:00
|
|
|
if (wpm < MIN_WPM) {
|
|
|
|
throw std::invalid_argument (
|
|
|
|
"Decoder can not handle such low WPM setting");
|
2017-04-27 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 22:48:34 +02:00
|
|
|
if (wpm > MAX_WPM) {
|
|
|
|
throw std::invalid_argument (
|
|
|
|
"Decoder can not handle such high WPM setting");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf_level > 1.0 || conf_level < 0.5) {
|
|
|
|
throw std::invalid_argument (
|
|
|
|
"Confidence level should be in the range [0.5, 1.0]");
|
2017-04-27 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
2017-04-08 21:18:40 +02:00
|
|
|
message_port_register_in (pmt::mp ("act_threshold"));
|
|
|
|
message_port_register_out (pmt::mp ("out"));
|
2016-02-27 23:45:41 +01:00
|
|
|
|
|
|
|
/* Register the message handlers */
|
2017-04-08 21:18:40 +02:00
|
|
|
set_msg_handler (
|
|
|
|
pmt::mp ("act_threshold"),
|
|
|
|
boost::bind (&cw_to_symbol_impl::set_act_threshold_msg_handler, this,
|
|
|
|
_1));
|
|
|
|
|
2017-04-27 18:02:01 +02:00
|
|
|
/*
|
|
|
|
* Try to split the CW pulses in smaller windows for detecting faster
|
|
|
|
* a false alarm. As we use the window size for setting the history
|
|
|
|
* it should have a reasonably size.
|
|
|
|
*/
|
2017-09-05 00:37:47 +02:00
|
|
|
size_t i = 1;
|
2017-04-27 18:02:01 +02:00
|
|
|
d_window_size = d_dot_samples / i;
|
2017-09-06 17:09:31 +02:00
|
|
|
while(d_window_size > 64) {
|
2017-09-05 00:37:47 +02:00
|
|
|
i++;
|
2017-04-27 18:02:01 +02:00
|
|
|
d_window_size = d_dot_samples / i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: The dot duration should be a perfect multiple of the window */
|
|
|
|
while(d_dot_samples % d_window_size != 0) {
|
|
|
|
d_window_size++;
|
2016-02-27 23:45:41 +01:00
|
|
|
}
|
2017-04-27 18:02:01 +02:00
|
|
|
|
2017-11-01 20:59:25 +01:00
|
|
|
LOG_WARN("Dot symbol samples: %lu", d_dot_samples);
|
|
|
|
LOG_WARN("Window size: %lu", d_window_size);
|
2017-09-05 00:37:47 +02:00
|
|
|
|
2017-04-27 18:02:01 +02:00
|
|
|
/* Set the duration of each symbol in multiples of the window size */
|
|
|
|
d_dot_windows_num = d_dot_samples / d_window_size;
|
|
|
|
d_dash_windows_num = 3 * d_dot_windows_num;
|
|
|
|
d_short_pause_windows_num = d_dash_windows_num;
|
|
|
|
d_long_pause_windows_num = 7 * d_dot_windows_num;
|
|
|
|
|
|
|
|
const int alignment_multiple = volk_get_alignment ()
|
|
|
|
/ (d_window_size * sizeof(float));
|
|
|
|
set_alignment (std::max (1, alignment_multiple));
|
|
|
|
|
|
|
|
d_const_val = (float *) volk_malloc(d_window_size * sizeof(float),
|
|
|
|
volk_get_alignment ());
|
|
|
|
d_tmp = (float *) volk_malloc(d_window_size * sizeof(float),
|
|
|
|
volk_get_alignment ());
|
|
|
|
d_out = (int32_t *) volk_malloc (d_window_size * sizeof(int32_t),
|
|
|
|
volk_get_alignment ());
|
|
|
|
|
|
|
|
if(!d_const_val || !d_tmp || !d_out) {
|
|
|
|
throw std::runtime_error("cw_to_symbol: Could not allocate memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < d_window_size; i++) {
|
|
|
|
d_const_val[i] = threshold;
|
|
|
|
}
|
|
|
|
set_history(d_window_size);
|
2016-01-27 21:54:42 +01:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:37:36 +01:00
|
|
|
inline void
|
|
|
|
cw_to_symbol_impl::send_symbol_msg (morse_symbol_t s)
|
|
|
|
{
|
2017-04-27 18:02:01 +02:00
|
|
|
if(s == MORSE_S_SPACE || s == MORSE_L_SPACE) {
|
|
|
|
d_prev_space_symbol = true;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
d_prev_space_symbol = false;
|
|
|
|
}
|
2017-04-08 21:18:40 +02:00
|
|
|
message_port_pub (pmt::mp ("out"), pmt::from_long (s));
|
2016-01-30 20:37:36 +01:00
|
|
|
}
|
|
|
|
|
2017-08-06 22:48:34 +02:00
|
|
|
inline bool
|
|
|
|
cw_to_symbol_impl::check_conf_level(size_t cnt, size_t target)
|
|
|
|
{
|
|
|
|
return ((float)cnt > target * d_confidence_level);
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:54:42 +01:00
|
|
|
/*
|
|
|
|
* Our virtual destructor.
|
|
|
|
*/
|
|
|
|
cw_to_symbol_impl::~cw_to_symbol_impl ()
|
|
|
|
{
|
2017-04-27 18:02:01 +02:00
|
|
|
volk_free (d_const_val);
|
|
|
|
volk_free (d_tmp);
|
|
|
|
volk_free (d_out);
|
2016-01-27 21:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
cw_to_symbol_impl::set_idle ()
|
|
|
|
{
|
2017-08-06 22:48:34 +02:00
|
|
|
d_dec_state = NO_SYNC;
|
2017-04-27 18:02:01 +02:00
|
|
|
d_window_cnt = 0;
|
2016-01-27 21:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
cw_to_symbol_impl::set_short_on ()
|
|
|
|
{
|
2017-04-27 18:02:01 +02:00
|
|
|
d_dec_state = SEARCH_DOT;
|
|
|
|
d_window_cnt = 1;
|
2016-01-27 21:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
cw_to_symbol_impl::set_long_on ()
|
|
|
|
{
|
2017-04-27 18:02:01 +02:00
|
|
|
d_dec_state = SEARCH_DASH;
|
2016-01-27 21:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2017-08-06 22:48:34 +02:00
|
|
|
cw_to_symbol_impl::set_search_space ()
|
2016-01-27 21:54:42 +01:00
|
|
|
{
|
2017-08-06 22:48:34 +02:00
|
|
|
d_dec_state = SEARCH_SPACE;
|
2017-04-27 18:02:01 +02:00
|
|
|
d_window_cnt = 1;
|
2016-01-27 21:54:42 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 02:16:18 +01:00
|
|
|
void
|
|
|
|
cw_to_symbol_impl::set_act_threshold_msg_handler (pmt::pmt_t msg)
|
|
|
|
{
|
2017-04-08 21:18:40 +02:00
|
|
|
if (pmt::is_pair (msg)) {
|
|
|
|
set_act_threshold (pmt::to_double (pmt::cdr (msg)));
|
2016-02-17 02:16:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:54:42 +01:00
|
|
|
int
|
|
|
|
cw_to_symbol_impl::work (int noutput_items,
|
2017-04-08 21:18:40 +02:00
|
|
|
gr_vector_const_void_star &input_items,
|
|
|
|
gr_vector_void_star &output_items)
|
2016-01-27 21:54:42 +01:00
|
|
|
{
|
2017-04-27 18:02:01 +02:00
|
|
|
bool triggered;
|
2017-09-06 17:09:31 +02:00
|
|
|
size_t i;
|
2017-04-27 18:02:01 +02:00
|
|
|
const float *in_old = (const float *) input_items[0];
|
|
|
|
const float *in = in_old + history() - 1;
|
|
|
|
|
2017-09-06 17:09:31 +02:00
|
|
|
if(noutput_items < 0) {
|
|
|
|
return noutput_items;
|
|
|
|
}
|
|
|
|
|
2017-04-27 18:02:01 +02:00
|
|
|
/* During idle state search for a possible trigger */
|
2017-08-06 22:48:34 +02:00
|
|
|
if(d_dec_state == NO_SYNC) {
|
2017-09-06 17:09:31 +02:00
|
|
|
for(i = 0; i < (size_t)noutput_items; i++) {
|
2017-04-27 18:02:01 +02:00
|
|
|
/*
|
|
|
|
* Clamp the input so the window mean is not affected by strong spikes
|
|
|
|
* Good luck understanding this black magic shit!
|
|
|
|
*/
|
|
|
|
triggered = is_triggered(in_old + i, d_window_size);
|
|
|
|
if(triggered) {
|
2017-08-06 22:48:34 +02:00
|
|
|
LOG_DEBUG("Triggered!");
|
2017-04-27 18:02:01 +02:00
|
|
|
set_short_on();
|
|
|
|
return i+1;
|
|
|
|
}
|
2017-04-08 21:18:40 +02:00
|
|
|
}
|
|
|
|
return noutput_items;
|
2016-02-27 23:45:41 +01:00
|
|
|
}
|
2017-08-06 22:48:34 +02:00
|
|
|
|
|
|
|
/* From now one, we handle the input in multiples of a window */
|
2017-09-06 17:09:31 +02:00
|
|
|
for (i = 0; i < (size_t)noutput_items / d_window_size; i++) {
|
2017-08-06 22:48:34 +02:00
|
|
|
triggered = is_triggered(in + i * d_window_size, d_window_size);
|
|
|
|
switch(d_dec_state) {
|
|
|
|
case SEARCH_DOT:
|
|
|
|
if(triggered) {
|
|
|
|
d_window_cnt++;
|
|
|
|
if(d_window_cnt > d_dot_windows_num) {
|
|
|
|
set_long_on();
|
2017-11-01 20:59:25 +01:00
|
|
|
LOG_DEBUG("Going to search for long sequence");
|
2017-08-06 22:48:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(check_conf_level(d_window_cnt, d_dot_windows_num)) {
|
|
|
|
LOG_DEBUG("DOT");
|
|
|
|
send_symbol_msg(MORSE_DOT);
|
|
|
|
}
|
2017-11-01 20:59:25 +01:00
|
|
|
LOG_DEBUG("Going to search for space: win cnt %lu", d_window_cnt);
|
2017-08-06 22:48:34 +02:00
|
|
|
set_search_space ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEARCH_DASH:
|
|
|
|
if(triggered) {
|
|
|
|
d_window_cnt++;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if(check_conf_level(d_window_cnt, d_dash_windows_num)) {
|
|
|
|
LOG_DEBUG("DASH");
|
|
|
|
send_symbol_msg(MORSE_DASH);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
LOG_DEBUG("DOT");
|
|
|
|
send_symbol_msg(MORSE_DOT);
|
|
|
|
}
|
|
|
|
set_search_space ();
|
2017-11-01 20:59:25 +01:00
|
|
|
LOG_DEBUG("Going to search for space");
|
2017-08-06 22:48:34 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEARCH_SPACE:
|
|
|
|
if (triggered) {
|
|
|
|
if(check_conf_level(d_window_cnt, d_long_pause_windows_num)) {
|
|
|
|
LOG_DEBUG("LONG SPACE");
|
|
|
|
send_symbol_msg(MORSE_L_SPACE);
|
|
|
|
}
|
|
|
|
else if(check_conf_level(d_window_cnt, d_short_pause_windows_num)){
|
|
|
|
LOG_DEBUG("SHORT SPACE");
|
|
|
|
send_symbol_msg(MORSE_S_SPACE);
|
|
|
|
}
|
|
|
|
set_short_on();
|
2017-11-01 20:59:25 +01:00
|
|
|
LOG_DEBUG("Going to search for dot");
|
2017-08-06 22:48:34 +02:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
d_window_cnt++;
|
|
|
|
if(d_window_cnt > d_long_pause_windows_num) {
|
|
|
|
LOG_DEBUG("LONG SPACE");
|
|
|
|
send_symbol_msg(MORSE_L_SPACE);
|
|
|
|
set_idle();
|
2017-11-01 20:59:25 +01:00
|
|
|
LOG_DEBUG("Going to idle");
|
2017-08-06 22:48:34 +02:00
|
|
|
return (i + 1) * d_window_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERROR("Invalid decoder state");
|
2017-04-27 18:02:01 +02:00
|
|
|
}
|
2016-01-27 21:54:42 +01:00
|
|
|
}
|
2017-08-06 22:48:34 +02:00
|
|
|
return i * d_window_size;
|
2016-01-27 21:54:42 +01:00
|
|
|
}
|
|
|
|
|
2016-02-27 23:45:41 +01:00
|
|
|
/**
|
2017-04-27 18:02:01 +02:00
|
|
|
* Sets a new activation threshold.
|
|
|
|
* @param thrhld the new threshold.
|
2016-02-27 23:45:41 +01:00
|
|
|
*/
|
|
|
|
void
|
2017-04-27 18:02:01 +02:00
|
|
|
cw_to_symbol_impl::set_act_threshold (float thrhld)
|
2016-02-27 23:45:41 +01:00
|
|
|
{
|
2017-04-27 18:02:01 +02:00
|
|
|
d_act_thrshld = thrhld;
|
2016-02-27 23:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-27 18:02:01 +02:00
|
|
|
* Clamps the input and performs at the same time binary slicing.
|
|
|
|
* With this way, a decision based on moving average is not affected
|
|
|
|
* by strong peaks.
|
|
|
|
* @param out the output buffer with the binary sliced output
|
|
|
|
* @param in the input signal
|
|
|
|
* @param len number of samples to process
|
2016-02-27 23:45:41 +01:00
|
|
|
*/
|
2017-04-27 18:02:01 +02:00
|
|
|
inline void
|
|
|
|
cw_to_symbol_impl::clamp_input (int32_t* out, const float* in, size_t len)
|
2016-02-27 23:45:41 +01:00
|
|
|
{
|
2017-04-27 18:02:01 +02:00
|
|
|
volk_32f_x2_subtract_32f(d_tmp, in, d_const_val, len);
|
|
|
|
volk_32f_binary_slicer_32i(d_out, d_tmp, len);
|
2016-02-27 23:45:41 +01:00
|
|
|
}
|
|
|
|
|
2017-09-06 17:09:31 +02:00
|
|
|
static inline int32_t
|
|
|
|
hadd (const int32_t* in, size_t len)
|
2016-02-17 02:16:18 +01:00
|
|
|
{
|
2017-04-27 18:02:01 +02:00
|
|
|
size_t i;
|
|
|
|
int32_t cnt = 0;
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
cnt += in[i];
|
2016-02-27 23:45:41 +01:00
|
|
|
}
|
2017-04-27 18:02:01 +02:00
|
|
|
return cnt;
|
2016-02-27 23:45:41 +01:00
|
|
|
}
|
|
|
|
|
2017-04-27 18:02:01 +02:00
|
|
|
inline bool
|
|
|
|
cw_to_symbol_impl::is_triggered (const float* in, size_t len)
|
2016-02-27 23:45:41 +01:00
|
|
|
{
|
2017-04-27 18:02:01 +02:00
|
|
|
int32_t cnt;
|
|
|
|
clamp_input(d_out, in, len);
|
|
|
|
cnt = hadd(d_out, len);
|
|
|
|
return (cnt >= (int32_t)(d_window_size * d_confidence_level)) ? true : false;
|
2016-02-17 02:16:18 +01:00
|
|
|
}
|
|
|
|
|
2016-01-27 21:54:42 +01:00
|
|
|
} /* namespace satnogs */
|
|
|
|
} /* namespace gr */
|
|
|
|
|