-
Notifications
You must be signed in to change notification settings - Fork 81
/
extractor_impl.cc
119 lines (102 loc) · 4.79 KB
/
extractor_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- c++ -*- */
/*
* Copyright 2022 gr-droneid author.
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gnuradio/io_signature.h>
#include "extractor_impl.h"
#include "droneid/misc_utils.h"
namespace gr {
namespace droneid {
extractor::sptr
extractor::make(const double sample_rate, const float threshold) {
return gnuradio::get_initial_sptr
(new extractor_impl(sample_rate, threshold));
}
/*
* The private constructor
*/
extractor_impl::extractor_impl(double sample_rate, const float threshold)
: gr::sync_block("extractor",
gr::io_signature::make2(2, 2, sizeof(gr_complex), sizeof(float)),
gr::io_signature::make(0, 0, 0)), fft_size_(misc_utils::get_fft_size(sample_rate)),
long_cp_len_(misc_utils::get_long_cp_len(sample_rate)), short_cp_len_(misc_utils::get_short_cp_len(sample_rate)),
extract_samples_count_((fft_size_ * 13) + (long_cp_len_ * 2) + (short_cp_len_ * 7)){
this->message_port_register_out(pmt::mp("pdus"));
buffer_.resize(extract_samples_count_);
current_state_ = state_t::WAITING_FOR_THRESHOLD;
collected_samples_ = 0;
total_samples_read_ = 0;
threshold_ = threshold;
}
/*
* Our virtual destructor.
*/
extractor_impl::~extractor_impl() {
}
void extractor_impl::set_threshold(const float threshold) {
std::lock_guard<decltype(parameter_lock_)> l(parameter_lock_);
threshold_ = threshold;
}
int
extractor_impl::work(int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items) {
const gr_complex *samples = (const gr_complex *) input_items[0];
const float *correlation_scores = (const float *) input_items[1];
float threshold;
{
std::lock_guard<decltype(parameter_lock_)> lock(parameter_lock_);
threshold = threshold_;
}
for (int idx = 0; idx < noutput_items; idx++) {
total_samples_read_++;
switch (current_state_) {
case state_t::WAITING_FOR_THRESHOLD: {
if (correlation_scores[idx] > threshold) {
start_sample_index_ = nitems_read(0) + idx;
std::cout << "Found burst @ " << total_samples_read_ << " / " << start_sample_index_ << "\n";
current_state_ = state_t::COLLECTING_SAMPLES;
collected_samples_ = 1;
buffer_[0] = samples[idx];
}
break;
}
case state_t::COLLECTING_SAMPLES: {
buffer_[collected_samples_++] = samples[idx];
if (collected_samples_ == extract_samples_count_) {
current_state_ = state_t::WAITING_FOR_THRESHOLD;
const pmt::pmt_t vec = pmt::init_c32vector(buffer_.size(), buffer_);
pmt::pmt_t meta = pmt::make_dict();
meta = pmt::dict_add(meta, pmt::mp("start_offset"), pmt::from_uint64(start_sample_index_));
std::cout << "Sending message with " << pmt::length(vec) << " elements (" << extract_samples_count_ << ") starting at " << start_sample_index_ << "\n";
message_port_pub(pmt::mp("pdus"), pmt::cons(meta, vec));
collected_samples_ = 0;
}
break;
}
}
}
// Do <+signal processing+>
// Tell runtime system how many output items we produced.
return noutput_items;
}
} /* namespace droneid */
} /* namespace gr */