2010-10-19 00:59:08 +08:00
|
|
|
/*
|
|
|
|
# Copyright 2010 Nick Foster
|
2013-04-02 06:51:00 +08:00
|
|
|
# Copyright 2013 Nicholas Corgan
|
2010-10-19 00:59:08 +08:00
|
|
|
#
|
|
|
|
# This file is part of gr-air-modes
|
|
|
|
#
|
|
|
|
# gr-air-modes 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.
|
|
|
|
#
|
|
|
|
# gr-air-modes 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 gr-air-modes; see the file COPYING. If not, write to
|
|
|
|
# the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
*/
|
|
|
|
|
2010-09-15 13:01:56 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-04-02 06:51:00 +08:00
|
|
|
#include <ciso646>
|
2010-09-15 13:01:56 +08:00
|
|
|
#include <air_modes_preamble.h>
|
2013-07-18 09:03:45 +08:00
|
|
|
#include <gnuradio/io_signature.h>
|
2010-11-21 08:34:44 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <iostream>
|
2013-07-18 09:03:45 +08:00
|
|
|
#include <gnuradio/tags.h>
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2012-07-17 06:22:14 +08:00
|
|
|
air_modes_preamble_sptr air_make_modes_preamble(int channel_rate, float threshold_db)
|
2010-09-15 13:01:56 +08:00
|
|
|
{
|
|
|
|
return air_modes_preamble_sptr (new air_modes_preamble(channel_rate, threshold_db));
|
|
|
|
}
|
|
|
|
|
|
|
|
air_modes_preamble::air_modes_preamble(int channel_rate, float threshold_db) :
|
2013-07-18 09:03:45 +08:00
|
|
|
gr::block ("modes_preamble",
|
|
|
|
gr::io_signature::make2 (2, 2, sizeof(float), sizeof(float)), //stream 0 is received data, stream 1 is moving average for reference
|
|
|
|
gr::io_signature::make (1, 1, sizeof(float))) //the output packets
|
2013-06-06 03:56:09 +08:00
|
|
|
{
|
|
|
|
d_chip_rate = 2000000; //2Mchips per second
|
|
|
|
d_samples_per_chip = channel_rate / d_chip_rate; //must be integer number of samples per chip to work
|
|
|
|
d_samples_per_symbol = d_samples_per_chip * 2;
|
2013-07-18 09:03:45 +08:00
|
|
|
d_check_width = 120 * d_samples_per_symbol; //only search to this far from the end of the stream buffer
|
2013-06-06 03:56:09 +08:00
|
|
|
d_threshold_db = threshold_db;
|
|
|
|
d_threshold = powf(10., threshold_db/20.); //the level that the sample must be above the moving average in order to qualify as a pulse
|
2013-07-18 09:03:45 +08:00
|
|
|
d_secs_per_sample = 1.0 / channel_rate;
|
|
|
|
set_output_multiple(1+d_check_width*2);
|
|
|
|
|
|
|
|
std::stringstream str;
|
|
|
|
str << name() << unique_id();
|
|
|
|
d_me = pmt::string_to_symbol(str.str());
|
|
|
|
d_key = pmt::string_to_symbol("preamble_found");
|
|
|
|
set_history(d_samples_per_symbol);
|
2013-06-06 03:56:09 +08:00
|
|
|
}
|
|
|
|
|
2011-06-12 12:32:28 +08:00
|
|
|
static void integrate_and_dump(float *out, const float *in, int chips, int samps_per_chip) {
|
|
|
|
for(int i=0; i<chips; i++) {
|
|
|
|
float acc = 0;
|
|
|
|
for(int j=0; j<samps_per_chip; j++) {
|
|
|
|
acc += in[i*samps_per_chip+j];
|
|
|
|
}
|
2011-06-12 13:07:20 +08:00
|
|
|
out[i] = acc;
|
2011-06-12 12:32:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//the preamble pattern in bits
|
|
|
|
//fixme goes in .h
|
|
|
|
static const bool preamble_bits[] = {1, 0, 1, 0, 0, 0, 0, 1, 0, 1};
|
|
|
|
static double correlate_preamble(const float *in, int samples_per_chip) {
|
|
|
|
double corr = 0.0;
|
|
|
|
for(int i=0; i<10; i++) {
|
|
|
|
for(int j=0; j<samples_per_chip;j++)
|
|
|
|
if(preamble_bits[i]) corr += in[i*samples_per_chip+j];
|
|
|
|
}
|
|
|
|
return corr;
|
|
|
|
}
|
|
|
|
|
2011-11-18 06:58:19 +08:00
|
|
|
//todo: make it return a pair of some kind, otherwise you can lose precision
|
2013-07-18 09:03:45 +08:00
|
|
|
static double tag_to_timestamp(gr::tag_t tstamp, uint64_t abs_sample_cnt, double secs_per_sample) {
|
2011-09-08 08:07:52 +08:00
|
|
|
uint64_t ts_sample, last_whole_stamp;
|
|
|
|
double last_frac_stamp;
|
2011-08-25 09:19:57 +08:00
|
|
|
|
2013-07-18 09:03:45 +08:00
|
|
|
if(tstamp.key == NULL || pmt::symbol_to_string(tstamp.key) != "rx_time") return 0;
|
2011-08-25 09:19:57 +08:00
|
|
|
|
2013-07-18 09:03:45 +08:00
|
|
|
last_whole_stamp = pmt::to_uint64(pmt::tuple_ref(tstamp.value, 0));
|
|
|
|
last_frac_stamp = pmt::to_double(pmt::tuple_ref(tstamp.value, 1));
|
2011-11-18 07:16:56 +08:00
|
|
|
ts_sample = tstamp.offset;
|
2012-06-13 22:49:22 +08:00
|
|
|
|
|
|
|
double tstime = double(abs_sample_cnt * secs_per_sample) + last_whole_stamp + last_frac_stamp;
|
2012-08-24 00:03:56 +08:00
|
|
|
if(0) std::cout << "HEY WE GOT A STAMP AT " << tstime << " TICKS AT SAMPLE " << ts_sample << " ABS SAMPLE CNT IS " << abs_sample_cnt << std::endl;
|
2012-06-13 22:49:22 +08:00
|
|
|
return tstime;
|
2011-08-25 09:19:57 +08:00
|
|
|
}
|
|
|
|
|
2011-06-12 12:32:28 +08:00
|
|
|
int air_modes_preamble::general_work(int noutput_items,
|
|
|
|
gr_vector_int &ninput_items,
|
2010-09-15 13:01:56 +08:00
|
|
|
gr_vector_const_void_star &input_items,
|
|
|
|
gr_vector_void_star &output_items)
|
|
|
|
{
|
2011-06-09 13:02:13 +08:00
|
|
|
const float *in = (const float *) input_items[0];
|
2010-09-15 13:01:56 +08:00
|
|
|
const float *inavg = (const float *) input_items[1];
|
2012-09-15 00:41:21 +08:00
|
|
|
|
|
|
|
int mininputs = std::min(ninput_items[0], ninput_items[1]); //they should be matched but let's be safe
|
|
|
|
//round number of input samples down to nearest d_samples_per_chip
|
|
|
|
//we also subtract off d_samples_per_chip to allow the bit center finder some leeway
|
|
|
|
const int ninputs = std::max(mininputs - (mininputs % d_samples_per_chip) - d_samples_per_chip, 0);
|
|
|
|
if (ninputs <= 0) { consume_each(0); return 0; }
|
|
|
|
|
2011-06-09 13:02:13 +08:00
|
|
|
float *out = (float *) output_items[0];
|
2010-09-15 13:01:56 +08:00
|
|
|
|
2012-08-24 00:03:56 +08:00
|
|
|
if(0) std::cout << "Preamble called with " << ninputs << " samples" << std::endl;
|
|
|
|
|
2011-06-12 12:32:28 +08:00
|
|
|
//fixme move into .h
|
2011-06-12 13:07:20 +08:00
|
|
|
const int pulse_offsets[4] = { 0,
|
|
|
|
int(2 * d_samples_per_chip),
|
|
|
|
int(7 * d_samples_per_chip),
|
|
|
|
int(9 * d_samples_per_chip)
|
2011-06-09 13:02:13 +08:00
|
|
|
};
|
|
|
|
|
2011-08-25 09:19:57 +08:00
|
|
|
uint64_t abs_sample_cnt = nitems_read(0);
|
2013-07-18 09:03:45 +08:00
|
|
|
std::vector<gr::tag_t> tstamp_tags;
|
|
|
|
get_tags_in_range(tstamp_tags, 0, abs_sample_cnt, abs_sample_cnt + ninputs, pmt::string_to_symbol("rx_time"));
|
2011-08-25 09:19:57 +08:00
|
|
|
//tags.back() is the most recent timestamp, then.
|
|
|
|
if(tstamp_tags.size() > 0) {
|
|
|
|
d_timestamp = tstamp_tags.back();
|
|
|
|
}
|
2011-09-08 06:49:14 +08:00
|
|
|
|
2011-06-12 12:32:28 +08:00
|
|
|
for(int i=0; i < ninputs; i++) {
|
2011-05-31 11:45:59 +08:00
|
|
|
float pulse_threshold = inavg[i] * d_threshold;
|
2011-06-12 12:32:28 +08:00
|
|
|
if(in[i] > pulse_threshold) { //hey we got a candidate
|
|
|
|
if(in[i+1] > in[i]) continue; //wait for the peak
|
|
|
|
//check to see the rest of the pulses are there
|
|
|
|
if( in[i+pulse_offsets[1]] < pulse_threshold ) continue;
|
|
|
|
if( in[i+pulse_offsets[2]] < pulse_threshold ) continue;
|
|
|
|
if( in[i+pulse_offsets[3]] < pulse_threshold ) continue;
|
|
|
|
|
|
|
|
//get a more accurate bit center by finding the correlation peak across all four preamble bits
|
2011-09-08 06:49:14 +08:00
|
|
|
bool late, early;
|
2012-09-15 00:41:21 +08:00
|
|
|
int how_late = 0;
|
2010-11-20 08:38:32 +08:00
|
|
|
do {
|
2011-06-12 12:32:28 +08:00
|
|
|
double now_corr = correlate_preamble(in+i, d_samples_per_chip);
|
|
|
|
double late_corr = correlate_preamble(in+i+1, d_samples_per_chip);
|
2011-09-08 06:49:14 +08:00
|
|
|
double early_corr = correlate_preamble(in+i-1, d_samples_per_chip);
|
2011-06-12 12:32:28 +08:00
|
|
|
late = (late_corr > now_corr);
|
2011-09-08 06:49:14 +08:00
|
|
|
//early = (early_corr > now_corr);
|
2012-09-15 00:41:21 +08:00
|
|
|
if(late) { i++; how_late++; }
|
2011-09-08 06:49:14 +08:00
|
|
|
//if(early && i>0) { std::cout << "EARLY " << i << std::endl; i--; }
|
2012-09-15 00:41:21 +08:00
|
|
|
} while(late and how_late < d_samples_per_chip);// xor early);
|
|
|
|
|
|
|
|
if(0) std::cout << "We were " << how_late << " samples late" << std::endl;
|
2010-11-29 09:07:09 +08:00
|
|
|
|
2011-09-08 06:49:14 +08:00
|
|
|
//now check to see that the non-peak symbols in the preamble
|
2011-06-12 12:32:28 +08:00
|
|
|
//are below the peaks by threshold dB
|
|
|
|
float avgpeak = ( in[i+pulse_offsets[0]]
|
|
|
|
+ in[i+pulse_offsets[1]]
|
|
|
|
+ in[i+pulse_offsets[2]]
|
|
|
|
+ in[i+pulse_offsets[3]]) / 4.0;
|
|
|
|
|
|
|
|
float space_threshold = inavg[i] + (avgpeak - inavg[i])/d_threshold;
|
|
|
|
bool valid_preamble = true; //f'in c++
|
|
|
|
for( int j=1.5*d_samples_per_symbol; j<=3*d_samples_per_symbol; j++)
|
|
|
|
if(in[i+j] > space_threshold) valid_preamble = false;
|
|
|
|
for( int j=5*d_samples_per_symbol; j<=7.5*d_samples_per_symbol; j++)
|
|
|
|
if(in[i+j] > space_threshold) valid_preamble = false;
|
|
|
|
if(!valid_preamble) continue;
|
|
|
|
|
|
|
|
//be sure we've got enough room in the input buffer to copy out a whole packet
|
2013-06-06 05:50:10 +08:00
|
|
|
if(ninputs-i < 240*d_samples_per_chip) {
|
2012-08-15 00:27:34 +08:00
|
|
|
consume_each(std::max(i-1,0));
|
2012-09-15 00:41:21 +08:00
|
|
|
if(0) std::cout << "Preamble consumed " << std::max(i-1,0) << ", returned 0 (no room)" << std::endl;
|
2011-06-12 12:32:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//all right i'm prepared to call this a preamble
|
|
|
|
//let's integrate and dump the output
|
2011-06-12 15:56:22 +08:00
|
|
|
//FIXME: disable and use center sample
|
2011-06-12 16:14:49 +08:00
|
|
|
bool life_sucks = true;
|
2011-06-12 16:07:22 +08:00
|
|
|
if(life_sucks) {
|
|
|
|
for(int j=0; j<240; j++) {
|
|
|
|
out[j] = in[i+j*d_samples_per_chip];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i -= d_samples_per_chip-1;
|
|
|
|
integrate_and_dump(out, &in[i], 240, d_samples_per_chip);
|
2011-06-12 15:56:22 +08:00
|
|
|
}
|
2011-06-12 12:32:28 +08:00
|
|
|
|
2011-08-25 09:19:57 +08:00
|
|
|
//get the timestamp of the preamble
|
2012-06-13 22:49:22 +08:00
|
|
|
double tstamp = tag_to_timestamp(d_timestamp, abs_sample_cnt + i, d_secs_per_sample);
|
2011-08-25 09:19:57 +08:00
|
|
|
|
2011-06-12 12:32:28 +08:00
|
|
|
//now tag the preamble
|
2010-11-21 08:34:44 +08:00
|
|
|
add_item_tag(0, //stream ID
|
2011-06-12 12:32:28 +08:00
|
|
|
nitems_written(0), //sample
|
|
|
|
d_key, //frame_info
|
2013-07-18 09:03:45 +08:00
|
|
|
pmt::from_double(tstamp),
|
2011-06-12 12:32:28 +08:00
|
|
|
d_me //block src id
|
|
|
|
);
|
2013-07-18 09:03:45 +08:00
|
|
|
|
2011-06-12 13:07:20 +08:00
|
|
|
//std::cout << "PREAMBLE" << std::endl;
|
2011-06-12 12:32:28 +08:00
|
|
|
|
2012-08-24 00:03:56 +08:00
|
|
|
//produce only one output per work call -- TODO this should probably change
|
2012-09-15 00:41:21 +08:00
|
|
|
if(0) std::cout << "Preamble consumed " << i+240*d_samples_per_chip << "with i=" << i << ", returned 240" << std::endl;
|
2012-10-08 08:54:03 +08:00
|
|
|
|
2011-06-12 12:32:28 +08:00
|
|
|
consume_each(i+240*d_samples_per_chip);
|
2011-06-12 13:07:20 +08:00
|
|
|
return 240;
|
2010-11-21 08:34:44 +08:00
|
|
|
}
|
2010-09-15 13:01:56 +08:00
|
|
|
}
|
2011-06-12 12:32:28 +08:00
|
|
|
|
|
|
|
//didn't get anything this time
|
2012-08-24 00:03:56 +08:00
|
|
|
if(0) std::cout << "Preamble consumed " << ninputs << ", returned 0" << std::endl;
|
2011-06-12 12:32:28 +08:00
|
|
|
consume_each(ninputs);
|
|
|
|
return 0;
|
2010-09-15 13:01:56 +08:00
|
|
|
}
|