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 int DSPL_API find_nearest(double* x, int n, double val, int *idx, double* dist)
615 {
616  double mind, dv;
617  int iv, i;
618 
619  if(!x)
620  return ERROR_PTR;
621  if(n < 1)
622  return ERROR_SIZE;
623 
624  mind = fabs(x[0] - val);
625  iv = 0;
626  for(i = 1; i < n; i++)
627  {
628  dv = fabs(x[i] - val);
629  if( dv < mind)
630  {
631  mind = dv;
632  iv = i;
633  }
634  }
635 
636  if(idx)
637  *idx = iv;
638  if(dist)
639  *dist = fabs(x[iv] - val);
640 
641  return RES_OK;
642 
643 }
644 
645 
646 
647 #ifdef DOXYGEN_ENGLISH
648 
698 #endif
699 #ifdef DOXYGEN_RUSSIAN
700 
750 #endif
751 int DSPL_API flipip(double* x, int n)
752 {
753  int k;
754  double tmp;
755  if(!x)
756  return ERROR_PTR;
757  if(n<1)
758  return ERROR_SIZE;
759 
760  for(k = 0; k < n/2; k++)
761  {
762  tmp = x[k];
763  x[k] = x[n-1-k];
764  x[n-1-k] = tmp;
765  }
766  return RES_OK;
767 
768 }
769 
770 
771 
772 #ifdef DOXYGEN_ENGLISH
773 
820 #endif
821 #ifdef DOXYGEN_RUSSIAN
822 
873 #endif
874 int DSPL_API flipip_cmplx(complex_t* x, int n)
875 {
876  int k;
877  complex_t tmp;
878  if(!x)
879  return ERROR_PTR;
880  if(n<1)
881  return ERROR_SIZE;
882 
883  for(k = 0; k < n/2; k++)
884  {
885  RE(tmp) = RE(x[k]);
886  RE(x[k]) = RE(x[n-1-k]);
887  RE(x[n-1-k]) = RE(tmp);
888 
889  IM(tmp) = IM(x[k]);
890  IM(x[k]) = IM(x[n-1-k]);
891  IM(x[n-1-k]) = IM(tmp);
892  }
893  return RES_OK;
894 }
895 
896 
897 
898 
899 
900 
901 #ifdef DOXYGEN_ENGLISH
902 
967 #endif
968 #ifdef DOXYGEN_RUSSIAN
969 
1040 #endif
1041 int DSPL_API linspace(double x0, double x1, int n, int type, double* x)
1042 {
1043  double dx;
1044  int k;
1045 
1046  if(n < 2)
1047  return ERROR_SIZE;
1048  if(!x)
1049  return ERROR_PTR;
1050 
1051  switch (type)
1052  {
1053  case DSPL_SYMMETRIC:
1054  dx = (x1 - x0)/(double)(n-1);
1055  x[0] = x0;
1056  for(k = 1; k < n; k++)
1057  x[k] = x[k-1] + dx;
1058  break;
1059  case DSPL_PERIODIC:
1060  dx = (x1 - x0)/(double)n;
1061  x[0] = x0;
1062  for(k = 1; k < n; k++)
1063  x[k] = x[k-1] + dx;
1064  break;
1065  default:
1066  return ERROR_SYM_TYPE;
1067  }
1068  return RES_OK;
1069 }
1070 
1071 
1072 
1073 
1074 
1075 #ifdef DOXYGEN_ENGLISH
1076 
1150 #endif
1151 #ifdef DOXYGEN_RUSSIAN
1152 
1223 #endif
1224 int DSPL_API logspace(double x0, double x1, int n, int type, double* x)
1225 {
1226  double mx, a, b;
1227  int k;
1228 
1229  if(n < 2)
1230  return ERROR_SIZE;
1231  if(!x)
1232  return ERROR_PTR;
1233 
1234  a = pow(10.0, x0);
1235  b = pow(10.0, x1);
1236 
1237  switch (type)
1238  {
1239  case DSPL_SYMMETRIC:
1240  mx = pow(b/a, 1.0/(double)(n-1));
1241  x[0] = a;
1242  for(k = 1; k < n; k++)
1243  x[k] = x[k-1] * mx;
1244  break;
1245  case DSPL_PERIODIC:
1246  mx = pow(b/a, 1.0/(double)n);
1247  x[0] = a;
1248  for(k = 1; k < n; k++)
1249  x[k] = x[k-1] * mx;
1250  break;
1251  default:
1252  return ERROR_SYM_TYPE;
1253  }
1254  return RES_OK;
1255 }
1256 
1257 
1258 
1259 
1260 #ifdef DOXYGEN_ENGLISH
1261 
1296 #endif
1297 #ifdef DOXYGEN_RUSSIAN
1298 
1333 #endif
1334 int DSPL_API ones(double* x, int n)
1335 {
1336  int i;
1337  if(!x)
1338  return ERROR_PTR;
1339  if(n<1)
1340  return ERROR_SIZE;
1341  for(i = 0; i < n; i++)
1342  x[i] = 1.0;
1343  return RES_OK;
1344 }
1345 
1346 
1347 #ifdef DOXYGEN_ENGLISH
1348 
1353 #endif
1354 #ifdef DOXYGEN_RUSSIAN
1355 
1360 #endif
1361 int DSPL_API sum(double* x, int n, double* s)
1362 {
1363  int i;
1364  double z = 0.0;
1365  if(!x || !s)
1366  return ERROR_PTR;
1367  if(n<1)
1368  return ERROR_SIZE;
1369 
1370  for(i = 0; i < n; i++)
1371  z += x[i];
1372  *s = z;
1373  return RES_OK;
1374 }
1375 
1376 #ifdef DOXYGEN_ENGLISH
1377 
1382 #endif
1383 #ifdef DOXYGEN_RUSSIAN
1384 
1389 #endif
1390 int DSPL_API sum_sqr(double* x, int n, double* s)
1391 {
1392  int i;
1393  double z = 0.0;
1394  if(!x || !s)
1395  return ERROR_PTR;
1396  if(n<1)
1397  return ERROR_SIZE;
1398 
1399  for(i = 0; i < n; i++)
1400  z += x[i]*x[i];
1401  *s = z;
1402  return RES_OK;
1403 }
1404 
1405 
1406 
1407 
1408 
int DSPL_API flipip_cmplx(complex_t *x, int n)
Flip complex vector x in place.
Definition: array.c:874
int DSPL_API flipip(double *x, int n)
Flip real vector x in place.
Definition: array.c:751
#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
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:1224
int DSPL_API ones(double *x, int n)
Function fills all real vector x by ones values.
Definition: array.c:1334
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:558
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:601
int DSPL_API sum(double *x, int n, double *s)
Definition: array.c:1361
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:619
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:1041
#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 decimate_cmplx(complex_t *x, int n, int d, complex_t *y, int *cnt)
Complex vector decimation.
Definition: array.c:587