summaryrefslogtreecommitdiff
path: root/gr-digital/lib/digital_ofdm_carrier_allocator_cvc.cc
blob: e5b3013302d7119a0bd195331cb2b1d0b6488b7a (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
/* -*- 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 "digital_ofdm_carrier_allocator_cvc.h"

digital_ofdm_carrier_allocator_cvc_sptr
digital_make_ofdm_carrier_allocator_cvc (
		int fft_len,
		const std::vector<std::vector<int> > &occupied_carriers,
		const std::vector<std::vector<int> > &pilot_carriers,
		const std::vector<std::vector<gr_complex> > &pilot_symbols,
		const std::string &len_tag_key)
{
  return gnuradio::get_initial_sptr (new digital_ofdm_carrier_allocator_cvc(fft_len, occupied_carriers, pilot_carriers, pilot_symbols, len_tag_key));
}


digital_ofdm_carrier_allocator_cvc::digital_ofdm_carrier_allocator_cvc (
		int fft_len,
		const std::vector<std::vector<int> > &occupied_carriers,
		const std::vector<std::vector<int> > &pilot_carriers,
		const std::vector<std::vector<gr_complex> > &pilot_symbols,
		const std::string &len_tag_key)
  : gr_tagged_stream_block ("ofdm_carrier_allocator_cvc",
		   gr_make_io_signature(1, 1, sizeof (gr_complex)),
		   gr_make_io_signature(1, 1, sizeof (gr_complex) * fft_len), len_tag_key),
	d_fft_len(fft_len),
	d_occupied_carriers(occupied_carriers),
	d_pilot_carriers(pilot_carriers),
	d_pilot_symbols(pilot_symbols),
	d_symbols_per_set(0)
{
  for (unsigned i = 0; i < d_occupied_carriers.size(); i++) {
    for (unsigned j = 0; j < d_occupied_carriers[i].size(); j++) {
      if (occupied_carriers[i][j] < 0) {
	d_occupied_carriers[i][j] += d_fft_len;
      }
      if (d_occupied_carriers[i][j] > d_fft_len || d_occupied_carriers[i][j] < 0) {
      	throw std::invalid_argument("data carrier index out of bounds");
      }
    }
  }
  for (unsigned i = 0; i < d_pilot_carriers.size(); i++) {
    if (d_pilot_carriers[i].size() != pilot_symbols[i].size()) {
      throw std::invalid_argument("pilot_carriers do not match pilot_symbols");
    }
    for (unsigned j = 0; j < d_pilot_carriers[i].size(); j++) {
      if (d_pilot_carriers[i][j] < 0) {
	d_pilot_carriers[i][j] += d_fft_len;
      }
      if (d_pilot_carriers[i][j] > d_fft_len || d_pilot_carriers[i][j] < 0) {
      	throw std::invalid_argument("pilot carrier index out of bounds");
      }
    }
  }

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


digital_ofdm_carrier_allocator_cvc::~digital_ofdm_carrier_allocator_cvc()
{
}


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


int
digital_ofdm_carrier_allocator_cvc::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];

  std::vector<gr_tag_t> tags;

  memset((void *) out, 0x00, sizeof(gr_complex) * d_fft_len * noutput_items);
  long n_ofdm_symbols = 0;
  int curr_set = 0;
  int symbols_to_allocate = d_occupied_carriers[0].size();
  int symbols_allocated = 0;
  for (int i = 0; i < ninput_items[0]; i++) {
    if (symbols_allocated == 0) {
      // Copy all tags associated with these input symbols onto this OFDM symbol
      get_tags_in_range(tags, 0,
	  nitems_read(0)+i,
	  nitems_read(0)+std::min(i+symbols_to_allocate, (int) ninput_items[0])
      );
      for (unsigned t = 0; t < tags.size(); t++) {
	add_item_tag(0, nitems_written(0)+n_ofdm_symbols,
	    tags[t].key,
	    tags[t].value);
      }
      n_ofdm_symbols++;
    }
    out[(n_ofdm_symbols-1) * d_fft_len + d_occupied_carriers[curr_set][symbols_allocated]] = in[i];
    symbols_allocated++;
    if (symbols_allocated == symbols_to_allocate) {
      curr_set = (curr_set + 1) % d_occupied_carriers.size();
      symbols_to_allocate = d_occupied_carriers[curr_set].size();
      symbols_allocated = 0;
    }
  }
  // 2) Copy pilot symbols
  curr_set = 0;
  for (int i = 0; i < n_ofdm_symbols; i++) {
    for (unsigned k = 0; k < d_pilot_carriers[curr_set].size(); k++) {
      out[i * d_fft_len + d_pilot_carriers[curr_set][k]] = d_pilot_symbols[curr_set][k];
    }
    curr_set = (curr_set + 1) % d_pilot_carriers.size();
  }

  return n_ofdm_symbols;
}