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;
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  for(k = 0; k < n; k++)
508  fprintf(pFile, "%+.12E\t%+.12E\n", x[k], y[k]);
509 
510  else
511  for(k = 0; k < n; k++)
512  fprintf(pFile, "%+.12E\n", x[k]);
513 
514 
515  fclose(pFile);
516  return RES_OK;
517 }
518 
519 
520 #ifdef DOXYGEN_ENGLISH
521 
522 #endif
523 #ifdef DOXYGEN_RUSSIAN
524 
525 #endif
526 int DSPL_API writetxt_cmplx(complex_t* x, int n, char* fn)
527 {
528  int k;
529  FILE* pFile = NULL;
530  if(!x)
531  return ERROR_PTR;
532  if(n < 1)
533  return ERROR_SIZE;
534  if(!fn)
535  return ERROR_FNAME;
536 
537  pFile = fopen(fn, "w+");
538  if(pFile == NULL)
539  return ERROR_FOPEN;
540 
541  for(k = 0; k < n; k++)
542  fprintf(pFile, "%+.12E %+.12E\n", RE(x[k]), IM(x[k]));
543 
544 
545  fclose(pFile);
546  return RES_OK;
547 }
548 
549 
550 
551 #ifdef DOXYGEN_ENGLISH
552 
592 #endif
593 #ifdef DOXYGEN_RUSSIAN
594 
636 #endif
637 int DSPL_API writetxt_int(int* x, int* y, int n, char* fn)
638 {
639  int k;
640  FILE* pFile = NULL;
641  if(!x)
642  return ERROR_PTR;
643  if(n < 1)
644  return ERROR_SIZE;
645  if(!fn)
646  return ERROR_FNAME;
647 
648  pFile = fopen(fn, "w+");
649  if(pFile == NULL)
650  return ERROR_FOPEN;
651 
652  if(y)
653  for(k = 0; k < n; k++)
654  fprintf(pFile, "% -14d\t% -14d\n", x[k], y[k]);
655 
656  else
657  for(k = 0; k < n; k++)
658  fprintf(pFile, "% -.14d\n", x[k]);
659 
660  fclose(pFile);
661  return RES_OK;
662 }
663 
664 
665 
666 
667 #ifdef DOXYGEN_ENGLISH
668 
760 #endif
761 #ifdef DOXYGEN_RUSSIAN
762 
852 #endif
853 int DSPL_API writetxt_3d(double* x, int nx, double* y, int ny,
854  double* z, char* fn)
855 {
856  int k, n;
857  FILE* pFile = NULL;
858 
859  if(!x || !y || !z)
860  return ERROR_PTR;
861  if(nx < 1 || ny < 1)
862  return ERROR_SIZE;
863  if(!fn)
864  return ERROR_FNAME;
865 
866  pFile = fopen(fn, "w+");
867  if(pFile == NULL)
868  return ERROR_FOPEN;
869 
870  for(k = 0; k < ny; k++)
871  {
872  for(n = 0; n < nx; n++)
873  fprintf(pFile, "%+.12E\t%+.12E\t%+.12E\n",
874  x[n], y[k], z[n+k*nx]);
875  fprintf(pFile, "\n");
876 
877  }
878  fclose(pFile);
879  return RES_OK;
880 }
881 
882 
883 
884 
885 
886 #ifdef DOXYGEN_ENGLISH
887 
888 #endif
889 #ifdef DOXYGEN_RUSSIAN
890 
891 #endif
892 int DSPL_API writetxt_3dline(double* x, double *y, double* z, int n, char* fn)
893 {
894  int k;
895  FILE* pFile = NULL;
896 
897  if(!x || !y || !z)
898  return ERROR_PTR;
899  if(n < 1)
900  return ERROR_SIZE;
901  if(!fn)
902  return ERROR_FNAME;
903 
904  pFile = fopen(fn, "w+");
905  if(pFile == NULL)
906  return ERROR_FOPEN;
907 
908  for(k = 0; k < n; k++)
909  fprintf(pFile, "%+.12E\t%+.12E\t%+.12E\n", x[k], y[k], z[k]);
910  fprintf(pFile, "\n");
911  fclose(pFile);
912  return RES_OK;
913 }
914 
915 
916 
917 #ifdef DOXYGEN_ENGLISH
918 
919 #endif
920 #ifdef DOXYGEN_RUSSIAN
921 
922 #endif
923 int DSPL_API writetxt_cmplx_re(double* x, complex_t *y, int n, char* fn)
924 {
925  int k;
926  FILE* pFile = NULL;
927 
928  if(!x)
929  return ERROR_PTR;
930  if(n < 1)
931  return ERROR_SIZE;
932  if(!fn)
933  return ERROR_FNAME;
934 
935  pFile = fopen(fn, "w+");
936  if(pFile == NULL)
937  return ERROR_FOPEN;
938 
939  if(x)
940  for(k = 0; k < n; k++)
941  fprintf(pFile, "%+.12E\t%+.12E\n", x[k], RE(y[k]));
942  else
943  for(k = 0; k < n; k++)
944  fprintf(pFile, "%+.12E\n", RE(y[k]));
945 
946  fclose(pFile);
947  return RES_OK;
948 }
949 
950 
951 #ifdef DOXYGEN_ENGLISH
952 
953 #endif
954 #ifdef DOXYGEN_RUSSIAN
955 
956 #endif
957 int DSPL_API writetxt_cmplx_im(double* x, complex_t *y, int n, char* fn)
958 {
959  int k;
960  FILE* pFile = NULL;
961 
962  if(!x)
963  return ERROR_PTR;
964  if(n < 1)
965  return ERROR_SIZE;
966  if(!fn)
967  return ERROR_FNAME;
968 
969  pFile = fopen(fn, "w+");
970  if(pFile == NULL)
971  return ERROR_FOPEN;
972 
973  if(x)
974  for(k = 0; k < n; k++)
975  fprintf(pFile, "%+.12E\t%+.12E\n", x[k], IM(y[k]));
976  else
977  for(k = 0; k < n; k++)
978  fprintf(pFile, "%+.12E\n", IM(y[k]));
979 
980  fclose(pFile);
981  return RES_OK;
982 }
983 
#define ERROR_FOPEN
Error opening file. A file with the specified name cannot be opened for writing or reading.
Definition: dspl.h:522
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:637
#define RE(x)
Macro sets real part of the complex number.
Definition: dspl.h:359
#define ERROR_PTR
Pointer error. This error means that one of the required pointers (memory to be allocated for) is tra...
Definition: dspl.h:545
#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:553
#define ERROR_FNAME
Error in file name. You must specify the correct file name.
Definition: dspl.h:521
#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:523
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:853
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:497
#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:524
#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:505
#define IM(x)
Macro sets imaginary part of the complex number.
Definition: dspl.h:417