libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
ratcompos.c
1 /*
2 * Copyright (c) 2015-2024 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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "dspl.h"
25 
26 
27 
28 
29 
30 #ifdef DOXYGEN_ENGLISH
31 
102 #endif
103 #ifdef DOXYGEN_RUSSIAN
104 
179 #endif
180 int DSPL_API ratcompos(double* b, double* a, int n,
181  double* c, double* d, int p,
182  double* beta, double* alpha)
183 {
184 
185  int k2, i, k, pn, pd, ln, ld, k2s, nk2s;
186  double *num = NULL, *den = NULL, *ndn = NULL, *ndd = NULL;
187  int res;
188 
189  if (!a || !b || !c || !d || !beta || !alpha)
190  {
191  res = ERROR_PTR;
192  goto exit_label;
193  }
194  if(n < 1 || p < 1)
195  {
196  res = ERROR_SIZE;
197  goto exit_label;
198  }
199 
200  k2 = (n*p)+1;
201  k2s = k2*sizeof(double); /* alpha and beta size */
202  nk2s = (n+1)*k2*sizeof(double); /* num, den, ndn and ndd size */
203 
204  num = (double*)malloc(nk2s);
205  den = (double*)malloc(nk2s);
206  ndn = (double*)malloc(nk2s);
207  ndd = (double*)malloc(nk2s);
208 
209  memset(num, 0, nk2s);
210  memset(den, 0, nk2s);
211  memset(ndn, 0, nk2s);
212  memset(ndd, 0, nk2s);
213 
214 
215  num[0] = den[0] = 1.0;
216  pn = 0;
217  ln = 1;
218  for(i = 1; i < n+1; i++)
219  {
220  res = conv(num+pn, ln, c, p+1, num+pn+k2);
221  if(res!=RES_OK)
222  goto exit_label;
223  res = conv(den+pn, ln, d, p+1, den+pn+k2);
224  if(res!=RES_OK)
225  goto exit_label;
226  pn += k2;
227  ln += p;
228  }
229 
230  pn = 0;
231  pd = n*k2;
232  ln = 1;
233  ld = k2;
234 
235  for (i = 0; i < n+1; i++)
236  {
237  res = conv(num + pn, ln, den + pd, ld, ndn + i*k2);
238  if(res!=RES_OK)
239  goto exit_label;
240  ln += p;
241  ld -= p;
242  pn += k2;
243  pd -= k2;
244  }
245 
246  for (i = 0; i < n+1; i++)
247  {
248  for (k = 0; k < k2; k++)
249  {
250  ndd[i*k2 + k] = ndn[i*k2 + k] * a[i];
251  ndn[i*k2 + k] *= b[i];
252  }
253  }
254 
255 
256  memset(alpha, 0, k2s);
257  memset(beta, 0, k2s);
258 
259  for (k = 0; k < k2; k++)
260  {
261  for (i = 0; i < n+1; i++)
262  {
263  beta[k] += ndn[i*k2 + k];
264  alpha[k] += ndd[i*k2 + k];
265  }
266  }
267 
268  res = RES_OK;
269 
270 exit_label:
271  if(num)
272  free(num);
273  if(den)
274  free(den);
275  if(ndn)
276  free(ndn);
277  if(ndd)
278  free(ndd);
279 
280  return res;
281 }
282 
int ratcompos(double *b, double *a, int n, double *c, double *d, int p, double *beta, double *alpha)
Рациональная композиця
Definition: ratcompos.c:180
int conv(double *a, int na, double *b, int nb, double *c)
Линейная свертка двух вещественных векторов
Definition: conv.c:157
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
#define ERROR_SIZE
Ошибка при передаче размера массива. Данная ошибка возникает когда помимо указателя на массив входных...
Definition: dspl.h:618
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558