srsLTE/srslte/lib/io/src/filesource.c

120 lines
3.0 KiB
C
Raw Normal View History

/**
*
* \section COPYRIGHT
*
2015-05-08 08:05:40 -07:00
* Copyright 2013-2015 The srsLTE Developers. See the
* COPYRIGHT file at the top-level directory of this distribution.
*
* \section LICENSE
*
* This file is part of the srsLTE library.
*
* 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,
* 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.
*
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 <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "srslte/io/filesource.h"
2014-01-28 03:41:17 -08:00
2015-03-18 08:05:38 -07:00
int srslte_filesource_init(srslte_filesource_t *q, char *filename, srslte_datatype_t type) {
bzero(q, sizeof(srslte_filesource_t));
2014-06-17 02:11:41 -07:00
q->f = fopen(filename, "r");
if (!q->f) {
perror("fopen");
return -1;
}
q->type = type;
return 0;
2014-01-28 03:41:17 -08:00
}
2015-03-18 08:05:38 -07:00
void srslte_filesource_free(srslte_filesource_t *q) {
2014-06-17 02:11:41 -07:00
if (q->f) {
fclose(q->f);
}
2015-03-18 08:05:38 -07:00
bzero(q, sizeof(srslte_filesource_t));
2014-01-28 03:41:17 -08:00
}
2015-03-18 08:05:38 -07:00
void srslte_filesource_seek(srslte_filesource_t *q, int pos) {
2014-06-17 02:11:41 -07:00
fseek(q->f, pos, SEEK_SET);
}
2014-01-28 03:41:17 -08:00
int read_complex_f(FILE *f, _Complex float *y) {
2014-06-17 02:11:41 -07:00
char in_str[64];
_Complex float x;
if (NULL == fgets(in_str, 64, f)) {
return -1;
} else {
if (index(in_str, 'i') || index(in_str, 'j')) {
sscanf(in_str,"%f%fi",&(__real__ x),&(__imag__ x));
} else {
__imag__ x = 0;
sscanf(in_str,"%f",&(__real__ x));
}
*y = x;
return 0;
}
2014-01-28 03:41:17 -08:00
}
2015-03-18 08:05:38 -07:00
int srslte_filesource_read(srslte_filesource_t *q, void *buffer, int nsamples) {
2014-06-17 02:11:41 -07:00
int i;
float *fbuf = (float*) buffer;
_Complex float *cbuf = (_Complex float*) buffer;
_Complex short *sbuf = (_Complex short*) buffer;
int size;
2014-01-28 03:41:17 -08:00
2014-06-17 02:11:41 -07:00
switch(q->type) {
2015-03-18 08:05:38 -07:00
case SRSLTE_FLOAT:
2014-06-17 02:11:41 -07:00
for (i=0;i<nsamples;i++) {
if (EOF == fscanf(q->f,"%g\n",&fbuf[i]))
break;
}
break;
2015-03-18 08:05:38 -07:00
case SRSLTE_COMPLEX_FLOAT:
2014-06-17 02:11:41 -07:00
for (i=0;i<nsamples;i++) {
if (read_complex_f(q->f, &cbuf[i])) {
break;
}
}
break;
2015-03-18 08:05:38 -07:00
case SRSLTE_COMPLEX_SHORT:
2014-06-17 02:11:41 -07:00
for (i=0;i<nsamples;i++) {
if (EOF == fscanf(q->f,"%hd%hdi\n",&(__real__ sbuf[i]),&(__imag__ sbuf[i])))
break;
}
break;
2015-03-18 08:05:38 -07:00
case SRSLTE_FLOAT_BIN:
case SRSLTE_COMPLEX_FLOAT_BIN:
case SRSLTE_COMPLEX_SHORT_BIN:
if (q->type == SRSLTE_FLOAT_BIN) {
2014-06-17 02:11:41 -07:00
size = sizeof(float);
2015-03-18 08:05:38 -07:00
} else if (q->type == SRSLTE_COMPLEX_FLOAT_BIN) {
2014-06-17 02:11:41 -07:00
size = sizeof(_Complex float);
2015-03-18 08:05:38 -07:00
} else if (q->type == SRSLTE_COMPLEX_SHORT_BIN) {
2014-06-17 02:11:41 -07:00
size = sizeof(_Complex short);
}
return fread(buffer, size, nsamples, q->f);
break;
default:
i = -1;
break;
}
return i;
2014-01-28 03:41:17 -08:00
}