2018-03-15 21:01:34 +00:00
|
|
|
/*
|
2019-01-09 20:22:17 +00:00
|
|
|
* Copyright (c) 2015-2019 Sergey Bakhurin
|
2018-03-15 21:01:34 +00:00
|
|
|
* Digital Signal Processing Library [http://dsplib.org]
|
|
|
|
*
|
|
|
|
* This file is part of DSPL.
|
2018-10-13 11:44:58 +00:00
|
|
|
*
|
2018-03-15 21:01:34 +00:00
|
|
|
* is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* DSPL 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-10-27 18:09:21 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-03-15 21:01:34 +00:00
|
|
|
#include "dspl.h"
|
|
|
|
|
|
|
|
|
2018-05-03 13:20:12 +00:00
|
|
|
|
2018-06-27 20:55:47 +00:00
|
|
|
/*******************************************************************************
|
2018-05-03 13:20:12 +00:00
|
|
|
Print DSPL info
|
2018-06-27 20:55:47 +00:00
|
|
|
*******************************************************************************/
|
2018-05-03 13:20:12 +00:00
|
|
|
void DSPL_API dspl_info()
|
|
|
|
{
|
2018-10-24 17:39:51 +00:00
|
|
|
printf("\n\n D S P L - 2.0\n");
|
2020-01-09 20:58:32 +00:00
|
|
|
printf(" version 2.20.01.09\n");
|
|
|
|
printf("\n Copyright (C) 2015-2020\n");
|
2019-10-13 14:19:50 +00:00
|
|
|
printf(" Sergey Bakhurin www.dsplib.org\n");
|
2019-10-20 16:49:09 +00:00
|
|
|
printf(" ---------------------------------------------\n");
|
|
|
|
printf(" BLAS and LAPACK ver.: 3.8.0 www.netlib.org\n");
|
2019-10-27 18:09:21 +00:00
|
|
|
|
2018-05-03 13:20:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-10 20:37:31 +00:00
|
|
|
/******************************************************************************
|
|
|
|
Write a real array to the binary file "fn"
|
|
|
|
*******************************************************************************/
|
|
|
|
int DSPL_API readbin(char* fn, void** x, int* k, int* dtype)
|
|
|
|
{
|
|
|
|
FILE* pFile = NULL;
|
|
|
|
int n, m, t, err;
|
2019-10-27 18:09:21 +00:00
|
|
|
|
2018-12-10 20:37:31 +00:00
|
|
|
if(!x)
|
|
|
|
return ERROR_PTR;
|
|
|
|
if(!fn)
|
|
|
|
return ERROR_FNAME;
|
|
|
|
|
|
|
|
pFile = fopen(fn, "rb");
|
|
|
|
if(pFile == NULL)
|
2019-10-27 18:09:21 +00:00
|
|
|
return ERROR_FOPEN;
|
2018-12-10 20:37:31 +00:00
|
|
|
|
|
|
|
if(fread(&t, sizeof(int), 1, pFile) != 1)
|
|
|
|
{
|
|
|
|
err = ERROR_FREAD_SIZE;
|
|
|
|
goto exit_label;
|
|
|
|
}
|
2019-10-27 18:09:21 +00:00
|
|
|
|
|
|
|
if(dtype)
|
2018-12-10 20:37:31 +00:00
|
|
|
*dtype = t;
|
2019-10-27 18:09:21 +00:00
|
|
|
|
2018-12-10 20:37:31 +00:00
|
|
|
if(fread(&n, sizeof(int), 1, pFile) != 1)
|
|
|
|
{
|
|
|
|
err = ERROR_FREAD_SIZE;
|
|
|
|
goto exit_label;
|
|
|
|
}
|
2019-10-27 18:09:21 +00:00
|
|
|
|
2018-12-10 20:37:31 +00:00
|
|
|
if(fread(&m, sizeof(int), 1, pFile) != 1)
|
|
|
|
{
|
|
|
|
err = ERROR_FREAD_SIZE;
|
|
|
|
goto exit_label;
|
|
|
|
}
|
2019-10-27 18:09:21 +00:00
|
|
|
|
2018-12-10 20:37:31 +00:00
|
|
|
if(k)
|
|
|
|
*k = n*m;
|
2019-10-27 18:09:21 +00:00
|
|
|
|
2018-12-10 20:37:31 +00:00
|
|
|
switch(t)
|
|
|
|
{
|
|
|
|
case DAT_DOUBLE:
|
2019-10-27 18:09:21 +00:00
|
|
|
(*x) = (*x) ? realloc(*x, n*m*sizeof(double)) :
|
|
|
|
malloc(n*m*sizeof(double));
|
2018-12-10 20:37:31 +00:00
|
|
|
if(fread(*x, sizeof(double), n*m, pFile) != n*m)
|
|
|
|
{
|
|
|
|
err = ERROR_FREAD_SIZE;
|
|
|
|
goto exit_label;
|
2019-10-27 18:09:21 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 20:37:31 +00:00
|
|
|
break;
|
|
|
|
case DAT_COMPLEX:
|
2019-10-27 18:09:21 +00:00
|
|
|
(*x) = (*x) ? realloc(*x, n*m*sizeof(complex_t)) :
|
|
|
|
malloc(n*m*sizeof(complex_t));
|
2018-12-10 20:37:31 +00:00
|
|
|
if(fread(*x, sizeof(complex_t), n*m, pFile) != n*m)
|
|
|
|
{
|
|
|
|
err = ERROR_FREAD_SIZE;
|
|
|
|
goto exit_label;
|
2019-10-27 18:09:21 +00:00
|
|
|
}
|
2018-12-10 20:37:31 +00:00
|
|
|
default:
|
|
|
|
err = ERROR_DAT_TYPE;
|
|
|
|
goto exit_label;
|
|
|
|
}
|
|
|
|
err = RES_OK;
|
|
|
|
exit_label:
|
|
|
|
if(pFile)
|
|
|
|
fclose(pFile);
|
2019-10-27 18:09:21 +00:00
|
|
|
return err;
|
2018-12-10 20:37:31 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 13:20:12 +00:00
|
|
|
|
|
|
|
|
2018-06-27 20:55:47 +00:00
|
|
|
/******************************************************************************
|
2018-10-13 11:44:58 +00:00
|
|
|
Write a real array to the binary file "fn"
|
2018-06-27 20:55:47 +00:00
|
|
|
*******************************************************************************/
|
2018-03-15 21:01:34 +00:00
|
|
|
int DSPL_API writebin(void* x, int n, int dtype, char* fn)
|
|
|
|
{
|
2018-10-24 17:39:51 +00:00
|
|
|
int k, res;
|
|
|
|
FILE* pFile = NULL;
|
|
|
|
|
|
|
|
if(!x)
|
|
|
|
return ERROR_PTR;
|
|
|
|
if(n < 1)
|
|
|
|
return ERROR_SIZE;
|
|
|
|
if(!fn)
|
|
|
|
return ERROR_FNAME;
|
|
|
|
|
|
|
|
pFile = fopen(fn, "wb");
|
|
|
|
if(pFile == NULL)
|
|
|
|
return ERROR_FOPEN;
|
|
|
|
|
|
|
|
|
|
|
|
if(fwrite(&dtype, sizeof(int), 1, pFile) != 1)
|
|
|
|
{
|
|
|
|
res = ERROR_FWRITE_SIZE;
|
|
|
|
goto exit_label;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(fwrite(&n, sizeof(int), 1, pFile) != 1)
|
|
|
|
{
|
|
|
|
res = ERROR_FWRITE_SIZE;
|
|
|
|
goto exit_label;
|
|
|
|
}
|
|
|
|
|
|
|
|
k = 1;
|
|
|
|
if(fwrite(&k, sizeof(int), 1, pFile) != 1)
|
|
|
|
{
|
|
|
|
res = ERROR_FWRITE_SIZE;
|
|
|
|
goto exit_label;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch(dtype)
|
|
|
|
{
|
|
|
|
case DAT_DOUBLE:
|
|
|
|
if(fwrite((double*)x, sizeof(double), n, pFile) != n)
|
|
|
|
{
|
|
|
|
res = ERROR_FWRITE_SIZE;
|
|
|
|
goto exit_label;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DAT_COMPLEX:
|
|
|
|
if(fwrite((complex_t*)x,
|
|
|
|
sizeof(complex_t), n, pFile) != n)
|
|
|
|
{
|
|
|
|
res = ERROR_FWRITE_SIZE;
|
|
|
|
goto exit_label;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = ERROR_DAT_TYPE;
|
|
|
|
goto exit_label;
|
|
|
|
}
|
|
|
|
res = RES_OK;
|
2018-03-15 21:01:34 +00:00
|
|
|
exit_label:
|
2018-10-24 17:39:51 +00:00
|
|
|
if(pFile)
|
|
|
|
fclose(pFile);
|
|
|
|
return res;
|
2018-03-15 21:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-06-27 20:55:47 +00:00
|
|
|
/******************************************************************************
|
2018-10-13 11:44:58 +00:00
|
|
|
Write a real arrays to the text file "fn"
|
2018-06-27 20:55:47 +00:00
|
|
|
*******************************************************************************/
|
2019-11-04 14:05:33 +00:00
|
|
|
int DSPL_API writetxt(double* x, double* y, int n, char* fn)
|
2018-03-15 21:01:34 +00:00
|
|
|
{
|
2018-10-24 17:39:51 +00:00
|
|
|
int k;
|
|
|
|
FILE* pFile = NULL;
|
|
|
|
if(!x)
|
|
|
|
return ERROR_PTR;
|
|
|
|
if(n < 1)
|
|
|
|
return ERROR_SIZE;
|
|
|
|
if(!fn)
|
|
|
|
return ERROR_FNAME;
|
|
|
|
|
|
|
|
pFile = fopen(fn, "w+");
|
|
|
|
if(pFile == NULL)
|
|
|
|
return ERROR_FOPEN;
|
|
|
|
|
|
|
|
if(y)
|
|
|
|
for(k = 0; k < n; k++)
|
|
|
|
fprintf(pFile, "%+.12E\t%+.12E\n", x[k], y[k]);
|
2020-04-14 17:01:23 +00:00
|
|
|
|
2018-10-24 17:39:51 +00:00
|
|
|
else
|
2020-04-14 17:01:23 +00:00
|
|
|
for(k = 0; k < n; k++)
|
2018-10-24 17:39:51 +00:00
|
|
|
fprintf(pFile, "%+.12E\n", x[k]);
|
2020-04-14 17:01:23 +00:00
|
|
|
|
|
|
|
|
2018-10-24 17:39:51 +00:00
|
|
|
fclose(pFile);
|
|
|
|
return RES_OK;
|
2018-03-15 21:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-13 20:44:46 +00:00
|
|
|
/******************************************************************************
|
|
|
|
Write a real arrays to the text file "fn"
|
|
|
|
*******************************************************************************/
|
|
|
|
int DSPL_API writetxt_cmplx(complex_t* x, int n, char* fn)
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
FILE* pFile = NULL;
|
|
|
|
if(!x)
|
|
|
|
return ERROR_PTR;
|
|
|
|
if(n < 1)
|
|
|
|
return ERROR_SIZE;
|
|
|
|
if(!fn)
|
|
|
|
return ERROR_FNAME;
|
|
|
|
|
|
|
|
pFile = fopen(fn, "w+");
|
|
|
|
if(pFile == NULL)
|
|
|
|
return ERROR_FOPEN;
|
|
|
|
|
|
|
|
for(k = 0; k < n; k++)
|
|
|
|
fprintf(pFile, "%+.12E %+.12E\n", RE(x[k]), IM(x[k]));
|
|
|
|
|
|
|
|
|
|
|
|
fclose(pFile);
|
|
|
|
return RES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-07 10:13:34 +00:00
|
|
|
/******************************************************************************
|
|
|
|
Write a int arrays to the text file "fn"
|
|
|
|
*******************************************************************************/
|
|
|
|
int DSPL_API writetxt_int(int* x, int* y, int n, char* fn)
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
FILE* pFile = NULL;
|
|
|
|
if(!x)
|
|
|
|
return ERROR_PTR;
|
|
|
|
if(n < 1)
|
|
|
|
return ERROR_SIZE;
|
|
|
|
if(!fn)
|
|
|
|
return ERROR_FNAME;
|
|
|
|
|
|
|
|
pFile = fopen(fn, "w+");
|
|
|
|
if(pFile == NULL)
|
|
|
|
return ERROR_FOPEN;
|
|
|
|
|
|
|
|
if(y)
|
|
|
|
for(k = 0; k < n; k++)
|
|
|
|
fprintf(pFile, "% -14d\t% -14d\n", x[k], y[k]);
|
2018-06-27 20:55:47 +00:00
|
|
|
|
2020-05-07 10:13:34 +00:00
|
|
|
else
|
|
|
|
for(k = 0; k < n; k++)
|
|
|
|
fprintf(pFile, "% -.14d\n", x[k]);
|
|
|
|
|
|
|
|
fclose(pFile);
|
|
|
|
return RES_OK;
|
|
|
|
}
|
2018-06-27 20:55:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2020-06-04 08:57:34 +00:00
|
|
|
* Write a 3d plot data to file "fn" (pgfplots3d acceptable)
|
2018-06-27 20:55:47 +00:00
|
|
|
******************************************************************************/
|
2020-06-04 13:26:02 +00:00
|
|
|
int DSPL_API writetxt_3d(double* x, int nx, double* y, int ny,
|
2018-10-24 17:39:51 +00:00
|
|
|
double* z, char* fn)
|
2018-06-27 20:55:47 +00:00
|
|
|
{
|
2018-10-24 17:39:51 +00:00
|
|
|
int k, n;
|
|
|
|
FILE* pFile = NULL;
|
|
|
|
|
|
|
|
if(!x || !y || !z)
|
|
|
|
return ERROR_PTR;
|
|
|
|
if(nx < 1 || ny < 1)
|
|
|
|
return ERROR_SIZE;
|
|
|
|
if(!fn)
|
|
|
|
return ERROR_FNAME;
|
|
|
|
|
|
|
|
pFile = fopen(fn, "w+");
|
|
|
|
if(pFile == NULL)
|
|
|
|
return ERROR_FOPEN;
|
|
|
|
|
|
|
|
for(k = 0; k < ny; k++)
|
|
|
|
{
|
2020-06-04 08:57:34 +00:00
|
|
|
for(n = 0; n < nx; n++)
|
2018-10-24 17:39:51 +00:00
|
|
|
fprintf(pFile, "%+.12E\t%+.12E\t%+.12E\n",
|
|
|
|
x[n], y[k], z[n+k*nx]);
|
|
|
|
fprintf(pFile, "\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
fclose(pFile);
|
|
|
|
return RES_OK;
|
2018-06-27 20:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2020-06-04 08:57:34 +00:00
|
|
|
* Write a 3d line data to file "fn" (pgfplots3d acceptable)
|
2018-06-27 20:55:47 +00:00
|
|
|
******************************************************************************/
|
|
|
|
int DSPL_API writetxt_3dline(double* x, double *y, double* z, int n, char* fn)
|
|
|
|
{
|
2018-10-24 17:39:51 +00:00
|
|
|
int k;
|
|
|
|
FILE* pFile = NULL;
|
|
|
|
|
|
|
|
if(!x || !y || !z)
|
|
|
|
return ERROR_PTR;
|
|
|
|
if(n < 1)
|
|
|
|
return ERROR_SIZE;
|
|
|
|
if(!fn)
|
|
|
|
return ERROR_FNAME;
|
|
|
|
|
|
|
|
pFile = fopen(fn, "w+");
|
|
|
|
if(pFile == NULL)
|
|
|
|
return ERROR_FOPEN;
|
|
|
|
|
|
|
|
for(k = 0; k < n; k++)
|
|
|
|
fprintf(pFile, "%+.12E\t%+.12E\t%+.12E\n", x[k], y[k], z[k]);
|
|
|
|
fprintf(pFile, "\n");
|
|
|
|
fclose(pFile);
|
|
|
|
return RES_OK;
|
2018-10-13 11:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2020-06-04 08:57:34 +00:00
|
|
|
Write a real part of complex array to the text file "fn"
|
2018-10-13 11:44:58 +00:00
|
|
|
*******************************************************************************/
|
|
|
|
int DSPL_API writetxt_cmplx_re(double* x, complex_t *y, int n, char* fn)
|
|
|
|
{
|
2018-10-24 17:39:51 +00:00
|
|
|
int k;
|
|
|
|
FILE* pFile = NULL;
|
|
|
|
|
|
|
|
if(!x)
|
|
|
|
return ERROR_PTR;
|
|
|
|
if(n < 1)
|
|
|
|
return ERROR_SIZE;
|
|
|
|
if(!fn)
|
|
|
|
return ERROR_FNAME;
|
|
|
|
|
|
|
|
pFile = fopen(fn, "w+");
|
|
|
|
if(pFile == NULL)
|
|
|
|
return ERROR_FOPEN;
|
|
|
|
|
|
|
|
if(x)
|
|
|
|
for(k = 0; k < n; k++)
|
|
|
|
fprintf(pFile, "%+.12E\t%+.12E\n", x[k], RE(y[k]));
|
|
|
|
else
|
|
|
|
for(k = 0; k < n; k++)
|
|
|
|
fprintf(pFile, "%+.12E\n", RE(y[k]));
|
|
|
|
|
|
|
|
fclose(pFile);
|
|
|
|
return RES_OK;
|
2018-10-13 11:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2020-06-04 08:57:34 +00:00
|
|
|
Write a image part of complex array to the text file "fn"
|
2018-10-13 11:44:58 +00:00
|
|
|
*******************************************************************************/
|
|
|
|
int DSPL_API writetxt_cmplx_im(double* x, complex_t *y, int n, char* fn)
|
|
|
|
{
|
2018-10-24 17:39:51 +00:00
|
|
|
int k;
|
|
|
|
FILE* pFile = NULL;
|
|
|
|
|
|
|
|
if(!x)
|
|
|
|
return ERROR_PTR;
|
|
|
|
if(n < 1)
|
|
|
|
return ERROR_SIZE;
|
|
|
|
if(!fn)
|
|
|
|
return ERROR_FNAME;
|
|
|
|
|
|
|
|
pFile = fopen(fn, "w+");
|
|
|
|
if(pFile == NULL)
|
|
|
|
return ERROR_FOPEN;
|
|
|
|
|
|
|
|
if(x)
|
|
|
|
for(k = 0; k < n; k++)
|
|
|
|
fprintf(pFile, "%+.12E\t%+.12E\n", x[k], IM(y[k]));
|
|
|
|
else
|
|
|
|
for(k = 0; k < n; k++)
|
|
|
|
fprintf(pFile, "%+.12E\n", IM(y[k]));
|
|
|
|
|
|
|
|
fclose(pFile);
|
|
|
|
return RES_OK;
|
2018-06-27 20:55:47 +00:00
|
|
|
}
|
|
|
|
|