libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
filter_freq_resp.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 
126 #endif
127 #ifdef DOXYGEN_RUSSIAN
128 
230 #endif
231 int DSPL_API filter_freq_resp(double* b, double* a, int ord,
232  double* w, int n, int flag,
233  double* mag, double* phi, double* tau)
234 {
235  int res, k, flag_analog;
236 
237  complex_t *hc = NULL;
238  double *phi0 = NULL;
239  double *phi1 = NULL;
240  double *w0 = NULL;
241  double *w1 = NULL;
242 
243  if(!b || !w)
244  return ERROR_PTR;
245  if(ord < 1)
246  return ERROR_FILTER_ORD;
247  if(n < 1)
248  return ERROR_SIZE;
249 
250  flag_analog = flag & DSPL_FLAG_ANALOG;
251 
252  hc = (complex_t*) malloc (n*sizeof(complex_t));
253 
254  res = flag_analog ?
255  freqs(b, a, ord, w, n, hc) :
256  freqz(b, a, ord, w, n, hc);
257 
258  if(res != RES_OK)
259  goto exit_label;
260 
261 
262  if(mag)
263  {
264  if(flag & DSPL_FLAG_LOGMAG)
265  {
266  for(k = 0; k < n; k++)
267  mag[k] = 10.0 * log10(ABSSQR(hc[k]));
268  }
269  else
270  {
271  for(k = 0; k < n; k++)
272  mag[k] = sqrt(ABSSQR(hc[k]));
273  }
274  }
275 
276 
277  if(phi)
278  {
279  for(k = 0; k < n; k++)
280  phi[k] = atan2(IM(hc[k]), RE(hc[k]));
281 
282  if(flag & DSPL_FLAG_UNWRAP)
283  {
284  res = unwrap(phi, n, M_2PI, 0.8);
285  if(res != RES_OK)
286  goto exit_label;
287  }
288  }
289 
290 
291  if(tau)
292  res = group_delay(b, a, ord, flag, w, n, tau);
293 
294 
295 
296 exit_label:
297  if(hc)
298  free(hc);
299  if(phi0)
300  free(phi0);
301  if(phi1)
302  free(phi1);
303  if(w0)
304  free(w0);
305  if(w1)
306  free(w1);
307  return res;
308 }
309 
310 
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 RE(x)
Макрос определяющий реальную часть комплексного числа.
Definition: dspl.h:420
#define ERROR_PTR
Ошибка указателя. Данная ошибка означает, что один из обязательных указателей (память под который дол...
Definition: dspl.h:610
#define ERROR_SIZE
Ошибка при передаче размера массива. Данная ошибка возникает когда помимо указателя на массив входных...
Definition: dspl.h:618
int freqz(double *b, double *a, int ord, double *w, int n, complex_t *h)
Расчет комплексного коэффициента передачи цифрового фильтра.
Definition: freqz.c:155
double complex_t[2]
Описание комплексного типа данных.
Definition: dspl.h:86
int filter_freq_resp(double *b, double *a, int ord, double *w, int n, int flag, double *mag, double *phi, double *tau)
Расчет амплитудно-частотной (АЧХ), фазочастотной характеристик (ФЧХ), а также группового времени запа...
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558
#define ABSSQR(x)
Макрос возвращает квадрат модуля комплексного числа x.
Definition: dspl.h:534
int freqs(double *b, double *a, int ord, double *w, int n, complex_t *h)
Расчет комплексного коэффициента передачи аналогового фильтра.
Definition: freqs.c:143
#define IM(x)
Макрос определяющий мнимую часть комплексного числа.
Definition: dspl.h:478