|
|
|
|
@ -15,10 +15,16 @@
@@ -15,10 +15,16 @@
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* IIR filter |
|
|
|
|
* See: https://cdn.mikroe.com/ebooks/img/8/2016/02/digital-filter-design-chapter-03-image-2-9.gif
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
#ifndef SDRBASE_DSP_IIRFILTER_H_ |
|
|
|
|
#define SDRBASE_DSP_IIRFILTER_H_ |
|
|
|
|
|
|
|
|
|
#include <stdint.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
#include <assert.h> |
|
|
|
|
|
|
|
|
|
template <typename Type, uint32_t Order> class IIRFilter |
|
|
|
|
@ -26,7 +32,8 @@ template <typename Type, uint32_t Order> class IIRFilter
@@ -26,7 +32,8 @@ template <typename Type, uint32_t Order> class IIRFilter
|
|
|
|
|
public: |
|
|
|
|
IIRFilter(const Type *a, const Type *b); |
|
|
|
|
~IIRFilter(); |
|
|
|
|
Type run(Type sample); |
|
|
|
|
void setCoeffs(const Type *a, const Type *b); |
|
|
|
|
Type run(const Type& sample); |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
Type *m_a; |
|
|
|
|
@ -40,7 +47,8 @@ template <typename Type> class IIRFilter<Type, 2>
@@ -40,7 +47,8 @@ template <typename Type> class IIRFilter<Type, 2>
|
|
|
|
|
public: |
|
|
|
|
IIRFilter(const Type *a, const Type *b); |
|
|
|
|
~IIRFilter(); |
|
|
|
|
Type run(Type sample); |
|
|
|
|
void setCoeffs(const Type *a, const Type *b); |
|
|
|
|
Type run(const Type& sample); |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
Type m_a[3]; |
|
|
|
|
@ -59,14 +67,7 @@ IIRFilter<Type, Order>::IIRFilter(const Type *a, const Type *b)
@@ -59,14 +67,7 @@ IIRFilter<Type, Order>::IIRFilter(const Type *a, const Type *b)
|
|
|
|
|
m_x = new Type[Order]; |
|
|
|
|
m_y = new Type[Order]; |
|
|
|
|
|
|
|
|
|
memcpy(m_a, a, (Order+1)*sizeof(Type)); |
|
|
|
|
memcpy(m_b, b, (Order+1)*sizeof(Type)); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Order; i++) |
|
|
|
|
{ |
|
|
|
|
m_x[i] = 0; |
|
|
|
|
m_y[i] = 0; |
|
|
|
|
} |
|
|
|
|
setCoeffs(a, b); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename Type, uint32_t Order> |
|
|
|
|
@ -79,23 +80,56 @@ IIRFilter<Type, Order>::~IIRFilter()
@@ -79,23 +80,56 @@ IIRFilter<Type, Order>::~IIRFilter()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename Type, uint32_t Order> |
|
|
|
|
Type IIRFilter<Type, Order>::run(Type sample) |
|
|
|
|
void IIRFilter<Type, Order>::setCoeffs(const Type *a, const Type *b) |
|
|
|
|
{ |
|
|
|
|
Type y = m_a[0] * sample; |
|
|
|
|
memcpy(m_a, b, (Order+1)*sizeof(Type)); |
|
|
|
|
memcpy(m_b, a, (Order+1)*sizeof(Type)); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Order; i++) |
|
|
|
|
for (uint32_t i = 0; i < Order; i++) |
|
|
|
|
{ |
|
|
|
|
y += m_a[i+1] * m_x[i] + m_b[i+1] * m_y[i]; |
|
|
|
|
m_x[i] = 0; |
|
|
|
|
m_y[i] = 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Type, uint32_t Order> |
|
|
|
|
Type IIRFilter<Type, Order>::run(const Type& sample) |
|
|
|
|
{ |
|
|
|
|
Type y = m_b[0] * sample; |
|
|
|
|
|
|
|
|
|
for (uint32_t i = Order; i > 0; i--) |
|
|
|
|
{ |
|
|
|
|
y += m_b[i] * m_x[i-1] + m_a[i] * m_y[i-1]; |
|
|
|
|
|
|
|
|
|
if (i > 1) // shift
|
|
|
|
|
{ |
|
|
|
|
m_x[i-1] = m_x[i-2]; |
|
|
|
|
m_y[i-1] = m_y[i-2]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// last shift
|
|
|
|
|
m_x[0] = sample; |
|
|
|
|
m_y[0] = y; |
|
|
|
|
|
|
|
|
|
memcpy(&m_x[1], &m_x[0], (Order-1)*sizeof(Type)); |
|
|
|
|
return y; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Type> |
|
|
|
|
IIRFilter<Type, 2>::IIRFilter(const Type *a, const Type *b) |
|
|
|
|
{ |
|
|
|
|
setCoeffs(a, b); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename Type> |
|
|
|
|
IIRFilter<Type, 2>::~IIRFilter() |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename Type> |
|
|
|
|
void IIRFilter<Type, 2>::setCoeffs(const Type *a, const Type *b) |
|
|
|
|
{ |
|
|
|
|
m_a[0] = a[0]; |
|
|
|
|
m_a[1] = a[1]; |
|
|
|
|
@ -103,17 +137,16 @@ IIRFilter<Type, 2>::IIRFilter(const Type *a, const Type *b)
@@ -103,17 +137,16 @@ IIRFilter<Type, 2>::IIRFilter(const Type *a, const Type *b)
|
|
|
|
|
m_b[0] = b[0]; |
|
|
|
|
m_b[1] = b[1]; |
|
|
|
|
m_b[2] = b[2]; |
|
|
|
|
m_x[0] = 0; |
|
|
|
|
m_x[1] = 0; |
|
|
|
|
m_y[0] = 0; |
|
|
|
|
m_y[1] = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename Type> |
|
|
|
|
IIRFilter<Type, 2>::~IIRFilter() |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename Type> |
|
|
|
|
Type IIRFilter<Type, 2>::run(Type sample) |
|
|
|
|
Type IIRFilter<Type, 2>::run(const Type& sample) |
|
|
|
|
{ |
|
|
|
|
Type y = m_a[0]*sample + m_a[1]*m_x[0] + m_a[2]*m_x[1] + m_b[1]*m_y[0] + m_b[2]*m_y[1]; // this is y[n]
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
|
|
m_x[1] = m_x[0]; |
|
|
|
|
m_x[0] = sample; |
|
|
|
|
|