summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Rondeau2010-11-21 19:40:35 -0500
committerTom Rondeau2010-11-21 19:40:35 -0500
commit8f2b07591c404c78c9fc7b9532d3fb6c27165981 (patch)
tree02d51d3734e3f1221a887250886d79ea1e5bbadd
parenta9777449322e6784f4e67c47d8300cef8d87dbc6 (diff)
downloadgnuradio-8f2b07591c404c78c9fc7b9532d3fb6c27165981.tar.gz
gnuradio-8f2b07591c404c78c9fc7b9532d3fb6c27165981.tar.bz2
gnuradio-8f2b07591c404c78c9fc7b9532d3fb6c27165981.zip
Tagging file source takes in sample rate and uses it to find the last time tag and adjust the time between these tags and the burst start by the sample rate.
Also added a function to gr_tag_info that can be used to sort tags based on nitems using std::sort.
-rw-r--r--gnuradio-core/src/lib/io/gr_tagged_file_sink.cc77
-rw-r--r--gnuradio-core/src/lib/io/gr_tagged_file_sink.h13
-rw-r--r--gnuradio-core/src/lib/io/gr_tagged_file_sink.i4
-rw-r--r--gnuradio-core/src/lib/runtime/gr_tag_info.h20
4 files changed, 81 insertions, 33 deletions
diff --git a/gnuradio-core/src/lib/io/gr_tagged_file_sink.cc b/gnuradio-core/src/lib/io/gr_tagged_file_sink.cc
index 9b5ae70a6..500a359b3 100644
--- a/gnuradio-core/src/lib/io/gr_tagged_file_sink.cc
+++ b/gnuradio-core/src/lib/io/gr_tagged_file_sink.cc
@@ -49,33 +49,27 @@
#endif
-gr_tagged_file_sink::gr_tagged_file_sink (size_t itemsize)
+gr_tagged_file_sink::gr_tagged_file_sink (size_t itemsize, double samp_rate)
: gr_sync_block ("tagged_file_sink",
gr_make_io_signature (1, 1, itemsize),
gr_make_io_signature (0, 0, 0)),
- d_itemsize (itemsize),d_n(0)
+ d_itemsize (itemsize), d_n(0), d_sample_rate(samp_rate)
{
d_state = NOT_IN_BURST;
+ d_last_N = 0;
+ d_timeval = 0;
}
gr_tagged_file_sink_sptr
-gr_make_tagged_file_sink (size_t itemsize)
+gr_make_tagged_file_sink (size_t itemsize, double samp_rate)
{
- return gnuradio::get_initial_sptr(new gr_tagged_file_sink (itemsize));
+ return gnuradio::get_initial_sptr(new gr_tagged_file_sink (itemsize, samp_rate));
}
gr_tagged_file_sink::~gr_tagged_file_sink ()
{
}
-bool pmtcompare(pmt::pmt_t x, pmt::pmt_t y)
-{
- uint64_t t_x, t_y;
- t_x = pmt::pmt_to_uint64(pmt::pmt_tuple_ref(x, 0));
- t_y = pmt::pmt_to_uint64(pmt::pmt_tuple_ref(y, 0));
- return t_x < t_y;
-}
-
int
gr_tagged_file_sink::work (int noutput_items,
gr_vector_const_void_star &input_items,
@@ -86,13 +80,12 @@ gr_tagged_file_sink::work (int noutput_items,
uint64_t start_N = nitems_read(0);
uint64_t end_N = start_N + (uint64_t)(noutput_items);
pmt::pmt_t bkey = pmt::pmt_string_to_symbol("burst");
- //pmt::pmt_t tkey = pmt::pmt_string_to_symbol("time"); // use gr_tags::s_key_time
+ //pmt::pmt_t tkey = pmt::pmt_string_to_symbol("time"); // use gr_tags::key_time
std::vector<pmt::pmt_t> all_tags;
get_tags_in_range(all_tags, 0, start_N, end_N);
- std::sort(all_tags.begin(), all_tags.end(), pmtcompare);
- std::cout << "Number of tags: " << all_tags.size() << std::endl;
+ std::sort(all_tags.begin(), all_tags.end(), gr_tags::nitems_compare);
std::vector<pmt::pmt_t>::iterator vitr = all_tags.begin();
@@ -100,18 +93,53 @@ gr_tagged_file_sink::work (int noutput_items,
while(idx < noutput_items) {
if(d_state == NOT_IN_BURST) {
while(vitr != all_tags.end()) {
- //std::cout << "\tNot in burst: " << *vitr << std::endl;
-
if((pmt::pmt_eqv(gr_tags::get_key(*vitr), bkey)) &&
pmt::pmt_is_true(gr_tags::get_value(*vitr))) {
- uint64_t N = gr_tags::get_nitems(*vitr) - start_N;
- idx = (int)N;
-
- std::cout << std::endl << "Found start of burst: " << N << ", " << N+start_N << std::endl;
+ uint64_t N = gr_tags::get_nitems(*vitr);
+ idx = (int)(N - start_N);
+
+ std::cout << std::endl << "Found start of burst: "
+ << idx << ", " << N << std::endl;
+
+ // Find time burst occurred by getting latest time tag and extrapolating
+ // to new time based on sample rate of this block.
+ std::vector<pmt::pmt_t> time_tags;
+ get_tags_in_range(time_tags, 0, d_last_N, N, gr_tags::key_time);
+ if(time_tags.size() > 0) {
+ pmt::pmt_t tag = time_tags[time_tags.size()-1];
+
+ uint64_t time_nitems = gr_tags::get_nitems(tag);
+
+ // Get time based on last time tag from USRP
+ pmt::pmt_t time = gr_tags::get_value(tag);
+ int tsecs = pmt::pmt_to_long(pmt::pmt_tuple_ref(time, 0));
+ double tfrac = pmt::pmt_to_double(pmt::pmt_tuple_ref(time, 1));
+
+ // Get new time from last time tag + difference in time to when
+ // burst tag occured based on the sample rate
+ double delta = (double)(N - time_nitems) / d_sample_rate;
+ d_timeval = (double)tsecs + tfrac + delta;
+
+ std::cout.setf(std::ios::fixed, std::ios::floatfield);
+ std::cout.precision(8);
+ std::cout << "Time found: " << (double)tsecs + tfrac << std::endl;
+ std::cout << " time: " << d_timeval << std::endl;
+ std::cout << " time at N = " << time_nitems << " burst N = " << N << std::endl;
+ }
+ else {
+ // if no time tag, use last seen tag and update time based on
+ // sample rate of the block
+ d_timeval += (double)(N - d_last_N) / d_sample_rate;
+ std::cout << "Time not found" << std::endl;
+ std::cout << " time: " << d_timeval << std::endl;
+ }
+ d_last_N = N;
std::stringstream filename;
- filename << "file" << d_n << ".dat";
+ filename.setf(std::ios::fixed, std::ios::floatfield);
+ filename.precision(8);
+ filename << "file" << d_n << "_" << d_timeval << ".dat";
d_n++;
int fd;
@@ -142,15 +170,11 @@ gr_tagged_file_sink::work (int noutput_items,
}
else { // In burst
while(vitr != all_tags.end()) {
- //std::cout << "\tin burst: " << *vitr << std::endl;
-
if((pmt::pmt_eqv(gr_tags::get_key(*vitr), bkey)) &&
pmt::pmt_is_false(gr_tags::get_value(*vitr))) {
uint64_t N = gr_tags::get_nitems(*vitr) - start_N;
idx_stop = (int)N;
- std::cout << "Found end of burst: " << N << ", " << N+start_N << std::endl;
-
int count = fwrite (&inbuf[d_itemsize*idx], d_itemsize, idx_stop-idx, d_handle);
if (count == 0)
break;
@@ -164,7 +188,6 @@ gr_tagged_file_sink::work (int noutput_items,
}
}
if(d_state == IN_BURST) {
- std::cout << "writing part of burst: " << noutput_items-idx << std::endl;
int count = fwrite (&inbuf[idx], d_itemsize, noutput_items-idx, d_handle);
if (count == 0)
break;
diff --git a/gnuradio-core/src/lib/io/gr_tagged_file_sink.h b/gnuradio-core/src/lib/io/gr_tagged_file_sink.h
index c657e6a75..956340f8d 100644
--- a/gnuradio-core/src/lib/io/gr_tagged_file_sink.h
+++ b/gnuradio-core/src/lib/io/gr_tagged_file_sink.h
@@ -28,7 +28,8 @@
class gr_tagged_file_sink;
typedef boost::shared_ptr<gr_tagged_file_sink> gr_tagged_file_sink_sptr;
-gr_tagged_file_sink_sptr gr_make_tagged_file_sink (size_t itemsize);
+gr_tagged_file_sink_sptr gr_make_tagged_file_sink (size_t itemsize,
+ double samp_rate);
/*!
* \brief Write stream to file descriptor.
@@ -37,7 +38,8 @@ gr_tagged_file_sink_sptr gr_make_tagged_file_sink (size_t itemsize);
class gr_tagged_file_sink : public gr_sync_block
{
- friend gr_tagged_file_sink_sptr gr_make_tagged_file_sink (size_t itemsize);
+ friend gr_tagged_file_sink_sptr gr_make_tagged_file_sink (size_t itemsize,
+ double samp_rate);
private:
enum {
@@ -49,9 +51,12 @@ class gr_tagged_file_sink : public gr_sync_block
int d_state;
FILE *d_handle;
int d_n;
-
+ double d_sample_rate;
+ uint64_t d_last_N;
+ double d_timeval;
+
protected:
- gr_tagged_file_sink (size_t itemsize);
+ gr_tagged_file_sink (size_t itemsize, double samp_rate);
public:
~gr_tagged_file_sink ();
diff --git a/gnuradio-core/src/lib/io/gr_tagged_file_sink.i b/gnuradio-core/src/lib/io/gr_tagged_file_sink.i
index 92248e5dd..1408adfc1 100644
--- a/gnuradio-core/src/lib/io/gr_tagged_file_sink.i
+++ b/gnuradio-core/src/lib/io/gr_tagged_file_sink.i
@@ -23,12 +23,12 @@
GR_SWIG_BLOCK_MAGIC(gr,tagged_file_sink)
gr_tagged_file_sink_sptr
-gr_make_tagged_file_sink (size_t itemsize);
+gr_make_tagged_file_sink (size_t itemsize, double samp_rate);
class gr_tagged_file_sink : public gr_sync_block
{
protected:
- gr_tagged_file_sink (size_t itemsize);
+ gr_tagged_file_sink (size_t itemsize, double samp_rate);
public:
~gr_tagged_file_sink ();
diff --git a/gnuradio-core/src/lib/runtime/gr_tag_info.h b/gnuradio-core/src/lib/runtime/gr_tag_info.h
index 0ba699a0e..5ed30a815 100644
--- a/gnuradio-core/src/lib/runtime/gr_tag_info.h
+++ b/gnuradio-core/src/lib/runtime/gr_tag_info.h
@@ -40,26 +40,46 @@ namespace gr_tags {
extern const pmt::pmt_t key_rssi;
extern const pmt::pmt_t key_gain;
+ /*!
+ * \brief Returns the item \p tag occurred at (as a uint64_t)
+ */
static inline uint64_t
get_nitems(const pmt::pmt_t &tag) {
return pmt::pmt_to_uint64(pmt::pmt_tuple_ref(tag, TAG_NITEM_REF));
}
+ /*!
+ * \brief Returns the source ID of \p tag (as a PMT)
+ */
static inline pmt::pmt_t
get_srcid(const pmt::pmt_t &tag) {
return pmt::pmt_tuple_ref(tag, TAG_SRCID_REF);
}
+ /*!
+ * \brief Returns the key of \p tag (as a PMT symbol)
+ */
static inline pmt::pmt_t
get_key(const pmt::pmt_t &tag) {
return pmt::pmt_tuple_ref(tag, TAG_KEY_REF);
}
+ /*!
+ * \brief Returns the value of \p tag (as a PMT)
+ */
static inline pmt::pmt_t
get_value(const pmt::pmt_t &tag) {
return pmt::pmt_tuple_ref(tag, TAG_VALUE_REF);
}
+ /*!
+ * \brief Comparison function to test which tag, \p x or \p y, came first in time
+ */
+ static inline bool
+ nitems_compare(pmt::pmt_t x, pmt::pmt_t y) {
+ return get_nitems(x) < get_nitems(y);
+ }
+
}; /* namespace tags */
#endif /* GR_TAG_INFO */