SDRAngel
4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
sdrbase
dsp
iirfilter.h
Go to the documentation of this file.
1
// Copyright (C) 2017 F4EXB //
3
// written by Edouard Griffiths //
4
// //
5
// This program is free software; you can redistribute it and/or modify //
6
// it under the terms of the GNU General Public License as published by //
7
// the Free Software Foundation as version 3 of the License, or //
8
// (at your option) any later version. //
9
// //
10
// This program is distributed in the hope that it will be useful, //
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
13
// GNU General Public License V3 for more details. //
14
// //
15
// You should have received a copy of the GNU General Public License //
16
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
18
24
#ifndef SDRBASE_DSP_IIRFILTER_H_
25
#define SDRBASE_DSP_IIRFILTER_H_
26
27
#include <stdint.h>
28
#include <string.h>
29
#include <assert.h>
30
31
template
<
typename
Type, u
int
32_t Order>
class
IIRFilter
32
{
33
public
:
34
IIRFilter
(
const
Type *a,
const
Type *b);
35
~IIRFilter
();
36
void
setCoeffs
(
const
Type *a,
const
Type *b);
37
Type
run
(
const
Type& sample);
38
39
private
:
40
Type *
m_a
;
41
Type *
m_b
;
42
Type *
m_x
;
43
Type *
m_y
;
44
};
45
46
template
<
typename
Type>
class
IIRFilter
<Type, 2>
47
{
48
public
:
49
IIRFilter
(
const
Type *a,
const
Type *b);
50
~IIRFilter
();
51
void
setCoeffs
(
const
Type *a,
const
Type *b);
52
Type
run
(
const
Type& sample);
53
54
private
:
55
Type
m_a
[3];
56
Type
m_b
[3];
57
Type
m_x
[2];
58
Type
m_y
[2];
59
};
60
61
template
<
typename
Type, u
int
32_t Order>
62
IIRFilter<Type, Order>::IIRFilter
(
const
Type *a,
const
Type *b)
63
{
64
assert(Order > 1);
65
66
m_a
=
new
Type[Order+1];
67
m_b
=
new
Type[Order+1];
68
m_x
=
new
Type[Order];
69
m_y
=
new
Type[Order];
70
71
setCoeffs
(a, b);
72
}
73
74
template
<
typename
Type, u
int
32_t Order>
75
IIRFilter<Type, Order>::~IIRFilter
()
76
{
77
delete
[]
m_y
;
78
delete
[]
m_x
;
79
delete
[]
m_b
;
80
delete
[]
m_a
;
81
}
82
83
template
<
typename
Type, u
int
32_t Order>
84
void
IIRFilter<Type, Order>::setCoeffs
(
const
Type *a,
const
Type *b)
85
{
86
memcpy(
m_a
, b, (Order+1)*
sizeof
(Type));
87
memcpy(
m_b
, a, (Order+1)*
sizeof
(Type));
88
89
for
(
uint32_t
i
= 0;
i
< Order;
i
++)
90
{
91
m_x
[
i
] = 0;
92
m_y
[
i
] = 0;
93
}
94
}
95
96
97
template
<
typename
Type, u
int
32_t Order>
98
Type
IIRFilter<Type, Order>::run
(
const
Type& sample)
99
{
100
Type y =
m_b
[0] * sample;
101
102
for
(
uint32_t
i
= Order;
i
> 0;
i
--)
103
{
104
y +=
m_b
[
i
] *
m_x
[
i
-1] +
m_a
[
i
] *
m_y
[
i
-1];
105
106
if
(
i
> 1)
// shift
107
{
108
m_x
[
i
-1] =
m_x
[
i
-2];
109
m_y[
i
-1] = m_y[
i
-2];
110
}
111
}
112
113
// last shift
114
m_x
[0] = sample;
115
m_y
[0] = y;
116
117
return
y;
118
}
119
120
121
template
<
typename
Type>
122
IIRFilter<Type, 2>::IIRFilter
(
const
Type *a,
const
Type *b)
123
{
124
setCoeffs
(a, b);
125
}
126
127
template
<
typename
Type>
128
IIRFilter<Type, 2>::~IIRFilter
()
129
{
130
}
131
132
template
<
typename
Type>
133
void
IIRFilter<Type, 2>::setCoeffs
(
const
Type *a,
const
Type *b)
134
{
135
m_a
[0] = a[0];
136
m_a
[1] = a[1];
137
m_a
[2] = a[2];
138
m_b
[0] = b[0];
139
m_b
[1] = b[1];
140
m_b
[2] = b[2];
141
m_x
[0] = 0;
142
m_x
[1] = 0;
143
m_y
[0] = 0;
144
m_y
[1] = 0;
145
}
146
147
template
<
typename
Type>
148
Type
IIRFilter<Type, 2>::run
(
const
Type& sample)
149
{
150
Type y =
m_b
[0]*sample +
m_b
[1]*
m_x
[0] +
m_b
[2]*
m_x
[1] +
m_a
[1]*
m_y
[0] +
m_a
[2]*
m_y
[1];
// this is y[n]
151
152
m_x
[1] =
m_x
[0];
153
m_x
[0] = sample;
154
155
m_y[1] = m_y[0];
156
m_y[0] = y;
157
158
return
y;
159
}
160
161
#endif
/* SDRBASE_DSP_IIRFILTER_H_ */
IIRFilter
Definition:
iirfilter.h:31
IIRFilter::run
Type run(const Type &sample)
Definition:
iirfilter.h:98
IIRFilter::m_a
Type * m_a
Definition:
iirfilter.h:40
IIRFilter::setCoeffs
void setCoeffs(const Type *a, const Type *b)
Definition:
iirfilter.h:84
IIRFilter::m_y
Type * m_y
Definition:
iirfilter.h:43
uint32_t
unsigned int uint32_t
Definition:
rtptypes_win.h:46
IIRFilter::m_x
Type * m_x
Definition:
iirfilter.h:42
i
int32_t i
Definition:
decimators.h:244
IIRFilter::m_b
Type * m_b
Definition:
iirfilter.h:41
IIRFilter::~IIRFilter
~IIRFilter()
Definition:
iirfilter.h:75
IIRFilter::IIRFilter
IIRFilter(const Type *a, const Type *b)
Definition:
iirfilter.h:62
Generated on Fri Aug 2 2019 17:56:33 for SDRAngel by
1.8.13