srsLTE/srslte/lib/common/sequence.c

141 lines
3.1 KiB
C
Raw Normal View History

/**
*
* \section COPYRIGHT
*
2015-11-13 04:22:33 -08:00
* Copyright 2013-2015 Software Radio Systems Limited
*
* \section LICENSE
2014-01-28 03:41:17 -08:00
*
* This file is part of the srsLTE library.
2014-01-28 03:41:17 -08:00
*
* srsLTE is free software: you can redistribute it and/or modify
2015-05-08 08:05:40 -07:00
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srsLTE is distributed in the hope that it will be useful,
2014-01-28 03:41:17 -08:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-05-08 08:05:40 -07:00
* GNU Affero General Public License for more details.
2014-01-28 03:41:17 -08:00
*
2015-05-08 08:05:40 -07:00
* A copy of the GNU Affero General Public License can be found in
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
2014-01-28 03:41:17 -08:00
*/
#include <stdlib.h>
2014-01-31 06:35:43 -08:00
#include <stdio.h>
2014-01-28 03:41:17 -08:00
#include <strings.h>
#include <assert.h>
#include "srslte/common/sequence.h"
2015-08-06 17:56:45 -07:00
#include "srslte/utils/vector.h"
#include "srslte/utils/bit.h"
2014-01-28 03:41:17 -08:00
#define Nc 1600
/*
* Pseudo Random Sequence generation.
* It follows the 3GPP Release 8 (LTE) 36.211
* Section 7.2
*/
2015-03-18 08:05:38 -07:00
void srslte_sequence_set_LTE_pr(srslte_sequence_t *q, uint32_t seed) {
2014-06-17 02:11:41 -07:00
int n;
uint32_t *x1, *x2;
2014-06-17 02:11:41 -07:00
x1 = calloc(Nc + q->len + 31, sizeof(uint32_t));
2014-06-17 02:11:41 -07:00
if (!x1) {
perror("calloc");
return;
}
x2 = calloc(Nc + q->len + 31, sizeof(uint32_t));
2014-06-17 02:11:41 -07:00
if (!x2) {
free(x1);
perror("calloc");
return;
}
for (n = 0; n < 31; n++) {
x2[n] = (seed >> n) & 0x1;
}
x1[0] = 1;
for (n = 0; n < Nc + q->len; n++) {
x1[n + 31] = (x1[n + 3] + x1[n]) & 0x1;
x2[n + 31] = (x2[n + 3] + x2[n + 2] + +x2[n+1] + x2[n]) & 0x1;
}
for (n = 0; n < q->len; n++) {
q->c[n] = (x1[n + Nc] + x2[n + Nc]) & 0x1;
}
free(x1);
free(x2);
2014-01-28 03:41:17 -08:00
}
2015-03-18 08:05:38 -07:00
int srslte_sequence_LTE_pr(srslte_sequence_t *q, uint32_t len, uint32_t seed) {
if (srslte_sequence_init(q, len)) {
return SRSLTE_ERROR;
2014-06-17 02:11:41 -07:00
}
q->len = len;
2015-03-18 08:05:38 -07:00
srslte_sequence_set_LTE_pr(q, seed);
srslte_bit_pack_vector(q->c, q->c_bytes, len);
for (int i=0;i<len;i++) {
q->c_float[i] = (1-2*q->c[i]);
q->c_short[i] = (int16_t) q->c_float[i];
}
return SRSLTE_SUCCESS;
2014-01-28 03:41:17 -08:00
}
2015-03-18 08:05:38 -07:00
int srslte_sequence_init(srslte_sequence_t *q, uint32_t len) {
2014-06-17 02:11:41 -07:00
if (q->c && (q->len != len)) {
free(q->c);
if (q->c_bytes) {
free(q->c_bytes);
}
if (q->c_float) {
free(q->c_float);
}
if (q->c_short) {
free(q->c_short);
}
2014-06-17 02:11:41 -07:00
}
if (!q->c) {
2015-08-06 17:56:45 -07:00
q->c = srslte_vec_malloc(len * sizeof(uint8_t));
2014-06-17 02:11:41 -07:00
if (!q->c) {
return SRSLTE_ERROR;
2014-06-17 02:11:41 -07:00
}
q->c_bytes = srslte_vec_malloc(len * sizeof(uint8_t)/8+8);
if (!q->c_bytes) {
return SRSLTE_ERROR;
}
q->c_float = srslte_vec_malloc(len * sizeof(float));
if (!q->c_float) {
return SRSLTE_ERROR;
}
q->c_short = srslte_vec_malloc(len * sizeof(short));
if (!q->c_short) {
return SRSLTE_ERROR;
}
2014-11-05 05:19:35 -08:00
q->len = len;
2014-06-17 02:11:41 -07:00
}
return SRSLTE_SUCCESS;
2014-01-28 03:41:17 -08:00
}
2015-03-18 08:05:38 -07:00
void srslte_sequence_free(srslte_sequence_t *q) {
2014-06-17 02:11:41 -07:00
if (q->c) {
free(q->c);
}
if (q->c_bytes) {
free(q->c_bytes);
}
if (q->c_float) {
free(q->c_float);
}
2015-03-18 08:05:38 -07:00
bzero(q, sizeof(srslte_sequence_t));
2014-01-28 03:41:17 -08:00
}