IT++ Logo
spread.cpp
Go to the documentation of this file.
1 
29 #include <itpp/comm/spread.h>
30 #include <itpp/base/converters.h>
31 #include <itpp/stat/misc_stat.h>
32 
33 
34 namespace itpp
35 {
36 
37 //------------- Spread1d -------------------
38 
39 Spread_1d::Spread_1d(const vec &incode)
40 {
41  set_code(incode);
42 }
43 
44 void Spread_1d::set_code(const vec &incode)
45 {
46  N = incode.size();
47  code = incode;
48  code /= norm(code);
49 }
50 
52 {
53  return code;
54 }
55 
56 void Spread_1d::spread(const vec &symbols, vec &out)
57 {
58  out.set_size(symbols.length()*N, false);
59 
60  for (int i = 0;i < symbols.length();i++)
61  out.replace_mid(i*N, symbols(i)*code);
62 }
63 
64 void Spread_1d::despread(const vec &rec_signal, vec &out, int timing)
65 {
66  int nosymbols = (int)std::floor(double((rec_signal.length() - timing)) / N);
67  out.set_size(nosymbols);
68 
69  for (int i = 0;i < nosymbols;i++)
70  out(i) = rec_signal.mid(i * N + timing, N) * code;
71 }
72 
73 
74 //---------------- Spread2d ----------------------
75 
77 {
78  return spreadI.get_code();
79 }
80 
82 {
83  return spreadQ.get_code();
84 }
85 
86 Spread_2d::Spread_2d(const vec &incodeI, const vec &incodeQ)
87 {
88  set_code(incodeI, incodeQ);
89 }
90 
91 void Spread_2d::set_code(const vec &incodeI, const vec &incodeQ)
92 {
93  it_assert(incodeI.length() == incodeQ.length(), "Size of I and Q codes doesn't match");
94  spreadI.set_code(incodeI);
95  spreadQ.set_code(incodeQ);
96 }
97 
98 void Spread_2d::spread(const cvec &symbols, cvec &out)
99 {
100  out = to_cvec(spreadI.spread(real(symbols)), spreadQ.spread(imag(symbols)));
101 }
102 
103 void Spread_2d::despread(const cvec &rec_signal, cvec &out, int timing)
104 {
105  out = to_cvec(spreadI.despread(real(rec_signal), timing), spreadQ.despread(imag(rec_signal), timing));
106 }
107 
108 
109 
110 //------------- Multicode_Spread_1d ----------------
111 
112 
114 {
115  set_codes(incodes);
116 }
117 
118 void Multicode_Spread_1d::set_codes(const mat &incodes)
119 {
120  codes = incodes;
121  N = incodes.cols();
122  L = incodes.rows();
123  for (int i = 0; i < L; i++)
124  codes.set_row(i, codes.get_row(i) / norm(codes.get_row(i)));
125 }
126 
128 {
129  return codes;
130 }
131 
132 vec Multicode_Spread_1d::spread(const vec &symbols)
133 {
134  int i;
135  int nomcsymbols = (int)std::floor(double(symbols.length() / L));
136  vec temp(nomcsymbols*N);
137 
138  for (i = 0;i < nomcsymbols;i++) {
139  temp.replace_mid(i*N, codes.T() * symbols.mid(i*L, L)); // TODO: this is now very slow
140  }
141 
142  return temp;
143 }
144 
145 vec Multicode_Spread_1d::despread(const vec &receivedsignal, int timing)
146 {
147  int i;
148  int nosymbols = (int)std::floor(double((receivedsignal.length() - timing)) / N);
149  vec temp(nosymbols*L);
150 
151  for (i = 0;i < nosymbols;i++) {
152  temp.replace_mid(i*L, codes*receivedsignal.mid(i*N + timing, N));
153  }
154  return temp;
155 }
156 
157 
158 //----------------- Multicode_Spread_2d -------------------
159 
160 
161 Multicode_Spread_2d::Multicode_Spread_2d(const mat &incodesI, const mat &incodesQ)
162 {
163  set_codes(incodesI, incodesQ);
164 }
165 
167 {
168  return mcspreadI.get_codes();
169 }
170 
172 {
173  return mcspreadQ.get_codes();
174 }
175 
176 void Multicode_Spread_2d::set_codes(const mat &incodesI, const mat &incodesQ)
177 {
178  it_assert(incodesI.rows() == incodesQ.rows() && incodesI.cols() == incodesQ.cols(),
179  "Multicode_Spread_2d::set_codes(): dimension mismatch");
180  mcspreadI.set_codes(incodesI);
181  mcspreadQ.set_codes(incodesQ);
182 }
183 
184 cvec Multicode_Spread_2d::spread(const cvec &symbols)
185 {
186  return to_cvec(mcspreadI.spread(real(symbols)), mcspreadQ.spread(imag(symbols)));
187 }
188 
189 cvec Multicode_Spread_2d::despread(const cvec &receivedsignal, int timing)
190 {
191  return to_cvec(mcspreadI.despread(real(receivedsignal), timing), mcspreadQ.despread(imag(receivedsignal), timing));
192 }
193 
194 } // namespace itpp
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:23 for IT++ by Doxygen 1.8.2