OLD | NEW |
(Empty) | |
| 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2009 Stylianos Papanastasiou, Jens Mittag |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Authors: |
| 19 * Stylianos Papanastasiou <stylianos@gmail.com> |
| 20 * Jens Mittag <jens.mittag@kit.edu> |
| 21 */ |
| 22 |
| 23 #include "physim-channel-estimator.h" |
| 24 #include "physim-ofdm-symbolcreator.h" |
| 25 #include "ns3/log.h" |
| 26 #include "ns3/double.h" |
| 27 |
| 28 NS_LOG_COMPONENT_DEFINE ("PhySimChannelEstimator"); |
| 29 |
| 30 namespace ns3 { |
| 31 |
| 32 |
| 33 NS_OBJECT_ENSURE_REGISTERED (PhySimChannelEstimator); |
| 34 |
| 35 TypeId |
| 36 PhySimChannelEstimator::GetTypeId (void) |
| 37 { |
| 38 static TypeId tid = TypeId ("ns3::PhySimChannelEstimator") |
| 39 .SetParent<Object> () |
| 40 .AddConstructor<PhySimChannelEstimator> () |
| 41 .AddAttribute ("Noise Estimate N0", |
| 42 "An estimate of spectral density noise of the AWGN noise at t
he receiver", |
| 43 DoubleValue (0.0001), |
| 44 MakeDoubleAccessor (&PhySimChannelEstimator::m_N0), |
| 45 MakeDoubleChecker<double> ()); |
| 46 return tid; |
| 47 } |
| 48 |
| 49 PhySimChannelEstimator::PhySimChannelEstimator () |
| 50 { |
| 51 } |
| 52 |
| 53 PhySimChannelEstimator::~PhySimChannelEstimator () |
| 54 { |
| 55 } |
| 56 |
| 57 /* |
| 58 * \brief Given samples up to the SIGNAL (i.e. the short and long training seque
nces) |
| 59 * calculate an initial channel estimate. |
| 60 */ |
| 61 double |
| 62 PhySimChannelEstimator::GetInitialChannelEstimation (const itpp::cvec &input) |
| 63 { |
| 64 return 0.0; |
| 65 } |
| 66 |
| 67 /* |
| 68 * \brief Apply the estimate calculated from the training sequences. This functi
on should only be called |
| 69 * twice: Once for the training sequence and once for the actual OFDM blocks. |
| 70 * |
| 71 * @param input time samples |
| 72 * @param estimate the initial channel estimation |
| 73 * @param phaseOffset acts as a flag for the TS, so we define 0 for training seq
uence, 1 for frame OFDM symbols |
| 74 * @return corrected TS or OFDM symbol |
| 75 */ |
| 76 itpp::cvec |
| 77 PhySimChannelEstimator::ApplyEstimateFromTrainingSequence (const itpp::cvec &inp
ut, double estimate, int phaseOffset) |
| 78 { |
| 79 // Dummy behavior: simply return input |
| 80 return input; |
| 81 } |
| 82 |
| 83 /* |
| 84 * \brief Called for every OFDM symbol to apply the channel estimation and corre
ction |
| 85 * |
| 86 * @param input frequency domain representation (53 values) of the OFDM symbol |
| 87 * @param symbolNo 0 for SIGNAL, 1..n for the rest of the OfDM symbols |
| 88 * @return corrected samples |
| 89 */ |
| 90 itpp::cvec |
| 91 PhySimChannelEstimator::ApplyOFDMSymbolCorrection (const itpp::cvec &input, uint
32_t symbolNo) |
| 92 { |
| 93 // Dummy behavior: simply return input |
| 94 return input; |
| 95 } |
| 96 |
| 97 void |
| 98 PhySimChannelEstimator::SetDetectedNoise (const double estN0) |
| 99 { |
| 100 m_N0 = estN0; |
| 101 } |
| 102 |
| 103 /* |
| 104 * \brief Returns an estimate of the spectral density of the AWGN noise. |
| 105 */ |
| 106 double |
| 107 PhySimChannelEstimator::GetDetectedNoise () |
| 108 { |
| 109 return m_N0; |
| 110 } |
| 111 |
| 112 /* |
| 113 * \brief CReset the estimator. |
| 114 */ |
| 115 void |
| 116 PhySimChannelEstimator::Reset () |
| 117 { |
| 118 } |
| 119 |
| 120 /* |
| 121 * \brief Returns the pilots contained in the freq. domain representation. |
| 122 */ |
| 123 itpp::cvec |
| 124 PhySimChannelEstimator::GetPilots (const itpp::cvec& input) |
| 125 { |
| 126 NS_ASSERT ( input.size () == 53 ); |
| 127 itpp::cvec pilotsToReturn (4); |
| 128 pilotsToReturn (0) = input (5); // subcarrier index -21 |
| 129 pilotsToReturn (1) = input (19); // subcarrier index -7 |
| 130 pilotsToReturn (2) = input (33); // subcarrier index 7 |
| 131 pilotsToReturn (3) = input (47); // subcarrier index 21 |
| 132 return pilotsToReturn; |
| 133 } |
| 134 |
| 135 NS_OBJECT_ENSURE_REGISTERED (PhySimSimpleChannelEstimator); |
| 136 |
| 137 TypeId |
| 138 PhySimSimpleChannelEstimator::GetTypeId (void) |
| 139 { |
| 140 static TypeId tid = TypeId ("ns3::PhySimSimpleChannelEstimator") |
| 141 .SetParent<PhySimChannelEstimator> () |
| 142 .AddConstructor<PhySimSimpleChannelEstimator> (); |
| 143 return tid; |
| 144 } |
| 145 |
| 146 PhySimSimpleChannelEstimator::PhySimSimpleChannelEstimator () |
| 147 { |
| 148 } |
| 149 |
| 150 PhySimSimpleChannelEstimator::~PhySimSimpleChannelEstimator () |
| 151 { |
| 152 } |
| 153 |
| 154 double |
| 155 PhySimSimpleChannelEstimator::GetInitialChannelEstimation (const itpp::cvec &inp
ut) |
| 156 { |
| 157 return 0.0; |
| 158 } |
| 159 |
| 160 itpp::cvec |
| 161 PhySimSimpleChannelEstimator::ApplyEstimateFromTrainingSequence (const itpp::cve
c &input, |
| 162 double estimate
, |
| 163 int phaseOffset
) |
| 164 { |
| 165 return input; |
| 166 } |
| 167 |
| 168 itpp::cvec |
| 169 PhySimSimpleChannelEstimator::ApplyOFDMSymbolCorrection (const itpp::cvec &input
, uint32_t symbolNo) |
| 170 { |
| 171 // Get reference Pilot Subcarrier |
| 172 itpp::cvec pilots = PhySimOFDMSymbolCreator::GetPilotSubcarrier (symbolNo); |
| 173 itpp::cvec actualPilots = GetPilots (input); |
| 174 |
| 175 // Calculate ratio between pilots and reference |
| 176 itpp::cvec ratio = itpp::elem_div (pilots, actualPilots); |
| 177 |
| 178 // Interpolate between them |
| 179 itpp::cvec linInterp = itpp::lininterp (ratio, 17); // get 52 elements. |
| 180 NS_ASSERT ( linInterp.size () == 52 ); |
| 181 linInterp.ins (26, 1); // The DC is at position 26 |
| 182 |
| 183 itpp::cvec result = itpp::elem_mult (input, linInterp); |
| 184 |
| 185 return result; |
| 186 } |
| 187 |
| 188 void |
| 189 PhySimSimpleChannelEstimator::Reset () |
| 190 { |
| 191 NS_LOG_FUNCTION_NOARGS (); |
| 192 } |
| 193 |
| 194 NS_OBJECT_ENSURE_REGISTERED (PhySimChannelFrequencyOffsetEstimator); |
| 195 |
| 196 TypeId |
| 197 PhySimChannelFrequencyOffsetEstimator::GetTypeId (void) |
| 198 { |
| 199 static TypeId tid = TypeId ("ns3::PhySimChannelFrequencyOffsetEstimator") |
| 200 .SetParent<PhySimChannelEstimator> () |
| 201 .AddConstructor<PhySimChannelFrequencyOffsetEstimator> () |
| 202 ; |
| 203 return tid; |
| 204 } |
| 205 |
| 206 PhySimChannelFrequencyOffsetEstimator::PhySimChannelFrequencyOffsetEstimator ()
: m_channelGains (4) |
| 207 { |
| 208 m_channelGains.zeros (); |
| 209 ofdm.set_parameters (64,0,1); |
| 210 } |
| 211 |
| 212 PhySimChannelFrequencyOffsetEstimator::~PhySimChannelFrequencyOffsetEstimator () |
| 213 { |
| 214 } |
| 215 |
| 216 /*! |
| 217 * Takes in an time samples vector including short (ST) and long (LT) training s
equence and returns |
| 218 * an offset correction factor to be applied to the reference vector by multiply
ing by exp(-jna), |
| 219 * where n=128,... and a is the correction factor. |
| 220 * Input vector must be more than or equal to 192 samples long (two STs + 160 fo
r the LT). |
| 221 * |
| 222 * Care must be taken to ensure that the input makes sense, so the following mus
t be true |
| 223 * - endOfST must be >=32 and should divisable by 16 (otherwise we ignore first
few values until it is) |
| 224 * - the beginOfLT + 160 +1 marks the beginning of the SIGNAL header. From then
on there should be an extra 80 samples |
| 225 * - the rest should be divisable by 80. Otherwise there are too many samples an
d we ignore the last ones |
| 226 * |
| 227 * @param input time samples |
| 228 * @return a correction factor a, which can be applied to the original samples |
| 229 */ |
| 230 double |
| 231 PhySimChannelFrequencyOffsetEstimator::GetInitialChannelEstimation (const itpp::
cvec &input) |
| 232 { |
| 233 int32_t beginOfLT = input.size () - 160; |
| 234 |
| 235 // Note: normally, we would do a NS_ASSERT here, but since we do not want |
| 236 // to stop our simulation run in such a case, we simply return a value that |
| 237 // won't correct anything here... |
| 238 // NS_ASSERT ( beginOfLT > 31 && input.size() >= 192 ); |
| 239 if ( beginOfLT <= 31 || input.size () < 192 ) |
| 240 { |
| 241 return 0.0; // results in a factor of 1.0 in exp(-jna), where a = 0.0 |
| 242 } |
| 243 |
| 244 uint32_t endOfShortSymbols = beginOfLT - 1; |
| 245 uint32_t numSequences = (endOfShortSymbols + 1) / 16; // estimate how many STs
there are |
| 246 |
| 247 NS_LOG_DEBUG ("PhySimChannelFrequencyOffsetEstimator:GetInitialChannelEstimati
on() input.size = " << input.size () << " beginOfLT = " << beginOfLT << " numSeq
uences = " << numSequences); |
| 248 |
| 249 // Calculate offset to start from. this is chosen so that the last short train
ing sequence ends |
| 250 // exactly before the beginOfLT |
| 251 uint32_t startOfShortSymbols = (endOfShortSymbols + 1) - (numSequences * 16); |
| 252 |
| 253 NS_LOG_DEBUG ("PhySimChannelFrequencyOffsetEstimator:GetInitialChannelEstimati
on() --> startOfShortSymbols = " << startOfShortSymbols); |
| 254 NS_LOG_DEBUG ("PhySimChannelFrequencyOffsetEstimator:GetInitialChannelEstimati
on() --> endOfShortSymbols = " << endOfShortSymbols); |
| 255 |
| 256 double coarseEstimation = InitialCoarseOffsetEstimation ( input (startOfShortS
ymbols, endOfShortSymbols) ); |
| 257 NS_LOG_DEBUG ("PhySimChannelFrequencyOffsetEstimator:GetInitialChannelEstimati
on() --> coarseEstimation = " << coarseEstimation); |
| 258 itpp::cvec correctedLT = correctLT (input (beginOfLT + 32, beginOfLT + 160 - 1
), coarseEstimation); |
| 259 double fineEstimation = InitialFineOffsetEstimation (correctedLT); |
| 260 NS_LOG_DEBUG ("PhySimChannelFrequencyOffsetEstimator:GetInitialChannelEstimati
on() --> fineEstimation = " << fineEstimation); |
| 261 // find out what the channel gains are |
| 262 double TSoffsetEstimator = coarseEstimation + fineEstimation; |
| 263 correctedLT = correctLT (correctedLT, TSoffsetEstimator); // second correction |
| 264 |
| 265 // demodulate the ofdm |
| 266 itpp::cvec firstTrSeq = PhySimOFDMSymbolCreator::TransformToInput (ofdm.demodu
late (correctedLT.mid (0, 64))); |
| 267 itpp::cvec secondTrSeq = PhySimOFDMSymbolCreator::TransformToInput (ofdm.demod
ulate (correctedLT.mid (64, 64))); |
| 268 itpp::cvec pilotsFirst = GetPilots (firstTrSeq); |
| 269 itpp::cvec pilotsSecond = GetPilots (secondTrSeq); |
| 270 m_channelGains = (pilotsFirst + pilotsSecond) / 2; |
| 271 return TSoffsetEstimator; |
| 272 } |
| 273 |
| 274 /*! |
| 275 * \brief Applies the Training Sequence Estimate (if not calculated returns itpp
::cvec unchanged) |
| 276 * Returns a itpp::cvec with the estimate from the training sequence applied. Th
is function should only be called |
| 277 * twice: Once for the training sequence and once for the actual OFDM blocks. |
| 278 * |
| 279 * @param input the complex samples to work on |
| 280 * @param estimate the frequency offset estimation from the training sequen
ce |
| 281 * @param phaseOffset indicates whether we are already in the data symbols or
still in the training sequence |
| 282 * 0 = still in training sequence, 1 = input contains data
symbols |
| 283 * @return corrected time samples |
| 284 */ |
| 285 itpp::cvec |
| 286 PhySimChannelFrequencyOffsetEstimator::ApplyEstimateFromTrainingSequence (const
itpp::cvec &input, |
| 287 double
estimate, |
| 288 int ph
aseOffset) |
| 289 { |
| 290 itpp::cvec correctedSamples (input); |
| 291 uint32_t pOffset = 128 + phaseOffset * 80; |
| 292 double offSetWithPhase; |
| 293 for (int32_t i = 0; i < input.size (); ++i) |
| 294 { |
| 295 offSetWithPhase = estimate * (i + pOffset); |
| 296 correctedSamples (i) *= std::complex<double> (cos (offSetWithPhase), -1 *
sin (offSetWithPhase)); |
| 297 } |
| 298 return correctedSamples; |
| 299 } |
| 300 |
| 301 itpp::cvec |
| 302 PhySimChannelFrequencyOffsetEstimator::ApplyOFDMSymbolCorrection (const itpp::cv
ec &input, uint32_t symbolNo) |
| 303 { |
| 304 // Correct Residual Carrier Frequency Offset |
| 305 itpp::cvec residualCarrierFreqOffsetCorrected = CorrectResidualCarrierFreqOffs
et (input, symbolNo); |
| 306 |
| 307 itpp::cvec pilots = PhySimChannelEstimator::GetPilots (residualCarrierFreqOffs
etCorrected); |
| 308 |
| 309 // Get reference Pilot Subcarrier |
| 310 itpp::cvec refpilots = PhySimOFDMSymbolCreator::GetPilotSubcarrier (symbolNo); |
| 311 |
| 312 // Calculate ratio between pilots and reference |
| 313 itpp::cvec ratio = itpp::elem_div (refpilots, pilots); |
| 314 |
| 315 // Interpolate between them |
| 316 itpp::cvec linInterp = itpp::lininterp (ratio, 17); // get 52 elements. |
| 317 NS_ASSERT ( linInterp.size () == 52 ); |
| 318 linInterp.ins (26, 1); // The DC is at position 26 |
| 319 |
| 320 itpp::cvec result = itpp::elem_mult (residualCarrierFreqOffsetCorrected, linIn
terp); |
| 321 |
| 322 return result; |
| 323 } |
| 324 |
| 325 void |
| 326 PhySimChannelFrequencyOffsetEstimator::Reset () |
| 327 { |
| 328 } |
| 329 |
| 330 double |
| 331 PhySimChannelFrequencyOffsetEstimator::InitialFineOffsetEstimation (const itpp::
cvec &input) |
| 332 { |
| 333 NS_ASSERT ( input.size () == 128 ); |
| 334 |
| 335 std::complex<double> sum = 0; |
| 336 |
| 337 for (uint32_t i = 0; i < 64; ++i) |
| 338 { |
| 339 sum += std::conj (input (i)) * input (i + 64); |
| 340 } |
| 341 |
| 342 return (std::arg (sum) / 64); |
| 343 } |
| 344 |
| 345 /*! |
| 346 * Takes in a short training sequence (of length at least 32 samples) and return
s a |
| 347 * coarse frequency offset estimation. This is equation (4) in [Sourour04]. |
| 348 * |
| 349 * @param input the short training sequence |
| 350 * @return frequency offset estimation |
| 351 */ |
| 352 double |
| 353 PhySimChannelFrequencyOffsetEstimator::InitialCoarseOffsetEstimation (const itpp
::cvec &input) |
| 354 { |
| 355 // Note: normally we would do an NS_ASSERT here, but since this will break our |
| 356 // algorithm/simulation run, we simply return with 0.0 if the assertion is not |
| 357 // fulfilled. |
| 358 // NS_ASSERT( input.size() >= 32 && ( input.size() % 16 == 0) ); |
| 359 if ( input.size () < 32 || ( input.size () % 16 != 0) ) |
| 360 { |
| 361 NS_LOG_WARN ("Initial coarse offset estimation error!"); |
| 362 return 0.0; |
| 363 } |
| 364 |
| 365 NS_LOG_DEBUG ("PhySimChannelFrequencyOffsetEstimator::InitialCoarseOffsetEstim
ation() input.size() = " << input.size ()); |
| 366 |
| 367 std::complex<double> sum = 0; |
| 368 // uint32_t numSequences = input.size() / 16; |
| 369 uint32_t endIndex = input.size () - 17; |
| 370 NS_LOG_DEBUG ("PhySimChannelFrequencyOffsetEstimator::InitialCoarseOffsetEstim
ation() endIndex = " << endIndex); |
| 371 |
| 372 for (uint32_t i = 0; i <= endIndex; ++i) |
| 373 { |
| 374 NS_LOG_DEBUG ("PhySimChannelFrequencyOffsetEstimator::InitialCoarseOffsetE
stimation() i = " << i); |
| 375 sum += std::conj (input (i)) * input (i + 16); |
| 376 } |
| 377 return arg (sum) / 16.0; |
| 378 } |
| 379 |
| 380 itpp::cvec |
| 381 PhySimChannelFrequencyOffsetEstimator::CorrectResidualCarrierFreqOffset (const i
tpp::cvec &input, uint32_t symbolNo) |
| 382 { |
| 383 NS_ASSERT ( input.size () == 53 ); |
| 384 double estimator = CalculateResidualCarrierFreqOffset (input, symbolNo); |
| 385 if (isnan (estimator)) // if estimator is not valid then return original inpu
t |
| 386 { |
| 387 NS_LOG_WARN ("PhySimChannelFrequencyOffsetEstimator::CorrectResidualCarrie
rFreqOffset() - Estimator not valid"); |
| 388 return input; |
| 389 } |
| 390 NS_LOG_DEBUG ("PhySimChannelFrequencyOffEstimator::CorrectResidualCarrierFreqO
ffset() - Applying frequency offsetEst: " << estimator); |
| 391 itpp::cvec output (input.size ()); |
| 392 |
| 393 for (int32_t i = 0; i < input.size (); ++i) |
| 394 { |
| 395 // The paper says not to touch the pilots but we do not implement steps 1
and 3 in section 4 |
| 396 // so we need to correct the pilots as well as there is no further process
ing to be done on them |
| 397 output (i) = input (i) * std::complex<double> (cos (estimator), sin (-1 *
estimator)); |
| 398 } |
| 399 |
| 400 return output; |
| 401 } |
| 402 |
| 403 double |
| 404 PhySimChannelFrequencyOffsetEstimator::CalculateResidualCarrierFreqOffset (const
itpp::cvec &input, uint32_t symbolNo) |
| 405 { |
| 406 NS_ASSERT ( input.size () == 53 ); |
| 407 itpp::cvec refpilots = PhySimOFDMSymbolCreator::GetPilotSubcarrier (symbolNo); |
| 408 itpp::cvec recpilots = PhySimChannelEstimator::GetPilots (input); |
| 409 itpp::cvec Pls = itpp::elem_mult (recpilots, refpilots); |
| 410 |
| 411 std::complex<double> sum = 0; |
| 412 for (uint32_t i = 0; i < 4; ++i) |
| 413 { |
| 414 sum += Pls (i) * std::conj (m_channelGains (i)); |
| 415 } |
| 416 return std::arg (sum); |
| 417 } |
| 418 |
| 419 /* |
| 420 * \brief Applies a correction factor to the LT (without the GI) and returns the
corrected LT |
| 421 */ |
| 422 itpp::cvec |
| 423 PhySimChannelFrequencyOffsetEstimator::correctLT (const itpp::cvec LT, const dou
ble correction) |
| 424 { |
| 425 NS_ASSERT (LT.size () == 128); |
| 426 itpp::cvec realInput = LT; |
| 427 for (int i = 0; i < realInput.size (); ++i) |
| 428 { |
| 429 realInput (i) *= std::complex<double> (cos (i * correction), sin (-i |
| 430 * correc
tion)); |
| 431 } |
| 432 return realInput; |
| 433 } |
| 434 |
| 435 itpp::cvec |
| 436 PhySimChannelFrequencyOffsetEstimator::GetPilots (const itpp::cvec &input) |
| 437 { |
| 438 NS_ASSERT ( input.size () == 53 ); |
| 439 itpp::cvec pilotsToReturn (4); |
| 440 pilotsToReturn (0) = input (5); // subcarrier index -21 |
| 441 pilotsToReturn (1) = input (19); // subcarrier index -7 |
| 442 pilotsToReturn (2) = input (33); // subcarrier index 7 |
| 443 pilotsToReturn (3) = input (47); // subcarrier index 21 |
| 444 return pilotsToReturn; |
| 445 } |
| 446 } // namespace ns3 |
OLD | NEW |