libdspl-2.0
Digital Signal Processing Algorithm Library
inout.c
1 /*
2 * Copyright (c) 2015-2019 Sergey Bakhurin
3 * Digital Signal Processing Library [http://dsplib.org]
4 *
5 * This file is part of DSPL.
6 *
7 * is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * DSPL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "dspl.h"
27 
28 
29 
30 #ifdef DOXYGEN_ENGLISH
31 
32 #endif
33 #ifdef DOXYGEN_RUSSIAN
34 
35 #endif
36 int DSPL_API addlog(char* str, char* fn)
37 {
38  FILE* pFile = NULL;
39  if(!str)
40  return ERROR_PTR;
41 
42  pFile = fopen(fn, "a+");
43  if(pFile == NULL)
44  return ERROR_FOPEN;
45 
46  fprintf(pFile, "%s\n", str);
47 
48  fclose(pFile);
49  return RES_OK;
50 }
51 
52 
53 
54 
55 
56 
57 
58 #ifdef DOXYGEN_ENGLISH
59 
60 #endif
61 #ifdef DOXYGEN_RUSSIAN
62 
63 #endif
64 void DSPL_API dspl_info()
65 {
66  printf("\n\n D S P L - 2.0\n");
67  printf(" version 2.20.01.09\n");
68  printf("\n Copyright (C) 2015-2020\n");
69  printf(" Sergey Bakhurin www.dsplib.org\n");
70  printf(" ---------------------------------------------\n");
71  printf(" BLAS and LAPACK ver.: 3.8.0 www.netlib.org\n");
72 
73 }
74 
75 
76 #ifdef DOXYGEN_ENGLISH
77 
78 #endif
79 #ifdef DOXYGEN_RUSSIAN
80 
81 #endif
82 int DSPL_API readbin(char* fn, void** x, int* k, int* dtype)
83 {
84  FILE* pFile = NULL;
85  int n, m, t, err;
86 
87  if(!x)
88  return ERROR_PTR;
89  if(!fn)
90  return ERROR_FNAME;
91 
92  pFile = fopen(fn, "rb");
93  if(pFile == NULL)
94  return ERROR_FOPEN;
95 
96  if(fread(&t, sizeof(int), 1, pFile) != 1)
97  {
98  err = ERROR_FREAD_SIZE;
99  goto exit_label;
100  }
101 
102  if(dtype)
103  *dtype = t;
104 
105  if(fread(&n, sizeof(int), 1, pFile) != 1)
106  {
107  err = ERROR_FREAD_SIZE;
108  goto exit_label;
109  }
110 
111  if(fread(&m, sizeof(int), 1, pFile) != 1)
112  {
113  err = ERROR_FREAD_SIZE;
114  goto exit_label;
115  }
116 
117  if(k)
118  *k = n*m;
119  switch(t)
120  {
121  case DAT_DOUBLE:
122  (*x) = (*x) ? realloc(*x, n*m*sizeof(double)) :
123  malloc(n*m*sizeof(double));
124  if(fread(*x, sizeof(double), n*m, pFile) != n*m)
125  {
126  err = ERROR_FREAD_SIZE;
127  goto exit_label;
128  }
129 
130  break;
131  case DAT_COMPLEX:
132  (*x) = (*x) ? realloc(*x, n*m*sizeof(complex_t)) :
133  malloc(n*m*sizeof(complex_t));
134  if(fread(*x, sizeof(complex_t), n*m, pFile) != n*m)
135  {
136  err = ERROR_FREAD_SIZE;
137  goto exit_label;
138  }
139  break;
140  default:
141  err = ERROR_DAT_TYPE;
142  goto exit_label;
143  }
144  err = RES_OK;
145 exit_label:
146  if(pFile)
147  fclose(pFile);
148  return err;
149 }
150 
151 
152 #ifdef DOXYGEN_ENGLISH
153 
234 #endif
235 #ifdef DOXYGEN_RUSSIAN
236 
327 #endif
328 int DSPL_API writebin(void* x, int n, int dtype, char* fn)
329 {
330  int k, res;
331  FILE* pFile = NULL;
332 
333  if(!x)
334  return ERROR_PTR;
335  if(n < 1)
336  return ERROR_SIZE;
337  if(!fn)
338  return ERROR_FNAME;
339 
340  pFile = fopen(fn, "wb");
341  if(pFile == NULL)
342  return ERROR_FOPEN;
343 
344 
345  if(fwrite(&dtype, sizeof(int), 1, pFile) != 1)
346  {
347  res = ERROR_FWRITE_SIZE;
348  goto exit_label;
349  }
350 
351 
352 
353  if(fwrite(&n, sizeof(int), 1, pFile) != 1)
354  {
355  res = ERROR_FWRITE_SIZE;
356  goto exit_label;
357  }
358 
359  k = 1;
360  if(fwrite(&k, sizeof(int), 1, pFile) != 1)
361  {
362  res = ERROR_FWRITE_SIZE;
363  goto exit_label;
364  };
365 
366  switch(dtype)
367  {
368  case DAT_DOUBLE:
369  if(fwrite((double*)x, sizeof(double), n, pFile) != n)
370  {
371  res = ERROR_FWRITE_SIZE;
372  goto exit_label;
373  }
374  break;
375  case DAT_COMPLEX:
376  if(fwrite((complex_t*)x,
377  sizeof(complex_t), n, pFile) != n)
378  {
379  res = ERROR_FWRITE_SIZE;
380  goto exit_label;
381  }
382  break;
383  default:
384  res = ERROR_DAT_TYPE;
385  goto exit_label;
386  }
387  res = RES_OK;
388 exit_label:
389  if(pFile)
390  fclose(pFile);
391  return res;
392 }
393 
394 
395 
396 
397 
398 
399 #ifdef DOXYGEN_ENGLISH
400 
444 #endif
445 #ifdef DOXYGEN_RUSSIAN
446 
490 #endif
491 int DSPL_API writetxt(double* x, double* y, int n, char* fn)
492 {
493  int k, err;
494  FILE* pFile = NULL;
495  if(!x)
496  return ERROR_PTR;
497  if(n < 1)
498  return ERROR_SIZE;
499  if(!fn)
500  return ERROR_FNAME;
501 
502  pFile = fopen(fn, "w+");
503  if(pFile == NULL)
504  return ERROR_FOPEN;
505 
506  if(y)
507  {
508  for(k = 0; k < n; k++)
509  {
510  if(isnan(x[k]) || isnan(y[k]))
511  {
512  err = ERROR_NAN;
513  goto exit_label;
514  }
515  if(isinf(x[k]) || isinf(y[k]))
516  {
517  err = ERROR_INF;
518  goto exit_label;
519  }
520  fprintf(pFile, "%+.12E\t%+.12E\n", x[k], y[k]);
521  }
522  }
523  else
524  {
525  for(k = 0; k < n; k++)
526  {
527  if(isnan(x[k]))
528  {
529  err = ERROR_NAN;
530  goto exit_label;
531  }
532  if(isinf(x[k]))
533  {
534  err = ERROR_INF;
535  goto exit_label;
536  }
537  fprintf(pFile, "%+.12E\n", x[k]);
538  }
539  }
540  err = RES_OK;
541 exit_label:
542  fclose(pFile);
543  return err;
544 }
545 
546 
547 #ifdef DOXYGEN_ENGLISH
548 
549 #endif
550 #ifdef DOXYGEN_RUSSIAN
551 
552 #endif
553 int DSPL_API writetxt_cmplx(complex_t* x, int n, char* fn)
554 {
555  int k;
556  FILE* pFile = NULL;
557  if(!x)
558  return ERROR_PTR;
559  if(n < 1)
560  return ERROR_SIZE;
561  if(!fn)
562  return ERROR_FNAME;
563 
564  pFile = fopen(fn, "w+");
565  if(pFile == NULL)
566  return ERROR_FOPEN;
567 
568  for(k = 0; k < n; k++)
569  fprintf(pFile, "%+.12E %+.12E\n", RE(x[k]), IM(x[k]));
570 
571 
572  fclose(pFile);
573  return RES_OK;
574 }
575 
576 
577 
578 #ifdef DOXYGEN_ENGLISH
579 
619 #endif
620 #ifdef DOXYGEN_RUSSIAN
621 
663 #endif
664 int DSPL_API writetxt_int(int* x, int* y, int n, char* fn)
665 {
666  int k;
667  FILE* pFile = NULL;
668  if(!x)
669  return ERROR_PTR;
670  if(n < 1)
671  return ERROR_SIZE;
672  if(!fn)
673  return ERROR_FNAME;
674 
675  pFile = fopen(fn, "w+");
676  if(pFile == NULL)
677  return ERROR_FOPEN;
678 
679  if(y)
680  for(k = 0; k < n; k++)
681  fprintf(pFile, "% -14d\t% -14d\n", x[k], y[k]);
682 
683  else
684  for(k = 0; k < n; k++)
685  fprintf(pFile, "% -.14d\n", x[k]);
686 
687  fclose(pFile);
688  return RES_OK;
689 }
690 
691 
692 
693 
694 #ifdef DOXYGEN_ENGLISH
695 
787 #endif
788 #ifdef DOXYGEN_RUSSIAN
789 
879 #endif
880 int DSPL_API writetxt_3d(double* x, int nx, double* y, int ny,
881  double* z, char* fn)
882 {
883  int k, n;
884  FILE* pFile = NULL;
885 
886  if(!x || !y || !z)
887  return ERROR_PTR;
888  if(nx < 1 || ny < 1)
889  return ERROR_SIZE;
890  if(!fn)
891  return ERROR_FNAME;
892 
893  pFile = fopen(fn, "w+");
894  if(pFile == NULL)
895  return ERROR_FOPEN;
896 
897  for(k = 0; k < ny; k++)
898  {
899  for(n = 0; n < nx; n++)
900  {
901  if(!isnan(z[n+k*nx]))
902  {
903  fprintf(pFile, "%+.12E\t%+.12E\t%+.12E\n",
904  x[n], y[k], z[n+k*nx]);
905 
906  }
907  }
908  fprintf(pFile, "\n");
909 
910  }
911  fclose(pFile);
912  return RES_OK;
913 }
914 
915 
916 
917 
918 
919 #ifdef DOXYGEN_ENGLISH
920 
921 #endif
922 #ifdef DOXYGEN_RUSSIAN
923 
924 #endif
925 int DSPL_API writetxt_3dline(double* x, double *y, double* z, int n, char* fn)
926 {
927  int k;
928  FILE* pFile = NULL;
929 
930  if(!x || !y || !z)
931  return ERROR_PTR;
932  if(n < 1)
933  return ERROR_SIZE;
934  if(!fn)
935  return ERROR_FNAME;
936 
937  pFile = fopen(fn, "w+");
938  if(pFile == NULL)
939  return ERROR_FOPEN;
940 
941  for(k = 0; k < n; k++)
942  fprintf(pFile, "%+.12E\t%+.12E\t%+.12E\n", x[k], y[k], z[k]);
943  fprintf(pFile, "\n");
944  fclose(pFile);
945  return RES_OK;
946 }
947 
948 
949 
950 #ifdef DOXYGEN_ENGLISH
951 
952 #endif
953 #ifdef DOXYGEN_RUSSIAN
954 
955 #endif
956 int DSPL_API writetxt_cmplx_re(double* x, complex_t *y, int n, char* fn)
957 {
958  int k;
959  FILE* pFile = NULL;
960 
961  if(!x)
962  return ERROR_PTR;
963  if(n < 1)
964  return ERROR_SIZE;
965  if(!fn)
966  return ERROR_FNAME;
967 
968  pFile = fopen(fn, "w+");
969  if(pFile == NULL)
970  return ERROR_FOPEN;
971 
972  if(x)
973  for(k = 0; k < n; k++)
974  fprintf(pFile, "%+.12E\t%+.12E\n", x[k], RE(y[k]));
975  else
976  for(k = 0; k < n; k++)
977  fprintf(pFile, "%+.12E\n", RE(y[k]));
978 
979  fclose(pFile);
980  return RES_OK;
981 }
982 
983 
984 #ifdef DOXYGEN_ENGLISH
985 
986 #endif
987 #ifdef DOXYGEN_RUSSIAN
988 
989 #endif
990 int DSPL_API writetxt_cmplx_im(double* x, complex_t *y, int n, char* fn)
991 {
992  int k;
993  FILE* pFile = NULL;
994 
995  if(!x)
996  return ERROR_PTR;
997  if(n < 1)
998  return ERROR_SIZE;
999  if(!fn)
1000  return ERROR_FNAME;
1001 
1002  pFile = fopen(fn, "w+");
1003  if(pFile == NULL)
1004  return ERROR_FOPEN;
1005 
1006  if(x)
1007  for(k = 0; k < n; k++)
1008  fprintf(pFile, "%+.12E\t%+.12E\n", x[k], IM(y[k]));
1009  else
1010  for(k = 0; k < n; k++)
1011  fprintf(pFile, "%+.12E\n", IM(y[k]));
1012 
1013  fclose(pFile);
1014  return RES_OK;
1015 }
1016 
#define ERROR_FOPEN
Error opening file. A file with the specified name cannot be opened for writing or reading.
Definition: dspl.h:583
int DSPL_API writebin(void *x, int n, int dtype, char *fn)
Save 1D vector to the binary file.
Definition: inout.c:328
int DSPL_API writetxt_int(int *x, int *y, int n, char *fn)
Save interger data to the text file fn. .
Definition: inout.c:664
#define RE(x)
Macro sets real part of the complex number.
Definition: dspl.h:420
#define ERROR_PTR
Pointer error. This error means that one of the required pointers (memory to be allocated for) is tra...
Definition: dspl.h:610
#define ERROR_SIZE
Error array size. This error occurs when in addition to the pointer the wrong input is passed to the ...
Definition: dspl.h:618
#define ERROR_FNAME
Error in file name. You must specify the correct file name.
Definition: dspl.h:582
#define ERROR_FREAD_SIZE
Error reading data block from binary file. An error occurs if size of the read block does not match t...
Definition: dspl.h:584
int DSPL_API writetxt_3d(double *x, int nx, double *y, int ny, double *z, char *fn)
Save data to the text file fn for 3D surface plotting.
Definition: inout.c:880
int DSPL_API writetxt(double *x, double *y, int n, char *fn)
Save real data to the text file fn. .
Definition: inout.c:491
double complex_t[2]
Complex data type.
Definition: dspl.h:86
#define RES_OK
The function completed correctly. No errors.
Definition: dspl.h:558
#define ERROR_FWRITE_SIZE
Error writing data to binary file. An error occurs if, the size of the recorded block does not match ...
Definition: dspl.h:586
#define ERROR_DAT_TYPE
Invalid data type. This error occurs when saving data to a binary file, when the parameter the defini...
Definition: dspl.h:566
#define IM(x)
Macro sets imaginary part of the complex number.
Definition: dspl.h:478