summaryrefslogtreecommitdiff
path: root/gr-digital/lib/ofdm_serializer_vcc_impl.cc
blob: 9c41daae77146b88712df490bca2db3c0853e195 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* -*- c++ -*- */
/* Copyright 2012 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio 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.
 * 
 * GNU Radio 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 GNU Radio; 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 <gr_io_signature.h>
#include "ofdm_serializer_vcc_impl.h"

namespace gr {
  namespace digital {

    ofdm_serializer_vcc::sptr
    ofdm_serializer_vcc::make(
	int fft_len,
	const std::vector<std::vector<int> > &occupied_carriers,
	const std::string &len_tag_key,
	const std::string &packet_len_tag_key,
	int symbols_skipped,
	bool input_is_shifted
    )
    {
      return gnuradio::get_initial_sptr (
	  new ofdm_serializer_vcc_impl(
		      fft_len, occupied_carriers,
		      len_tag_key, packet_len_tag_key,
		      symbols_skipped, input_is_shifted
	  )
      );
    }

    ofdm_serializer_vcc::sptr
    ofdm_serializer_vcc::make(
		const digital_ofdm_carrier_allocator_cvc_sptr &allocator,
		const std::string &packet_len_tag_key,
		int symbols_skipped,
		bool input_is_shifted
    )
    {
      return gnuradio::get_initial_sptr(
	  new ofdm_serializer_vcc_impl(
	    allocator->fft_len(),
	    allocator->occupied_carriers(),
	    allocator->len_tag_key(),
	    packet_len_tag_key,
	    symbols_skipped,
	    input_is_shifted
	  )
      );
    }

    ofdm_serializer_vcc_impl::ofdm_serializer_vcc_impl (
		    int fft_len,
		    const std::vector<std::vector<int> > &occupied_carriers,
		    const std::string &len_tag_key,
		    const std::string &packet_len_tag_key,
		    int symbols_skipped,
		    bool input_is_shifted)
      : gr_tagged_stream_block ("ofdm_serializer_vcc",
		       gr_make_io_signature(1, 1, sizeof (gr_complex) * fft_len),
		       gr_make_io_signature(1, 1, sizeof (gr_complex)),
		       len_tag_key),
	    d_fft_len(fft_len),
	    d_occupied_carriers(occupied_carriers),
	    d_packet_len_tag_key(pmt::string_to_symbol(packet_len_tag_key)),
	    d_out_len_tag_key(pmt::string_to_symbol((packet_len_tag_key.empty() ? len_tag_key : packet_len_tag_key))),
	    d_symbols_skipped(symbols_skipped % occupied_carriers.size()),
	    d_curr_set(symbols_skipped % occupied_carriers.size()),
	    d_symbols_per_set(0)
    {
      for (unsigned i = 0; i < d_occupied_carriers.size(); i++) {
	for (unsigned k = 0; k < d_occupied_carriers[i].size(); k++) {
	  if (d_occupied_carriers[i][k] < 0) {
	    d_occupied_carriers[i][k] += fft_len;
	  }
	  if (d_occupied_carriers[i][k] >= fft_len || d_occupied_carriers[i][k] < 0) {
	    throw std::invalid_argument("ofdm_serializer_vcc: trying to occupy a carrier outside the fft length.");
	  }
	  if (input_is_shifted) {
	    d_occupied_carriers[i][k] = (d_occupied_carriers[i][k] + fft_len) % fft_len;
	  }
	}
      }

      for (unsigned i = 0; i < d_occupied_carriers.size(); i++) {
	d_symbols_per_set += d_occupied_carriers[i].size();
      }
      set_relative_rate((double) d_symbols_per_set / d_occupied_carriers.size());
      set_tag_propagation_policy(TPP_DONT);
    }

    ofdm_serializer_vcc_impl::~ofdm_serializer_vcc_impl()
    {
    }

    int
    ofdm_serializer_vcc_impl::calculate_output_stream_length(const gr_vector_int &ninput_items)
    {
      int nout = (ninput_items[0] / d_occupied_carriers.size()) * d_symbols_per_set;
      for (unsigned i = 0; i < ninput_items[0] % d_occupied_carriers.size(); i++) {
	nout += d_occupied_carriers[(i + d_curr_set) % d_occupied_carriers.size()].size();
      }
      return nout;
    }

    void
    ofdm_serializer_vcc_impl::update_length_tags(int n_produced, int n_ports)
    {
      add_item_tag(0, nitems_written(0),
	d_out_len_tag_key,
	pmt::from_long(n_produced)
      );
    }

    int
    ofdm_serializer_vcc_impl::work (int noutput_items,
                       gr_vector_int &ninput_items,
                       gr_vector_const_void_star &input_items,
                       gr_vector_void_star &output_items)
    {
      const gr_complex *in = (const gr_complex *) input_items[0];
      gr_complex *out = (gr_complex *) output_items[0];
      long frame_length = ninput_items[0]; // Input frame
      long packet_length = 0; // Output frame
      int carr_offset = 0;

      std::vector<gr_tag_t> tags;
      // Packet mode
      if (!d_length_tag_key_str.empty()) {
	get_tags_in_range(tags, 0, nitems_read(0), nitems_read(0)+1);
	for (unsigned i = 0; i < tags.size(); i++) {
	  if (pmt::symbol_to_string(tags[i].key) == "ofdm_sync_carr_offset") {
	    carr_offset = pmt::to_long(tags[i].value);
	  }
	  if (tags[i].key == d_packet_len_tag_key) {
	    packet_length = pmt::to_long(tags[i].value);
	    remove_item_tag(0, tags[i]);
	  }
	}
      } else {
	// recalc frame length from noutput_items
	frame_length = 0;
	int sym_per_frame = 0;
	while ((sym_per_frame + d_occupied_carriers[(frame_length + 1) % d_occupied_carriers.size()].size()) < noutput_items) {
	  frame_length++;
	  sym_per_frame += d_occupied_carriers[(frame_length + 1) % d_occupied_carriers.size()].size();
	}
      }

      // Copy symbols
      int n_out_symbols = 0;
      for (int i = 0; i < frame_length; i++) {
	// Copy all tags associated with this input OFDM symbol onto the first output symbol
	get_tags_in_range(tags, 0,
	  nitems_read(0)+i,
	  nitems_read(0)+i+1
	);
	for (unsigned t = 0; t < tags.size(); t++) {
	  add_item_tag(0, nitems_written(0)+n_out_symbols,
	    tags[i].key,
	    tags[i].value
	  );
	}
	for (unsigned k = 0; k < d_occupied_carriers[d_curr_set].size(); k++) {
	  out[n_out_symbols++] = in[i * d_fft_len + d_occupied_carriers[d_curr_set][k] + carr_offset];
	}
	if (packet_length && n_out_symbols > packet_length) {
	  n_out_symbols = packet_length;
	  break;
	}
	d_curr_set = (d_curr_set + 1) % d_occupied_carriers.size();
      }

      // Housekeeping
      if (d_length_tag_key_str.empty()) {
	consume_each(frame_length);
      } else {
	d_curr_set = d_symbols_skipped;
      }

      return n_out_symbols;
    }

  } /* namespace digital */
} /* namespace gr */