libdspl-2.0
Digital Signal Processing Algorithm Library
filter_iir.c
1/*
2* Copyright (c) 2015-2022 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 "dspl.h"
24
25
26#ifdef DOXYGEN_ENGLISH
98#endif
99#ifdef DOXYGEN_RUSSIAN
166#endif
167int DSPL_API filter_iir(double* b, double* a, int ord,
168 double* x, int n, double* y)
169{
170 double *buf = NULL;
171 double *an = NULL;
172 double *bn = NULL;
173 double u;
174 int k;
175 int m;
176 int count;
177
178 if(!b || !x || !y)
179 return ERROR_PTR;
180
181 if(ord < 1 || n < 1)
182 return ERROR_SIZE;
183
184 if(a && a[0]==0.0)
185 return ERROR_FILTER_A0;
186
187 count = ord + 1;
188 buf = (double*) malloc(count*sizeof(double));
189 an = (double*) malloc(count*sizeof(double));
190
191 memset(buf, 0, count*sizeof(double));
192
193 if(!a)
194 {
195 memset(an, 0, count*sizeof(double));
196 bn = b;
197 }
198 else
199 {
200 bn = (double*) malloc(count*sizeof(double));
201 for(k = 0; k < count; k++)
202 {
203 an[k] = a[k] / a[0];
204 bn[k] = b[k] / a[0];
205 }
206 }
207
208 for(k = 0; k < n; k++)
209 {
210 for(m = ord; m > 0; m--)
211 buf[m] = buf[m-1];
212 u = 0.0;
213 for(m = ord; m > 0; m--)
214 u += buf[m]*an[m];
215
216 buf[0] = x[k] - u;
217 y[k] = 0.0;
218 for(m = 0; m < count; m++)
219 y[k] += buf[m] * bn[m];
220 }
221
222 if(buf)
223 free(buf);
224 if(an)
225 free(an);
226 if(bn && (bn != b))
227 free(bn);
228 return RES_OK;
229}
230
#define ERROR_FILTER_A0
Parameter cannot be zero digital for IIR filter transfer characteristic .
Definition: dspl.h:572
#define RES_OK
The function completed correctly. No errors.
Definition: dspl.h:558
#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 filter_iir(double *b, double *a, int ord, double *x, int n, double *y)
Real IIR filtration.
Definition: filter_iir.c:167