libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
group_delay.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 
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 #include "dspl.h"
26 
27 
28 
29 #ifdef DOXYGEN_ENGLISH
30 
93 #endif
94 #ifdef DOXYGEN_RUSSIAN
95 
160 #endif
161 int DSPL_API group_delay(double* pb, double* pa, int ord, int flag,
162  double* w, int n, double* tau)
163 {
164  double a, b, c, d, da, db, dc, dd, f, e;
165  int t, m;
166 
167  double *qa = NULL;
168 
169  if(!pb || !w || !tau || (!pa && (flag & DSPL_FLAG_ANALOG)))
170  return ERROR_PTR;
171  if(ord < 1)
172  return ERROR_FILTER_ORD;
173  if(n < 1)
174  return ERROR_SIZE;
175 
176 
177  if(pa)
178  qa = pa;
179  else
180  {
181  qa = (double*)malloc((ord+1) * sizeof(double));
182  memset(qa, 0, (ord+1) * sizeof(double));
183  qa[0] = 1.0;
184  }
185 
186  for(t = 0; t < n; t++)
187  {
188  a = b = c = d = da = db = dc = dd = 0.0;
189  if(flag & DSPL_FLAG_ANALOG)
190  {
191  for(m = 0; m < ord+1; m+=4)
192  {
193  a += pb[m] * pow(w[t], (double)m);
194  c += qa[m] * pow(w[t], (double)m);
195  da += pb[m] * (double) m * pow(w[t], (double)(m-1));
196  dc += qa[m] * (double) m * pow(w[t], (double)(m-1));
197  }
198  for(m = 2; m < ord+1; m+=4)
199  {
200  a -= pb[m] * pow(w[t], (double)m);
201  c -= qa[m] * pow(w[t], (double)m);
202  da -= pb[m] * (double) m * pow(w[t], (double)(m-1));
203  dc -= qa[m] * (double) m * pow(w[t], (double)(m-1));
204  }
205 
206  for(m = 1; m < ord+1; m+=4)
207  {
208  b += pb[m] * pow(w[t], (double)m) ;
209  d += qa[m] * pow(w[t], (double)m) ;
210  db += pb[m] * (double) m * pow(w[t], (double)(m-1)) ;
211  dd += qa[m] * (double) m * pow(w[t], (double)(m-1)) ;
212  }
213 
214  for(m = 3; m < ord+1; m+=4)
215  {
216  b -= pb[m] * pow(w[t], (double)m) ;
217  d -= qa[m] * pow(w[t], (double)m) ;
218  db -= pb[m] * (double) m * pow(w[t], (double)(m-1)) ;
219  dd -= qa[m] * (double) m * pow(w[t], (double)(m-1)) ;
220  }
221 
222  }
223  else
224  {
225  for(m = 0; m < ord+1; m++)
226  {
227  a += pb[m] * cos(w[t]*(double)m);
228  b -= pb[m] * sin(w[t]*(double)m);
229  c += qa[m] * cos(w[t]*(double)m);
230  d -= qa[m] * sin(w[t]*(double)m);
231 
232  da -= pb[m] *(double)m * sin(w[t]*(double)m);
233  db -= pb[m] *(double)m * cos(w[t]*(double)m);
234  dc -= qa[m] *(double)m * sin(w[t]*(double)m);
235  dd -= qa[m] *(double)m * cos(w[t]*(double)m);
236  }
237  }
238 
239  f = da * c + a * dc + db * d + b * dd;
240  e = db * c + b * dc - da * d - a * dd;
241  tau[t] = (f * (b * c - a * d) - e * (a * c + b * d)) /
242  ((a * a + b * b) * (c * c + d * d));
243  }
244 
245  if(qa != pa)
246  free(qa);
247 
248  return RES_OK;
249 }
int group_delay(double *pb, double *pa, int ord, int flag, double *w, int n, double *tau)
Расчет группового времени запаздывания цифрового или аналогового фильтра.
Definition: group_delay.c:161
#define ERROR_FILTER_ORD
Порядок фильтра задан неверно. Порядок фильтра должен быть задан положительным целым значением.
Definition: dspl.h:575
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
#define ERROR_SIZE
Ошибка при передаче размера массива. Данная ошибка возникает когда помимо указателя на массив входных...
Definition: dspl.h:618
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558