libdspl-2.0
Digital Signal Processing Algorithm Library
cheby.c
1 /*
2 * Copyright (c) 2015-2019 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 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24 #include "dspl.h"
25 
26 
27 #ifdef DOXYGEN_ENGLISH
28 
78 #endif
79 #ifdef DOXYGEN_RUSSIAN
80 
133 #endif
134 int DSPL_API cheby_poly1(double* x, int n, int ord, double* y)
135 {
136  int k, m;
137  double t[2];
138 
139  if(!x || !y)
140  return ERROR_PTR;
141  if(n < 1)
142  return ERROR_SIZE;
143  if(ord<0)
144  return ERROR_POLY_ORD;
145  if(ord==0)
146  {
147  for(k = 0; k < n; k++)
148  {
149  y[k] = 1.0;
150  }
151  return RES_OK;
152  }
153 
154  if(ord==1)
155  {
156  memcpy(y, x, n*sizeof(double));
157  return RES_OK;
158  }
159 
160  for(k = 0; k < n; k++)
161  {
162  m = 2;
163  t[1] = x[k];
164  t[0] = 1.0;
165  while(m <= ord)
166  {
167  y[k] = 2.0 * x[k] *t[1] - t[0];
168  t[0] = t[1];
169  t[1] = y[k];
170  m++;
171  }
172  }
173  return RES_OK;
174 }
175 
176 
177 
178 
179 #ifdef DOXYGEN_ENGLISH
180 
229 #endif
230 #ifdef DOXYGEN_RUSSIAN
231 
284 #endif
285 int DSPL_API cheby_poly2(double* x, int n, int ord, double* y)
286 {
287  int k, m;
288  double t[2];
289 
290  if(!x || !y)
291  return ERROR_PTR;
292  if(n < 1)
293  return ERROR_SIZE;
294  if(ord<0)
295  return ERROR_POLY_ORD;
296  if(ord==0)
297  {
298  for(k = 0; k < n; k++)
299  {
300  y[k] = 1.0;
301  }
302  return RES_OK;
303  }
304 
305  if(ord==1)
306  {
307  for(k = 0; k < n; k++)
308  {
309  y[k] = 2.0*x[k];
310  };
311  return RES_OK;
312  }
313 
314  for(k = 0; k < n; k++)
315  {
316  m = 2;
317  t[1] = 2.0*x[k];
318  t[0] = 1.0;
319  while(m <= ord)
320  {
321  y[k] = 2.0 * x[k] *t[1] - t[0];
322  t[0] = t[1];
323  t[1] = y[k];
324  m++;
325  }
326  }
327  return RES_OK;
328 }
329 
#define ERROR_POLY_ORD
The polynomial order is incorrect. The order of the polynomial must be a positive integer.
Definition: dspl.h:544
int DSPL_API cheby_poly2(double *x, int n, int ord, double *y)
Chebyshev polynomial of the second kind order ord
Definition: cheby.c:285
#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
int DSPL_API cheby_poly1(double *x, int n, int ord, double *y)
Chebyshev polynomial of the first kind order ord
Definition: cheby.c:134
#define RES_OK
The function completed correctly. No errors.
Definition: dspl.h:497