libdspl-2.0
Digital Signal Processing Algorithm Library
array.c
1 /*
2 * Copyright (c) 2015-2020 Sergey Bakhurin
3 * Digital Signal Processing Library [http://dsplib.org]
4 *
5 * This file is part of libdspl-2.0.
6 *
7 * is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser 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 Lesser 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 #include "blas.h"
27 
28 
29 #ifdef DOXYGEN_ENGLISH
30 
99 #endif
100 
101 #ifdef DOXYGEN_RUSSIAN
102 
170 #endif
171 int DSPL_API array_scale_lin(double* x, int n,
172  double xmin, double xmax, double dx,
173  double h, double* y)
174 {
175  double kx;
176  int k;
177  if(!x)
178  return ERROR_PTR;
179  if(n < 1)
180  return ERROR_SIZE;
181  if(h<0.0)
182  return ERROR_NEGATIVE;
183 
184  if(xmin >= xmax)
185  return ERROR_MIN_MAX;
186 
187  kx = h / (xmax - xmin);
188 
189  for(k = 0; k < n; k++)
190  y[k] = (x[k] - xmin) * kx + dx;
191 
192  return RES_OK;
193 }
194 
195 
196 
197 #ifdef DOXYGEN_ENGLISH
198 
256 #endif
257 
258 #ifdef DOXYGEN_RUSSIAN
259 
314 #endif
315 int DSPL_API concat(void* a, size_t na, void* b, size_t nb, void* c)
316 {
317  if(!a || !b || !c || c == b)
318  return ERROR_PTR;
319  if(na < 1 || nb < 1)
320  return ERROR_SIZE;
321 
322  if(c != a)
323  memcpy(c, a, na);
324 
325  memcpy((char*)c+na, b, nb);
326  return RES_OK;
327 }
328 
329 
330 
331 
332 
333 #ifdef DOXYGEN_ENGLISH
334 
387 #endif
388 #ifdef DOXYGEN_RUSSIAN
389 
444 #endif
445 int DSPL_API decimate(double* x, int n, int d, double* y, int* cnt)
446 {
447  int k = 0, i = 0;
448  if(!x || !y)
449  return ERROR_PTR;
450  if(n < 1)
451  return ERROR_SIZE;
452  if(d < 1)
453  return ERROR_NEGATIVE;
454 
455  k = i = 0;
456  while(k + d <= n)
457  {
458  y[i] = x[k];
459  k+=d;
460  i++;
461  }
462  if(cnt)
463  *cnt = i;
464 
465  return RES_OK;
466 }
467 
468 
469 
470 
471 #ifdef DOXYGEN_ENGLISH
472 
530 #endif
531 #ifdef DOXYGEN_RUSSIAN
532 
586 #endif
587 int DSPL_API decimate_cmplx(complex_t* x, int n, int d, complex_t* y, int* cnt)
588 {
589  int k = 0, i = 0;
590  if(!x || !y)
591  return ERROR_PTR;
592  if(n < 1)
593  return ERROR_SIZE;
594  if(d < 1)
595  return ERROR_NEGATIVE;
596 
597  k = i = 0;
598  while(k + d < n)
599  {
600  RE(y[i]) = RE(x[k]);
601  IM(y[i]) = IM(x[k]);
602  k+=d;
603  i++;
604  }
605  if(cnt)
606  *cnt = i;
607 
608  return RES_OK;
609 }
610 
611 
612 
613 
614 
615 #ifdef DOXYGEN_ENGLISH
616 
666 #endif
667 #ifdef DOXYGEN_RUSSIAN
668 
718 #endif
719 int DSPL_API flipip(double* x, int n)
720 {
721  int k;
722  double tmp;
723  if(!x)
724  return ERROR_PTR;
725  if(n<1)
726  return ERROR_SIZE;
727 
728  for(k = 0; k < n/2; k++)
729  {
730  tmp = x[k];
731  x[k] = x[n-1-k];
732  x[n-1-k] = tmp;
733  }
734  return RES_OK;
735 
736 }
737 
738 
739 
740 #ifdef DOXYGEN_ENGLISH
741 
788 #endif
789 #ifdef DOXYGEN_RUSSIAN
790 
841 #endif
842 int DSPL_API flipip_cmplx(complex_t* x, int n)
843 {
844  int k;
845  complex_t tmp;
846  if(!x)
847  return ERROR_PTR;
848  if(n<1)
849  return ERROR_SIZE;
850 
851  for(k = 0; k < n/2; k++)
852  {
853  RE(tmp) = RE(x[k]);
854  RE(x[k]) = RE(x[n-1-k]);
855  RE(x[n-1-k]) = RE(tmp);
856 
857  IM(tmp) = IM(x[k]);
858  IM(x[k]) = IM(x[n-1-k]);
859  IM(x[n-1-k]) = IM(tmp);
860  }
861  return RES_OK;
862 }
863 
864 
865 
866 
867 
868 
869 #ifdef DOXYGEN_ENGLISH
870 
935 #endif
936 #ifdef DOXYGEN_RUSSIAN
937 
1008 #endif
1009 int DSPL_API linspace(double x0, double x1, int n, int type, double* x)
1010 {
1011  double dx;
1012  int k;
1013 
1014  if(n < 2)
1015  return ERROR_SIZE;
1016  if(!x)
1017  return ERROR_PTR;
1018 
1019  switch (type)
1020  {
1021  case DSPL_SYMMETRIC:
1022  dx = (x1 - x0)/(double)(n-1);
1023  x[0] = x0;
1024  for(k = 1; k < n; k++)
1025  x[k] = x[k-1] + dx;
1026  break;
1027  case DSPL_PERIODIC:
1028  dx = (x1 - x0)/(double)n;
1029  x[0] = x0;
1030  for(k = 1; k < n; k++)
1031  x[k] = x[k-1] + dx;
1032  break;
1033  default:
1034  return ERROR_SYM_TYPE;
1035  }
1036  return RES_OK;
1037 }
1038 
1039 
1040 
1041 
1042 
1043 #ifdef DOXYGEN_ENGLISH
1044 
1118 #endif
1119 #ifdef DOXYGEN_RUSSIAN
1120 
1191 #endif
1192 int DSPL_API logspace(double x0, double x1, int n, int type, double* x)
1193 {
1194  double mx, a, b;
1195  int k;
1196 
1197  if(n < 2)
1198  return ERROR_SIZE;
1199  if(!x)
1200  return ERROR_PTR;
1201 
1202  a = pow(10.0, x0);
1203  b = pow(10.0, x1);
1204 
1205  switch (type)
1206  {
1207  case DSPL_SYMMETRIC:
1208  mx = pow(b/a, 1.0/(double)(n-1));
1209  x[0] = a;
1210  for(k = 1; k < n; k++)
1211  x[k] = x[k-1] * mx;
1212  break;
1213  case DSPL_PERIODIC:
1214  mx = pow(b/a, 1.0/(double)n);
1215  x[0] = a;
1216  for(k = 1; k < n; k++)
1217  x[k] = x[k-1] * mx;
1218  break;
1219  default:
1220  return ERROR_SYM_TYPE;
1221  }
1222  return RES_OK;
1223 }
1224 
1225 
1226 
1227 
1228 #ifdef DOXYGEN_ENGLISH
1229 
1264 #endif
1265 #ifdef DOXYGEN_RUSSIAN
1266 
1301 #endif
1302 int DSPL_API ones(double* x, int n)
1303 {
1304  int i;
1305  if(!x)
1306  return ERROR_PTR;
1307  if(n<1)
1308  return ERROR_SIZE;
1309  for(i = 0; i < n; i++)
1310  x[i] = 1.0;
1311  return RES_OK;
1312 }
int DSPL_API flipip_cmplx(complex_t *x, int n)
Flip complex vector x in place.
Definition: array.c:842
int DSPL_API flipip(double *x, int n)
Flip real vector x in place.
Definition: array.c:719
#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:549
#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:557
int DSPL_API logspace(double x0, double x1, int n, int type, double *x)
Function fills a vector with n logarithmically spaced elements between and .
Definition: array.c:1192
int DSPL_API ones(double *x, int n)
Function fills all real vector x by ones values.
Definition: array.c:1302
double complex_t[2]
Complex data type.
Definition: dspl.h:86
int DSPL_API decimate(double *x, int n, int d, double *y, int *cnt)
Real vector decimation.
Definition: array.c:445
#define RES_OK
The function completed correctly. No errors.
Definition: dspl.h:497
int DSPL_API concat(void *a, size_t na, void *b, size_t nb, void *c)
Concatenate arrays a and b
Definition: array.c:315
#define ERROR_MIN_MAX
The minimum value (lower bound) is greater than the maximum.
Definition: dspl.h:540
int DSPL_API array_scale_lin(double *x, int n, double xmin, double xmax, double dx, double h, double *y)
Vector x linear transformation.
Definition: array.c:171
#define ERROR_SYM_TYPE
Parameter error of type of filling arrays in linear or logarithmic scale. Valid fill types are symmet...
Definition: dspl.h:558
int DSPL_API linspace(double x0, double x1, int n, int type, double *x)
Function fills a vector with n linearly spaced elements between x0 and x1.
Definition: array.c:1009
#define IM(x)
Macro sets imaginary part of the complex number.
Definition: dspl.h:417
#define ERROR_NEGATIVE
Negative parameter. The function returns the given error code when it takes a negative parameter....
Definition: dspl.h:543
int DSPL_API decimate_cmplx(complex_t *x, int n, int d, complex_t *y, int *cnt)
Complex vector decimation.
Definition: array.c:587