libdspl-2.0
Digital Signal Processing Algorithm Library
statistic.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 
29 
30 
31 #ifdef DOXYGEN_ENGLISH
32 
77 #endif
78 #ifdef DOXYGEN_RUSSIAN
79 
122 #endif
123 int DSPL_API find_max_abs(double* a, int n, double* m, int* ind)
124 {
125  int k, i;
126  double t;
127  if(!a)
128  return ERROR_PTR;
129  if(n < 1)
130  return ERROR_SIZE;
131  t = fabs(a[0]);
132  i = 0;
133  for(k = 1; k < n; k++)
134  {
135  if(fabs(a[k]) > t)
136  {
137  t = fabs(a[k]);
138  i = k;
139  }
140  }
141  if(m)
142  *m = t;
143  if(ind)
144  *ind = i;
145  return RES_OK;
146 }
147 
148 
149 #ifdef DOXYGEN_ENGLISH
150 
151 #endif
152 #ifdef DOXYGEN_RUSSIAN
153 
154 #endif
155 int DSPL_API histogram(double* x, int n, int nh, double* pedges, double* ph)
156 {
157  double xmin, xmax;
158  int k, ind;
159  int res;
160 
161  if(!x || !pedges || !ph)
162  return ERROR_PTR;
163 
164  if(n<1 || nh<1)
165  return ERROR_SIZE;
166 
167  res = minmax(x, n, &xmin, &xmax);
168  if(res != RES_OK)
169  return res;
170 
171  res = linspace(xmin, xmax, nh+1, DSPL_SYMMETRIC, pedges);
172  if(res != RES_OK)
173  return res;
174 
175  memset(ph, 0, nh*sizeof(double));
176  for(k = 0; k < n; k++)
177  {
178  ind = 0;
179  while(ind<nh && x[k]>=pedges[ind])
180  ind++;
181  ph[ind-1]+=1.0;
182  }
183  return RES_OK;
184 }
185 
186 
187 
188 
189 #ifdef DOXYGEN_ENGLISH
190 
191 #endif
192 #ifdef DOXYGEN_RUSSIAN
193 
194 #endif
195 int DSPL_API histogram_norm(double* y, int n, int nh, double* x, double* w)
196 {
197  double *pedges = NULL;
198  int k, res;
199 
200  if(!y || !x || !w)
201  return ERROR_PTR;
202 
203  if(n<1 || nh<1)
204  return ERROR_SIZE;
205 
206  pedges = (double*)malloc((nh+1)*sizeof(double));
207 
208  res = histogram(y, n, nh, pedges, w);
209  if(res != RES_OK)
210  goto exit_label;
211 
212  for(k = 1; k < nh+1; k++)
213  {
214  x[k-1] = 0.5*(pedges[k] + pedges[k-1]);
215  w[k-1] /= ((double)n * (pedges[k] - pedges[k-1]));
216  }
217 
218  res = RES_OK;
219 exit_label:
220 
221  if(pedges)
222  free(pedges);
223  return res;
224 
225 }
226 
227 
228 
229 
230 
231 
232 
233 #ifdef DOXYGEN_ENGLISH
234 
269 #endif
270 #ifdef DOXYGEN_RUSSIAN
271 
308 #endif
309 int DSPL_API mean(double* x, int n, double* m)
310 {
311  int k;
312  double sum;
313  if(!x || !m)
314  return ERROR_PTR;
315  if(n<1)
316  return ERROR_SIZE;
317 
318  sum = x[0];
319  for(k = 1; k < n; k++)
320  sum += x[k];
321 
322  (*m) = sum / (double)n;
323  return RES_OK;
324 }
325 
326 
327 
328 
329 
330 
331 #ifdef DOXYGEN_ENGLISH
332 
367 #endif
368 #ifdef DOXYGEN_RUSSIAN
369 
406 #endif
407 int DSPL_API mean_cmplx(complex_t* x, int n, complex_t* m)
408 {
409  int k;
410  complex_t sum;
411  if(!x || !m)
412  return ERROR_PTR;
413  if(n<1)
414  return ERROR_SIZE;
415 
416  RE(sum) = RE(x[0]);
417  IM(sum) = IM(x[0]);
418  for(k = 1; k < n; k++)
419  {
420  RE(sum) += RE(x[k]);
421  IM(sum) += IM(x[k]);
422  }
423  RE(sum) /= (double)n;
424  IM(sum) /= (double)n;
425  memcpy(m, sum, sizeof(complex_t));
426 
427  return RES_OK;
428 }
429 
430 
431 
432 
433 
434 
435 
436 #ifdef DOXYGEN_ENGLISH
437 
438 #endif
439 #ifdef DOXYGEN_RUSSIAN
440 
441 #endif
442 int DSPL_API minmax(double* x, int n, double* xmin, double* xmax)
443 {
444  int k;
445  double min, max;
446 
447  if(!x)
448  return ERROR_PTR;
449 
450  if(n<1)
451  return ERROR_SIZE;
452 
453  min = max = x[0];
454  for(k = 1; k < n; k++)
455  {
456  min = x[k] < min ? x[k] : min;
457  max = x[k] > max ? x[k] : max;
458  }
459 
460  if(xmin)
461  *xmin = min;
462  if(xmax)
463  *xmax = max;
464 
465  return RES_OK;
466 }
467 
468 
469 
470 
471 
472 
473 #ifdef DOXYGEN_ENGLISH
474 #endif
513 #ifdef DOXYGEN_RUSSIAN
514 
555 #endif
556 int DSPL_API std(double* x, int n, double* s)
557 {
558  int k, err;
559  double sum, m;
560  err = mean(x, n, &m);
561  if(err != RES_OK)
562  goto exit_label;
563  sum = (x[0] - m) * (x[0] - m);
564  for(k = 1; k < n; k++)
565  sum += (x[k] - m) * (x[k] - m);
566  (*s) = sqrt(sum / (double)(n-1));
567 exit_label:
568  return err;
569 }
570 
571 
572 
573 
574 
575 
576 
577 #ifdef DOXYGEN_ENGLISH
578 #endif
617 #ifdef DOXYGEN_RUSSIAN
618 
661 #endif
662 int DSPL_API std_cmplx(complex_t* x, int n, double* s)
663 {
664  int k, err;
665  complex_t tmp, m;
666  double sum;
667  err = mean_cmplx(x, n, &m);
668  if(err != RES_OK)
669  goto exit_label;
670 
671  sum = 0.0;
672  for(k = 0; k < n; k++)
673  {
674  RE(tmp) = RE(x[k]) - RE(m);
675  IM(tmp) = IM(x[k]) - IM(m);
676  sum += ABSSQR(tmp);
677  }
678  *s = sqrt(sum / (double)(n-1));
679 exit_label:
680  return err;
681 }
682 
683 
684 
685 
#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 std_cmplx(complex_t *x, int n, double *s)
Calculates the standard deviation of the complex input vector x
Definition: statistic.c:662
int DSPL_API std(double *x, int n, double *s)
Calculates the standard deviation of the input vector x
Definition: statistic.c:556
double complex_t[2]
Complex data type.
Definition: dspl.h:86
int DSPL_API find_max_abs(double *a, int n, double *m, int *ind)
Find maximum absolute value from the real vector a
Definition: statistic.c:123
#define RES_OK
The function completed correctly. No errors.
Definition: dspl.h:558
#define ABSSQR(x)
The macro returns the square of the modulus of a complex number x.
Definition: dspl.h:534
int DSPL_API sum(double *x, int n, double *s)
Definition: array.c:1361
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
int DSPL_API mean_cmplx(complex_t *x, int n, complex_t *m)
Calculates the mean of the complex input vector x
Definition: statistic.c:407
#define IM(x)
Macro sets imaginary part of the complex number.
Definition: dspl.h:478
int DSPL_API mean(double *x, int n, double *m)
Calculates the mean of the input vector x
Definition: statistic.c:309