libdspl-2.0
Digital Signal Processing Algorithm Library
verification.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 #include "dspl.h"
26 
27 
28 #ifdef DOXYGEN_ENGLISH
29 
77 #endif
78 #ifdef DOXYGEN_RUSSIAN
79 
129 #endif
130 int DSPL_API verif(double* x, double* y, size_t n, double eps, double* err)
131 {
132  double d, maxd;
133  size_t k;
134  int res;
135  if(!x || !y)
136  return ERROR_PTR;
137  if(n < 1)
138  return ERROR_SIZE;
139  if(eps <= 0.0 )
140  return ERROR_NEGATIVE;
141 
142  maxd = -100.0;
143 
144  for(k = 0; k < n; k++)
145  {
146  d = fabs(x[k] - y[k]);
147  if(fabs(x[k]) > 0.0)
148  {
149  d = d / fabs(x[k]);
150  if(d > maxd)
151  maxd = d;
152  }
153  }
154  if(err)
155  *err = maxd;
156 
157  if(maxd > eps)
158  res = DSPL_VERIF_FAILED;
159  else
160  res = DSPL_VERIF_SUCCESS;
161 
162  return res;
163 }
164 
165 
166 
167 
168 
169 #ifdef DOXYGEN_ENGLISH
170 
171 #endif
172 #ifdef DOXYGEN_RUSSIAN
173 
174 #endif
175 int DSPL_API verif_data_gen(int len, int type, char* fn)
176 {
177  double *pd = NULL;
178  complex_t *pc = NULL;
179  random_t rnd = {0};
180  int err;
181  if(len < 1)
182  return ERROR_SIZE;
183  if(!fn)
184  return ERROR_FNAME;
185 
186  err = random_init(&rnd, RAND_TYPE_MRG32K3A, NULL);
187  if(err != RES_OK)
188  goto exit_label;
189  switch(type & DAT_MASK)
190  {
191  case DAT_DOUBLE:
192  pd = (double*)malloc(len*sizeof(double));
193  if(!pd)
194  {
195  err = ERROR_MALLOC;
196  goto exit_label;
197  }
198  err = randn(pd, len, 1.0, 10.0, &rnd);
199  if(err != RES_OK)
200  goto exit_label;
201 
202  err = writebin(pd, len, type, fn);
203  if(err != RES_OK)
204  goto exit_label;
205  break;
206  case DAT_COMPLEX:
207  pc = (complex_t*)malloc(len*sizeof(complex_t));
208  if(!pc)
209  {
210  err = ERROR_MALLOC;
211  goto exit_label;
212  }
213  err = randn((double*)pc, 2*len, 1.0, 10.0, &rnd);
214  if(err != RES_OK)
215  goto exit_label;
216 
217  err = writebin(pc, len, type, fn);
218  if(err != RES_OK)
219  goto exit_label;
220  break;
221  default:
222  err = ERROR_DAT_TYPE;
223  }
224 
225 exit_label:
226  if(pd)
227  free(pd);
228  if(pc)
229  free(pc);
230  return err;
231 }
232 
233 
234 
235 
236 
237 #ifdef DOXYGEN_ENGLISH
238 
292 #endif
293 #ifdef DOXYGEN_RUSSIAN
294 
346 #endif
347 int DSPL_API verif_cmplx(complex_t* x, complex_t* y, size_t n,
348  double eps, double* err)
349 {
350 
351  complex_t d;
352  double mx, md, maxd;
353  size_t k;
354  int res;
355  if(!x || !y)
356  return ERROR_PTR;
357  if(n < 1)
358  return ERROR_SIZE;
359  if(eps <= 0.0 )
360  return ERROR_NEGATIVE;
361 
362  maxd = -100.0;
363 
364  for(k = 0; k < n; k++)
365  {
366  RE(d) = RE(x[k]) - RE(y[k]);
367  IM(d) = IM(x[k]) - IM(y[k]);
368  md = ABS(d);
369  mx = ABS(x[k]);
370  if(mx > 0.0)
371  {
372  md = md / mx;
373  if(md > maxd)
374  maxd = md;
375  }
376  }
377  if(err)
378  *err = maxd;
379 
380  if(maxd > eps)
381  res = DSPL_VERIF_FAILED;
382  else
383  res = DSPL_VERIF_SUCCESS;
384 
385  return res;
386 }
387 
388 
389 
390 
391 #ifdef DOXYGEN_ENGLISH
392 
393 #endif
394 #ifdef DOXYGEN_RUSSIAN
395 
396 #endif
397 void DSPL_API verif_str(double* yout, int nout,
398  char* str_msg, char* outfn, char* logfn)
399 {
400  char str[VERIF_STR_BUF] = {0};
401  char msg[VERIF_STR_BUF] = {0};
402  double *y = NULL;
403  double derr = 0.0;
404  int n, verr, type;
405 
406  sprintf(str, "%s", str_msg);
407  while(strlen(str) < VERIF_STR_LEN)
408  str[strlen(str)] = VERIF_CHAR_POINT;
409 
410  readbin(outfn, (void**)(&y), &n, &type);
411 
412  if(nout != n)
413  {
414  sprintf(msg, "FAILED (out size [%d] neq [%d])", n, nout);
415  strcat(str, msg);
416  addlog(str, logfn);
417  printf("%s\n", str);
418  return;
419  }
420 
421  if(type!=DAT_DOUBLE)
422  {
423  sprintf(msg, "FAILED (type is complex)");
424  strcat(str, msg);
425  addlog(str, logfn);
426  printf("%s\n", str);
427  return;
428  }
429 
430  verr = verif(yout, y, nout, VERIF_LEVEL_DOUBLE, &derr);
431  if(verr == DSPL_VERIF_SUCCESS)
432  sprintf(msg, "ok (err = %12.4E)", derr);
433  else
434  sprintf(msg, "FAILED (err = %12.4E)", derr);
435  strcat(str, msg);
436  addlog(str, logfn);
437  printf("%s\n", str);
438 }
439 
440 
441 
442 
443 
444 
445 
446 #ifdef DOXYGEN_ENGLISH
447 
448 #endif
449 #ifdef DOXYGEN_RUSSIAN
450 
451 #endif
452 void DSPL_API verif_str_cmplx(complex_t* yout, int nout,
453  char* str_msg, char* outfn, char* logfn)
454 {
455  char str[VERIF_STR_BUF] = {0};
456  char msg[VERIF_STR_BUF] = {0};
457  complex_t *y = NULL;
458  double derr = 0.0;
459  int n, verr, type;
460 
461  sprintf(str, "%s", str_msg);
462  while(strlen(str) < VERIF_STR_LEN)
463  str[strlen(str)] = VERIF_CHAR_POINT;
464 
465  readbin(outfn, (void**)(&y), &n, &type);
466 
467  if(nout != n)
468  {
469  sprintf(msg, "FAILED (out size [%d] neq [%d])", n, nout);
470  strcat(str, msg);
471  addlog(str, logfn);
472  printf("%s\n", str);
473  return;
474  }
475 
476  if(type!=DAT_COMPLEX)
477  {
478  sprintf(msg, "FAILED (type is complex)");
479  strcat(str, msg);
480  addlog(str, logfn);
481  printf("%s\n", str);
482  return;
483  }
484 
485  verr = verif_cmplx(yout, y, nout, VERIF_LEVEL_DOUBLE, &derr);
486  if(verr == DSPL_VERIF_SUCCESS)
487  sprintf(msg, "ok (err = %12.4E)", derr);
488  else
489  sprintf(msg, "FAILED (err = %12.4E)", derr);
490  strcat(str, msg);
491  addlog(str, logfn);
492  printf("%s\n", str);
493 }
494 
495 
496 
Definition: dspl.h:350
int DSPL_API writebin(void *x, int n, int dtype, char *fn)
Save 1D vector to the binary file.
Definition: inout.c:328
#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
int DSPL_API random_init(random_t *prnd, int type, void *seed)
Pseudorandom numbers generators initialization.
Definition: randgen.c:121
double complex_t[2]
Complex data type.
Definition: dspl.h:86
int DSPL_API verif(double *x, double *y, size_t n, double eps, double *err)
Real arrays verification.
Definition: verification.c:130
#define RES_OK
The function completed correctly. No errors.
Definition: dspl.h:558
#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
#define ERROR_NEGATIVE
Negative parameter. The function returns the given error code when it takes a negative parameter....
Definition: dspl.h:604
int DSPL_API verif_cmplx(complex_t *x, complex_t *y, size_t n, double eps, double *err)
Complex arrays verification.
Definition: verification.c:347
#define ERROR_MALLOC
Dynamic memory allocation error. This error means that an error occurred while dynamically allocating...
Definition: dspl.h:599