libdspl-2.0/dspl/src/inout.c

359 wiersze
7.6 KiB
C
Czysty Zwykły widok Historia

2018-03-15 21:01:34 +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>
#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");
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");
2018-05-03 13:20:12 +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;
if(!x)
return ERROR_PTR;
if(!fn)
return ERROR_FNAME;
pFile = fopen(fn, "rb");
if(pFile == NULL)
return ERROR_FOPEN;
if(fread(&t, sizeof(int), 1, pFile) != 1)
{
err = ERROR_FREAD_SIZE;
goto exit_label;
}
if(dtype)
*dtype = t;
if(fread(&n, sizeof(int), 1, pFile) != 1)
{
err = ERROR_FREAD_SIZE;
goto exit_label;
}
if(fread(&m, sizeof(int), 1, pFile) != 1)
{
err = ERROR_FREAD_SIZE;
goto exit_label;
}
if(k)
*k = n*m;
switch(t)
{
case DAT_DOUBLE:
(*x) = (*x) ? realloc(*x, n*m*sizeof(double)) :
malloc(n*m*sizeof(double));
if(fread(*x, sizeof(double), n*m, pFile) != n*m)
{
err = ERROR_FREAD_SIZE;
goto exit_label;
}
break;
case DAT_COMPLEX:
(*x) = (*x) ? realloc(*x, n*m*sizeof(complex_t)) :
malloc(n*m*sizeof(complex_t));
if(fread(*x, sizeof(complex_t), n*m, pFile) != n*m)
{
err = ERROR_FREAD_SIZE;
goto exit_label;
}
default:
err = ERROR_DAT_TYPE;
goto exit_label;
}
err = RES_OK;
exit_label:
if(pFile)
fclose(pFile);
return err;
}
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;
Added gnuplot interface commande line functions. No need to use plt filees anymore Changes to be committed: deleted: bin/gnuplot/bessel_i0.plt deleted: bin/gnuplot/bilinear_test.plt deleted: bin/gnuplot/butter_ap_test.plt deleted: bin/gnuplot/cheby1_ap_test.plt deleted: bin/gnuplot/cheby_poly1.plt deleted: bin/gnuplot/cheby_poly2.plt deleted: bin/gnuplot/filter_iir.plt deleted: bin/gnuplot/gnuplot_script.plt deleted: bin/gnuplot/iir_bstop.plt deleted: bin/gnuplot/iir_lpf.plt deleted: bin/gnuplot/iir_test.plt deleted: bin/gnuplot/sinc_test.plt deleted: bin/gnuplot/sine_int.plt modified: bin/img/bessel_i0.png modified: bin/img/bilinear.png modified: bin/img/cheby_poly1.png modified: bin/img/cheby_poly2.png modified: bin/img/filter_iir_test.png deleted: bin/img/gnuplot_script.png modified: bin/img/iir_bstop.png modified: bin/img/iir_lpf.png modified: bin/img/iir_test.png modified: bin/img/sinc_test.png new file: dspl/doc/html/_form0_tmp.ps new file: dspl/doc/html/_formulas.aux new file: dspl/doc/html/_formulas.dvi new file: dspl/doc/html/_formulas.log new file: dspl/doc/html/_formulas.tex deleted: dspl/doc/html/formula.repository modified: dspl/dox/doxyfile_ru modified: dspl/dox/ru/error_list.dox new file: dspl/dox/ru/gnuplot.dox modified: dspl/dox/ru/groups_define.dox modified: dspl/dox/ru/mainpage.dox new file: dspl/src/gnuplot.c modified: dspl/src/inout.c modified: examples/src/bessel_i0.c modified: examples/src/bilinear_test.c modified: examples/src/butter_ap_test.c modified: examples/src/cheby1_ap_test.c modified: examples/src/cheby2_ap_zp_test.c modified: examples/src/cheby_poly1_test.c modified: examples/src/cheby_poly2_test.c modified: examples/src/filter_iir_test.c modified: examples/src/gnuplot_script_test.c modified: examples/src/iir_bstop.c modified: examples/src/iir_lpf.c modified: examples/src/iir_test.c modified: examples/src/sinc_test.c modified: examples/src/sine_int_test.c deleted: examples/src/test.c modified: ide/codeblocks/examples.depend modified: include/dspl.c modified: include/dspl.h
2020-01-09 19:31:03 +00:00
int res;
2018-10-24 17:39:51 +00:00
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]);
Added gnuplot interface commande line functions. No need to use plt filees anymore Changes to be committed: deleted: bin/gnuplot/bessel_i0.plt deleted: bin/gnuplot/bilinear_test.plt deleted: bin/gnuplot/butter_ap_test.plt deleted: bin/gnuplot/cheby1_ap_test.plt deleted: bin/gnuplot/cheby_poly1.plt deleted: bin/gnuplot/cheby_poly2.plt deleted: bin/gnuplot/filter_iir.plt deleted: bin/gnuplot/gnuplot_script.plt deleted: bin/gnuplot/iir_bstop.plt deleted: bin/gnuplot/iir_lpf.plt deleted: bin/gnuplot/iir_test.plt deleted: bin/gnuplot/sinc_test.plt deleted: bin/gnuplot/sine_int.plt modified: bin/img/bessel_i0.png modified: bin/img/bilinear.png modified: bin/img/cheby_poly1.png modified: bin/img/cheby_poly2.png modified: bin/img/filter_iir_test.png deleted: bin/img/gnuplot_script.png modified: bin/img/iir_bstop.png modified: bin/img/iir_lpf.png modified: bin/img/iir_test.png modified: bin/img/sinc_test.png new file: dspl/doc/html/_form0_tmp.ps new file: dspl/doc/html/_formulas.aux new file: dspl/doc/html/_formulas.dvi new file: dspl/doc/html/_formulas.log new file: dspl/doc/html/_formulas.tex deleted: dspl/doc/html/formula.repository modified: dspl/dox/doxyfile_ru modified: dspl/dox/ru/error_list.dox new file: dspl/dox/ru/gnuplot.dox modified: dspl/dox/ru/groups_define.dox modified: dspl/dox/ru/mainpage.dox new file: dspl/src/gnuplot.c modified: dspl/src/inout.c modified: examples/src/bessel_i0.c modified: examples/src/bilinear_test.c modified: examples/src/butter_ap_test.c modified: examples/src/cheby1_ap_test.c modified: examples/src/cheby2_ap_zp_test.c modified: examples/src/cheby_poly1_test.c modified: examples/src/cheby_poly2_test.c modified: examples/src/filter_iir_test.c modified: examples/src/gnuplot_script_test.c modified: examples/src/iir_bstop.c modified: examples/src/iir_lpf.c modified: examples/src/iir_test.c modified: examples/src/sinc_test.c modified: examples/src/sine_int_test.c deleted: examples/src/test.c modified: ide/codeblocks/examples.depend modified: include/dspl.c modified: include/dspl.h
2020-01-09 19:31:03 +00:00
2018-10-24 17:39:51 +00:00
else
Added gnuplot interface commande line functions. No need to use plt filees anymore Changes to be committed: deleted: bin/gnuplot/bessel_i0.plt deleted: bin/gnuplot/bilinear_test.plt deleted: bin/gnuplot/butter_ap_test.plt deleted: bin/gnuplot/cheby1_ap_test.plt deleted: bin/gnuplot/cheby_poly1.plt deleted: bin/gnuplot/cheby_poly2.plt deleted: bin/gnuplot/filter_iir.plt deleted: bin/gnuplot/gnuplot_script.plt deleted: bin/gnuplot/iir_bstop.plt deleted: bin/gnuplot/iir_lpf.plt deleted: bin/gnuplot/iir_test.plt deleted: bin/gnuplot/sinc_test.plt deleted: bin/gnuplot/sine_int.plt modified: bin/img/bessel_i0.png modified: bin/img/bilinear.png modified: bin/img/cheby_poly1.png modified: bin/img/cheby_poly2.png modified: bin/img/filter_iir_test.png deleted: bin/img/gnuplot_script.png modified: bin/img/iir_bstop.png modified: bin/img/iir_lpf.png modified: bin/img/iir_test.png modified: bin/img/sinc_test.png new file: dspl/doc/html/_form0_tmp.ps new file: dspl/doc/html/_formulas.aux new file: dspl/doc/html/_formulas.dvi new file: dspl/doc/html/_formulas.log new file: dspl/doc/html/_formulas.tex deleted: dspl/doc/html/formula.repository modified: dspl/dox/doxyfile_ru modified: dspl/dox/ru/error_list.dox new file: dspl/dox/ru/gnuplot.dox modified: dspl/dox/ru/groups_define.dox modified: dspl/dox/ru/mainpage.dox new file: dspl/src/gnuplot.c modified: dspl/src/inout.c modified: examples/src/bessel_i0.c modified: examples/src/bilinear_test.c modified: examples/src/butter_ap_test.c modified: examples/src/cheby1_ap_test.c modified: examples/src/cheby2_ap_zp_test.c modified: examples/src/cheby_poly1_test.c modified: examples/src/cheby_poly2_test.c modified: examples/src/filter_iir_test.c modified: examples/src/gnuplot_script_test.c modified: examples/src/iir_bstop.c modified: examples/src/iir_lpf.c modified: examples/src/iir_test.c modified: examples/src/sinc_test.c modified: examples/src/sine_int_test.c deleted: examples/src/test.c modified: ide/codeblocks/examples.depend modified: include/dspl.c modified: include/dspl.h
2020-01-09 19:31:03 +00:00
for(k = 0; k < n; k++)
2018-10-24 17:39:51 +00:00
fprintf(pFile, "%+.12E\n", x[k]);
Added gnuplot interface commande line functions. No need to use plt filees anymore Changes to be committed: deleted: bin/gnuplot/bessel_i0.plt deleted: bin/gnuplot/bilinear_test.plt deleted: bin/gnuplot/butter_ap_test.plt deleted: bin/gnuplot/cheby1_ap_test.plt deleted: bin/gnuplot/cheby_poly1.plt deleted: bin/gnuplot/cheby_poly2.plt deleted: bin/gnuplot/filter_iir.plt deleted: bin/gnuplot/gnuplot_script.plt deleted: bin/gnuplot/iir_bstop.plt deleted: bin/gnuplot/iir_lpf.plt deleted: bin/gnuplot/iir_test.plt deleted: bin/gnuplot/sinc_test.plt deleted: bin/gnuplot/sine_int.plt modified: bin/img/bessel_i0.png modified: bin/img/bilinear.png modified: bin/img/cheby_poly1.png modified: bin/img/cheby_poly2.png modified: bin/img/filter_iir_test.png deleted: bin/img/gnuplot_script.png modified: bin/img/iir_bstop.png modified: bin/img/iir_lpf.png modified: bin/img/iir_test.png modified: bin/img/sinc_test.png new file: dspl/doc/html/_form0_tmp.ps new file: dspl/doc/html/_formulas.aux new file: dspl/doc/html/_formulas.dvi new file: dspl/doc/html/_formulas.log new file: dspl/doc/html/_formulas.tex deleted: dspl/doc/html/formula.repository modified: dspl/dox/doxyfile_ru modified: dspl/dox/ru/error_list.dox new file: dspl/dox/ru/gnuplot.dox modified: dspl/dox/ru/groups_define.dox modified: dspl/dox/ru/mainpage.dox new file: dspl/src/gnuplot.c modified: dspl/src/inout.c modified: examples/src/bessel_i0.c modified: examples/src/bilinear_test.c modified: examples/src/butter_ap_test.c modified: examples/src/cheby1_ap_test.c modified: examples/src/cheby2_ap_zp_test.c modified: examples/src/cheby_poly1_test.c modified: examples/src/cheby_poly2_test.c modified: examples/src/filter_iir_test.c modified: examples/src/gnuplot_script_test.c modified: examples/src/iir_bstop.c modified: examples/src/iir_lpf.c modified: examples/src/iir_test.c modified: examples/src/sinc_test.c modified: examples/src/sine_int_test.c deleted: examples/src/test.c modified: ide/codeblocks/examples.depend modified: include/dspl.c modified: include/dspl.h
2020-01-09 19:31:03 +00:00
2018-10-24 17:39:51 +00:00
fclose(pFile);
return RES_OK;
2018-03-15 21:01:34 +00:00
}
2018-06-27 20:55:47 +00:00
/******************************************************************************
* Write a 3d plot data to file "fn" (pgfplots3d accepteble)
******************************************************************************/
2018-10-13 11:44:58 +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++)
{
for(n = 0; n < nx; n++)
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
}
/******************************************************************************
* Write a 3d line data to file "fn" (pgfplots3d accepteble)
******************************************************************************/
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
}
/******************************************************************************
Write a real part of coplex array to the text file "fn"
*******************************************************************************/
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
}
/******************************************************************************
Write a image part of coplex array to the text file "fn"
*******************************************************************************/
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
}