Monday, February 9, 2015

Re: [Discuss-gnuradio] Simple Frame Detection

/* -*- c++ -*- */
/*
* Copyright 2015 <+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.
*/

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

#include <gnuradio/io_signature.h>
#include "deinterleaver_bb_impl.h"

namespace gr {
namespace frame_detection {

deinterleaver_bb::sptr
deinterleaver_bb::make(int n, int m, int message_length)
{
return gnuradio::get_initial_sptr
(new deinterleaver_bb_impl(n, m, message_length));
}

/*
* The private constructor
*/
deinterleaver_bb_impl::deinterleaver_bb_impl(int n, int m, int message_length)
: gr::block("deinterleaver_bb",
gr::io_signature::make(1, 1, sizeof(char)),
gr::io_signature::make(1, 1, sizeof(char)))
{
i_n = n;
i_m = m;
counter = 0;
n_count = 0;
m_count = 0;
buffer = new char[n*m];
buffer2 = new char[n*m];
buf_count = 0;
frame_detected = false;
end_of_frame = false;
eof_pos = 0;
m_length = message_length;
}

/*
* Our virtual destructor.
*/
deinterleaver_bb_impl::~deinterleaver_bb_impl()
{
delete[] buffer;
delete[] buffer2;
}

void
deinterleaver_bb_impl::forecast (int noutput_items, gr_vector_int &ninput_items_required)
{
ninput_items_required[0] = noutput_items;
}

int
deinterleaver_bb_impl::general_work (int noutput_items,
gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
const char *in = (const char *) input_items[0];
char *out = (char *) output_items[0];

int n = ninput_items[0];
int i = 0;
int msg_count = 0;

while (n > 0)
{
if(!frame_detected)
{
if(in[i] != char(0))
{
frame_detected = true;
end_of_frame = false;
}

out[i] = 0;

consume_each(1);
n--;
i++;
}

else
{
if(msg_count == m_length)
{
end_of_frame = true;
eof_pos = i;
}

if ((i == eof_pos + i_n*i_m) && (end_of_frame))
{
out[i++] = 0;
n--;
consume_each(1);
frame_detected = false;
}

else
{
if(counter < i_n*i_m)
{
counter++;
out[i] = 0;
}
else
{
out[i] = buffer2[buf_count++];
n--;
consume_each(1);
}

if (buf_count >= i_n*i_m)
buf_count = 0;

buffer[n_count + i_m*m_count] = in[i];

msg_count++;

if (n_count + i_m*m_count == i_n*i_m - 1)
{
for(int j = 0; j < i_n*i_m; j++)
buffer2[j] = buffer[j];
}

if(m_count < i_n - 1)
m_count++;
else
{
m_count = 0;
n_count++;
}

if(n_count == i_m)
n_count = 0;

i++;
}
}
}

// Tell runtime system how many output items we produced.
return i;
}

} /* namespace frame_detection */
} /* namespace gr */

/* -*- c++ -*- */
/*
* Copyright 2015 <+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_FRAME_DETECTION_DEINTERLEAVER_BB_IMPL_H
#define INCLUDED_FRAME_DETECTION_DEINTERLEAVER_BB_IMPL_H

#include <frame_detection/deinterleaver_bb.h>

namespace gr {
namespace frame_detection {

class deinterleaver_bb_impl : public deinterleaver_bb
{
private:
int i_n;
int i_m;
int counter;
int n_count;
int m_count;
char* buffer;
char* buffer2;
int buf_count;
bool frame_detected;
bool end_of_frame;
int eof_pos;
int m_length;

public:
deinterleaver_bb_impl(int n, int m, int message_length);
~deinterleaver_bb_impl();

// Where all the action really happens
void forecast (int noutput_items, gr_vector_int &ninput_items_required);

int general_work(int noutput_items,
gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
};

} // namespace frame_detection
} // namespace gr

#endif /* INCLUDED_FRAME_DETECTION_DEINTERLEAVER_BB_IMPL_H */

/* -*- c++ -*- */
/*
* Copyright 2015 <+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.
*/

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

#include <gnuradio/io_signature.h>
#include "preamble_detector_bb_impl.h"
#include <cmath>
#include <iostream>
using namespace std;

namespace gr {
namespace frame_detection {

preamble_detector_bb::sptr
preamble_detector_bb::make(const std::vector<int> &preamble, int preamble_length, int message_length, int i_size)
{
return gnuradio::get_initial_sptr
(new preamble_detector_bb_impl(preamble, preamble_length, message_length, i_size));
}

/*
* The private constructor
*/
preamble_detector_bb_impl::preamble_detector_bb_impl(const std::vector<int> &preamble,
int preamble_length,
int message_length,
int i_size)
: gr::block("preamble_detector_bb",
gr::io_signature::make(1, 1, sizeof(char)),
gr::io_signature::make(1, 1, sizeof(char)))
{
sequence = new char[preamble_length];
sequence_length = preamble_length;
for(int i = 0; i < preamble_length; i++)
sequence[i] = char(preamble[i]);
sequence_detected = false;
counter = 0;
message_counter = 0;
m_length = message_length;
item_size = i_size;
}

/*
* Our virtual destructor.
*/
preamble_detector_bb_impl::~preamble_detector_bb_impl()
{
delete[] sequence;
}

int
preamble_detector_bb_impl::general_work(int noutput_items,
gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
const char *in = (const char *) input_items[0];
char *out = (char *) output_items[0];
int n = ninput_items[0];
int i = 0;
bool thresh_sent = false;
int thresh_count = 0;
int zero_count = 0;
int history = 0;

while(n > 0)
{
if(!thresh_sent)
{
if(!sequence_detected)
{
if(in[i - history] == sequence[counter])
{
counter++;
}
else
counter = 0;

if(counter == sequence_length)
{
out[i] = char(255);
sequence_detected = true;
counter = 0;
thresh_count++;
}
else
{
out[i] = 0;
zero_count++;
if(zero_count == item_size)
zero_count = 0;
}

n--;
}
else
{

if(thresh_count < (item_size - zero_count))
{
thresh_count++;
out[i] = char(255);
}
else
{
thresh_sent = true;
sequence_detected = false;
thresh_count = 0;
out[i] = in[i - (history + item_size - zero_count) + 1];
message_counter++;
n--;
}
}

}
else
{
out[i] = in[i - (history + item_size - zero_count) + 1];

if(message_counter < (m_length - 1))
{
message_counter++;
}
else
{
thresh_sent = false;
history += item_size - zero_count - 1;
zero_count = 0;
message_counter = 0;
}

n--;

}

i++;
}

consume_each(ninput_items[0]);

return i;
}

} /* namespace frame_detection */
} /* namespace gr */

/* -*- c++ -*- */
/*
* Copyright 2015 <+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_FRAME_DETECTION_PREAMBLE_DETECTOR_BB_IMPL_H
#define INCLUDED_FRAME_DETECTION_PREAMBLE_DETECTOR_BB_IMPL_H

#include <frame_detection/preamble_detector_bb.h>

namespace gr {
namespace frame_detection {

class preamble_detector_bb_impl : public preamble_detector_bb
{
private:
char* sequence;
int sequence_length;
bool sequence_detected;
int counter;
int message_counter;
int m_length;
int item_size;

public:
preamble_detector_bb_impl(const std::vector<int> &preamble,
int preamble_length,
int message_length,
int item_size);
~preamble_detector_bb_impl();

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

} // namespace frame_detection
} // namespace gr

#endif /* INCLUDED_FRAME_DETECTION_PREAMBLE_DETECTOR_BB_IMPL_H */

Actually, I am not using OFDM. What I wish to do is way more simple than OFDM, but at the same time there seems to be little to no documentation on how to do it.  I will attach a screenshot of how my system is supposed to be. 

The blocks I've programmed are the interleaver, the preamble detector and the deinterleaver. These last two are a tentative solution to the problem I'm having, but they still don't work as expected. I gave up on trying to discard previous samples, as I feel this is not in the nature of GNURadio and its flow of samples. I've decided that something that outputs 0 while it is not synchronized is good enough for me. Specially because later on I will test this system with a USRP, substituting that channel model by UHD source and sink. 

The interleaver fills an N*M matrix line by line and once it is full, sends it column by column.

The deinterleaver works almost exactly the same but, for synchronization reasons, waits for a threshold signal different from zero to start its process. Once it starts it, it will descramble a number of samples equal to message length and then go back to a state where it waits for a threshold signal. When it is not deinterleaving, it outputs zero.

The preamble detector is the block responsible for sending that threshold signal. It starts in a state where it is waiting to detect the sent preamble. Not with correlation. Simpler stuff. It just compares the input to the sequence. Each time there is a match, it increments a counter. Once the counter has a value equal to the preamble length, it will start sending the threshold signal. As there is a Pack K Bits block after the preamble detector, I had to do some adaptations on this threshold signal. The threshold signal has to be just one sample different from zero so the deinterleaver can start working. As the output of the detector will be packed in groups of 8 (the item size parameter), it sends 8 samples of 255 (the threshold signal) that will be packed together to form just one threshold sample. Once the threshold signal is sent, the detector will send a number of samples of 8 * message length, as they will be packed together and form a proper message.

In order to send a threshold sequence of the proper size, the preamble detector counts the number of zeroes it sends and adapts the threshold sequence appropriately. For example: the item size is 8 and in case it has sent 18 zeroes while detecting a preamble sequence. In order to produce only one packed sample different from zero, it will send 6 threshold samples. The total output sequence will then be 24 samples long, that will be packed in 3 samples, where the first two will be zeroes but the last one will be different than zero and serve as threshold for the deinterleaver.

I had to place the detector where it is, because if it was placed after the pack K bits block, the output from the detector would sometimes be misunderstood as threshold by the deinterleaver. I've tried it before and the results were horrible even in some tests.

These setup I have now went really well in the unit tests. But once I've tried to plug it in the whole system, the output is unpredictable. It usually outputs lots of garbage. Sometimes it outputs the original message in a scrambled way and rarely it outputs the correct message.

I will attach the files for the preamble detector and the deinterleaver. They are not well documented, sorry. I have been desperately working on this during the weekend with barely no sleep, so the current form is the result of lots of desperate work.

The codes and flowgraphs for the rest of the system can be found on my github page, in the gnuradio folder: https://github.com/franchenstein/tcc 

2015-02-08 14:27 GMT-02:00 Brian Padalino <bpadalino@gmail.com>:
On Sun, Feb 8, 2015 at 11:01 AM, Aditya Dhananjay <aditya@cs.nyu.edu> wrote:
> Hello Daniel,
>
> Are you using OFDM? If so, read on, otherwise disregard the rest of the
> email.

This sounds much less like a problem with regards on how to figure out
the synchronization but more about how to do it inside the confines of
GNU Radio.

The original post wants to discard everything before a preamble they
detect.  Once preamble is detected, I assume a state machine down
stream will decode some information in the samples coming down which
says "The length of this transmission is 12345 symbols".  At this
time, the downstream block will send a message to the upstream block -
holding off the samples until it knows how many more to send down -
and finish up.

The feedback loop cannot be realized inside the flowgraph since
everything needs to be feed forward.

This is a great example of how someone may want to do RF burst
processing which may not necessarily be "real-time" but due to
dead-air or guard bands might be able to catch up on the processing if
you didn't have to keep processing every last incoming sample -
especially at a higher sample rate.  Moreover, it shows how blocks
could be used to message each other to create feedback loops and
convey downstream information to upstream processing elements which
may care - things like SNR estimation, timing information, etc.

I'm very interested to see how this all ends up.

Brian

No comments:

Post a Comment