mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
333 lines
8.4 KiB
C
333 lines
8.4 KiB
C
11 years ago
|
/**
|
||
|
*
|
||
|
* \section COPYRIGHT
|
||
|
*
|
||
|
* Copyright 2013-2014 The libLTE Developers. See the
|
||
|
* COPYRIGHT file at the top-level directory of this distribution.
|
||
|
*
|
||
|
* \section LICENSE
|
||
|
*
|
||
|
* This file is part of the libLTE library.
|
||
|
*
|
||
|
* libLTE is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Lesser General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of
|
||
|
* the License, or (at your option) any later version.
|
||
|
*
|
||
|
* libLTE 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 Lesser General Public License for more details.
|
||
|
*
|
||
|
* A copy of the GNU Lesser 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/.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <strings.h>
|
||
|
#include <unistd.h>
|
||
|
#include <math.h>
|
||
|
#include <time.h>
|
||
|
|
||
|
#include <sys/time.h>
|
||
|
#include <time.h>
|
||
11 years ago
|
#include "liblte/phy/phy.h"
|
||
11 years ago
|
|
||
10 years ago
|
#include "turbodecoder_test.h"
|
||
11 years ago
|
|
||
|
typedef _Complex float cf_t;
|
||
|
|
||
11 years ago
|
uint32_t frame_length = 1000, nof_frames = 100;
|
||
11 years ago
|
float ebno_db = 100.0;
|
||
11 years ago
|
uint32_t seed = 0;
|
||
11 years ago
|
int K = -1;
|
||
|
|
||
10 years ago
|
#define MAX_ITERATIONS 4
|
||
11 years ago
|
int nof_iterations = MAX_ITERATIONS;
|
||
|
int test_known_data = 0;
|
||
|
int test_errors = 0;
|
||
|
|
||
11 years ago
|
#define SNR_POINTS 8
|
||
|
#define SNR_MIN 0.0
|
||
|
#define SNR_MAX 4.0
|
||
11 years ago
|
|
||
|
void usage(char *prog) {
|
||
11 years ago
|
printf("Usage: %s [nlesv]\n", prog);
|
||
11 years ago
|
printf(
|
||
|
"\t-k Test with known data (ignores frame_length) [Default disabled]\n");
|
||
11 years ago
|
printf("\t-i nof_iterations [Default %d]\n", nof_iterations);
|
||
|
printf("\t-n nof_frames [Default %d]\n", nof_frames);
|
||
|
printf("\t-l frame_length [Default %d]\n", frame_length);
|
||
|
printf("\t-e ebno in dB [Default scan]\n");
|
||
|
printf("\t-t test: check errors on exit [Default disabled]\n");
|
||
|
printf("\t-s seed [Default 0=time]\n");
|
||
11 years ago
|
}
|
||
|
|
||
|
void parse_args(int argc, char **argv) {
|
||
11 years ago
|
int opt;
|
||
|
while ((opt = getopt(argc, argv, "inlstvekt")) != -1) {
|
||
|
switch (opt) {
|
||
|
case 'n':
|
||
|
nof_frames = atoi(argv[optind]);
|
||
|
break;
|
||
|
case 'k':
|
||
|
test_known_data = 1;
|
||
|
break;
|
||
|
case 't':
|
||
|
test_errors = 1;
|
||
|
break;
|
||
|
case 'i':
|
||
|
nof_iterations = atoi(argv[optind]);
|
||
|
break;
|
||
|
case 'l':
|
||
|
frame_length = atoi(argv[optind]);
|
||
|
break;
|
||
|
case 'e':
|
||
|
ebno_db = atof(argv[optind]);
|
||
|
break;
|
||
|
case 's':
|
||
10 years ago
|
seed = (uint32_t) strtoul(argv[optind], NULL, 0);
|
||
11 years ago
|
break;
|
||
|
case 'v':
|
||
|
verbose++;
|
||
|
break;
|
||
|
default:
|
||
|
usage(argv[0]);
|
||
|
exit(-1);
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
void output_matlab(float ber[MAX_ITERATIONS][SNR_POINTS], int snr_points) {
|
||
11 years ago
|
int i, j;
|
||
|
FILE *f = fopen("turbocoder_snr.m", "w");
|
||
|
if (!f) {
|
||
|
perror("fopen");
|
||
|
exit(-1);
|
||
|
}
|
||
|
fprintf(f, "ber=[");
|
||
11 years ago
|
for (j = 0; j < MAX_ITERATIONS; j++) {
|
||
11 years ago
|
for (i = 0; i < snr_points; i++) {
|
||
|
fprintf(f, "%g ", ber[j][i]);
|
||
|
}
|
||
|
fprintf(f, ";\n");
|
||
|
}
|
||
|
fprintf(f, "];\n");
|
||
|
fprintf(f, "snr=linspace(%g,%g-%g/%d,%d);\n", SNR_MIN, SNR_MAX, SNR_MAX,
|
||
|
snr_points, snr_points);
|
||
|
fprintf(f, "semilogy(snr,ber,snr,0.5*erfc(sqrt(10.^(snr/10))));\n");
|
||
11 years ago
|
fprintf(f,
|
||
|
"legend('1 iter','2 iter', '3 iter', '4 iter', 'theory-uncoded');");
|
||
11 years ago
|
fprintf(f, "grid on;\n");
|
||
|
fclose(f);
|
||
11 years ago
|
}
|
||
|
|
||
|
int main(int argc, char **argv) {
|
||
11 years ago
|
uint32_t frame_cnt;
|
||
11 years ago
|
float *llr;
|
||
10 years ago
|
uint8_t *llr_c;
|
||
|
uint8_t *data_tx, *data_rx, *symbols;
|
||
11 years ago
|
uint32_t i, j;
|
||
11 years ago
|
float var[SNR_POINTS];
|
||
11 years ago
|
uint32_t snr_points;
|
||
11 years ago
|
float ber[MAX_ITERATIONS][SNR_POINTS];
|
||
11 years ago
|
uint32_t errors[100];
|
||
|
uint32_t coded_length;
|
||
11 years ago
|
struct timeval tdata[3];
|
||
|
float mean_usec;
|
||
|
tdec_t tdec;
|
||
|
tcod_t tcod;
|
||
|
|
||
|
parse_args(argc, argv);
|
||
|
|
||
|
if (!seed) {
|
||
|
seed = time(NULL);
|
||
|
}
|
||
|
srand(seed);
|
||
|
|
||
|
if (test_known_data) {
|
||
|
frame_length = KNOWN_DATA_LEN;
|
||
|
} else {
|
||
|
frame_length = lte_cb_size(lte_find_cb_index(frame_length));
|
||
|
}
|
||
|
|
||
11 years ago
|
coded_length = 3 * (frame_length) + TOTALTAIL;
|
||
11 years ago
|
|
||
|
printf(" Frame length: %d\n", frame_length);
|
||
|
if (ebno_db < 100.0) {
|
||
|
printf(" EbNo: %.2f\n", ebno_db);
|
||
|
}
|
||
|
|
||
10 years ago
|
data_tx = malloc(frame_length * sizeof(uint8_t));
|
||
11 years ago
|
if (!data_tx) {
|
||
|
perror("malloc");
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
10 years ago
|
data_rx = malloc(frame_length * sizeof(uint8_t));
|
||
11 years ago
|
if (!data_rx) {
|
||
|
perror("malloc");
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
10 years ago
|
symbols = malloc(coded_length * sizeof(uint8_t));
|
||
11 years ago
|
if (!symbols) {
|
||
|
perror("malloc");
|
||
|
exit(-1);
|
||
|
}
|
||
|
llr = malloc(coded_length * sizeof(float));
|
||
|
if (!llr) {
|
||
|
perror("malloc");
|
||
|
exit(-1);
|
||
|
}
|
||
10 years ago
|
llr_c = malloc(coded_length * sizeof(uint8_t));
|
||
11 years ago
|
if (!llr_c) {
|
||
|
perror("malloc");
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
if (tcod_init(&tcod, frame_length)) {
|
||
|
fprintf(stderr, "Error initiating Turbo coder\n");
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
if (tdec_init(&tdec, frame_length)) {
|
||
|
fprintf(stderr, "Error initiating Turbo decoder\n");
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
float ebno_inc, esno_db;
|
||
|
ebno_inc = (SNR_MAX - SNR_MIN) / SNR_POINTS;
|
||
|
if (ebno_db == 100.0) {
|
||
|
snr_points = SNR_POINTS;
|
||
|
for (i = 0; i < snr_points; i++) {
|
||
|
ebno_db = SNR_MIN + i * ebno_inc;
|
||
|
esno_db = ebno_db + 10 * log10((double) 1 / 3);
|
||
|
var[i] = sqrt(1 / (pow(10, esno_db / 10)));
|
||
|
}
|
||
|
} else {
|
||
|
esno_db = ebno_db + 10 * log10((double) 1 / 3);
|
||
|
var[0] = sqrt(1 / (pow(10, esno_db / 10)));
|
||
|
snr_points = 1;
|
||
|
}
|
||
|
for (i = 0; i < snr_points; i++) {
|
||
|
mean_usec = 0;
|
||
|
frame_cnt = 0;
|
||
|
bzero(errors, sizeof(int) * MAX_ITERATIONS);
|
||
|
while (frame_cnt < nof_frames) {
|
||
|
|
||
|
/* generate data_tx */
|
||
|
for (j = 0; j < frame_length; j++) {
|
||
|
if (test_known_data) {
|
||
|
data_tx[j] = known_data[j];
|
||
|
} else {
|
||
|
data_tx[j] = rand() % 2;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* coded BER */
|
||
|
if (test_known_data) {
|
||
11 years ago
|
for (j = 0; j < coded_length; j++) {
|
||
11 years ago
|
symbols[j] = known_data_encoded[j];
|
||
|
}
|
||
|
} else {
|
||
11 years ago
|
tcod_encode(&tcod, data_tx, symbols, frame_length);
|
||
11 years ago
|
}
|
||
|
|
||
|
for (j = 0; j < coded_length; j++) {
|
||
|
llr[j] = symbols[j] ? sqrt(2) : -sqrt(2);
|
||
|
}
|
||
|
|
||
|
ch_awgn_f(llr, llr, var[i], coded_length);
|
||
|
|
||
|
/* decoder */
|
||
11 years ago
|
tdec_reset(&tdec, frame_length);
|
||
11 years ago
|
|
||
11 years ago
|
uint32_t t;
|
||
11 years ago
|
if (nof_iterations == -1) {
|
||
|
t = MAX_ITERATIONS;
|
||
|
} else {
|
||
|
t = nof_iterations;
|
||
|
}
|
||
11 years ago
|
for (j = 0; j < t; j++) {
|
||
|
|
||
|
if (!j)
|
||
|
gettimeofday(&tdata[1], NULL); // Only measure 1 iteration
|
||
|
tdec_iteration(&tdec, llr, frame_length);
|
||
|
tdec_decision(&tdec, data_rx, frame_length);
|
||
|
if (!j)
|
||
|
gettimeofday(&tdata[2], NULL);
|
||
|
if (!j)
|
||
|
get_time_interval(tdata);
|
||
|
if (!j)
|
||
|
mean_usec = (float) mean_usec * 0.9 + (float) tdata[0].tv_usec * 0.1;
|
||
11 years ago
|
|
||
|
/* check errors */
|
||
|
errors[j] += bit_diff(data_tx, data_rx, frame_length);
|
||
|
if (j < MAX_ITERATIONS) {
|
||
11 years ago
|
ber[j][i] = (float) errors[j] / (frame_cnt * frame_length);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
frame_cnt++;
|
||
|
printf("Eb/No: %3.2f %10d/%d ",
|
||
11 years ago
|
SNR_MIN + i * ebno_inc, frame_cnt, nof_frames);
|
||
|
printf("BER: %.2e ", (float) errors[j - 1] / (frame_cnt * frame_length));
|
||
|
printf("%3.1f Mbps (%6.2f usec)", (float) frame_length / mean_usec,
|
||
|
mean_usec);
|
||
11 years ago
|
printf("\r");
|
||
|
|
||
|
}
|
||
|
printf("\n");
|
||
|
|
||
|
if (snr_points == 1) {
|
||
|
if (test_known_data && seed == KNOWN_DATA_SEED
|
||
11 years ago
|
&& ebno_db == KNOWN_DATA_EBNO && frame_cnt == KNOWN_DATA_NFRAMES) {
|
||
|
for (j = 0; j < MAX_ITERATIONS; j++) {
|
||
11 years ago
|
if (errors[j] > known_data_errors[j]) {
|
||
|
fprintf(stderr, "Expected %d errors but got %d\n",
|
||
|
known_data_errors[j], errors[j]);
|
||
|
exit(-1);
|
||
11 years ago
|
} else {
|
||
|
printf("Iter %d ok\n", j + 1);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
} else {
|
||
11 years ago
|
for (j = 0; j < MAX_ITERATIONS; j++) {
|
||
|
printf("BER: %g\t%u errors\n",
|
||
|
(float) errors[j] / (frame_cnt * frame_length), errors[j]);
|
||
11 years ago
|
if (test_errors) {
|
||
11 years ago
|
if (errors[j]
|
||
|
> get_expected_errors(frame_cnt, seed, j + 1, frame_length,
|
||
|
ebno_db)) {
|
||
11 years ago
|
fprintf(stderr, "Expected %d errors but got %d\n",
|
||
11 years ago
|
get_expected_errors(frame_cnt, seed, j + 1, frame_length,
|
||
|
ebno_db), errors[j]);
|
||
11 years ago
|
exit(-1);
|
||
|
} else {
|
||
11 years ago
|
printf("Iter %d ok\n", j + 1);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
free(data_tx);
|
||
|
free(symbols);
|
||
|
free(llr);
|
||
|
free(llr_c);
|
||
|
free(data_rx);
|
||
|
|
||
|
tdec_free(&tdec);
|
||
|
tcod_free(&tcod);
|
||
|
|
||
|
printf("\n");
|
||
|
output_matlab(ber, snr_points);
|
||
|
printf("Done\n");
|
||
|
exit(0);
|
||
11 years ago
|
}
|