/
filters.cpp
163 lines (128 loc) · 4.31 KB
/
filters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright (C) 2014 RobotCub Consortium, European Commission FP6 Project IST-004370
* Author: Silvio Traversaro
* email: silvio.traversaro@iit.it
* website: www.icub.org
* Permission is granted to copy, distribute, and/or modify this program
* under the terms of the GNU General Public License, version 2 or any
* later version published by the Free Software Foundation.
*
* A copy of the license can be found at
* http://www.robotcub.org/icub/license/gpl.txt
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details
*/
#include "filters.h"
#include <yarp/math/Math.h>
using namespace std;
using namespace yarp::sig;
using namespace yarp::math;
using namespace iCub::ctrl::realTime;
/***************************************************************************/
Eigen::Map<Eigen::VectorXd> toEigen(yarp::sig::Vector & vec)
{
return Eigen::Map<Eigen::VectorXd>(vec.data(),vec.size());
}
Eigen::Map<const Eigen::VectorXd> toEigen(const yarp::sig::Vector & vec)
{
return Eigen::Map<const Eigen::VectorXd>(vec.data(),vec.size());
}
/***************************************************************************/
Filter::Filter(const Vector &num, const Vector &den, const Vector &y0)
{
b=toEigen(num);
a=toEigen(den);
m=b.size(); uold.resize(y0.size(),m-1);
n=a.size(); yold.resize(y0.size(),m-1);
init(y0);
}
/***************************************************************************/
void Filter::init(const Vector &y0)
{
// otherwise use zero
init(y0,yarp::math::zeros(y0.length()));
}
/***************************************************************************/
void Filter::init(const Vector &y0, const Vector &u0)
{
Vector u_init(y0.length(),0.0);
Vector y_init=y0;
y=y0;
double sum_b=0.0;
for (size_t i=0; i<b.size(); i++)
sum_b+=b[i];
double sum_a=0.0;
for (size_t i=0; i<a.size(); i++)
sum_a+=a[i];
if (fabs(sum_b)>1e-9) // if filter DC gain is not zero
u_init=(sum_a/sum_b)*y0;
else
{
// if filter gain is zero then you need to know in advance what
// the next input is going to be for initializing (that is u0)
// Note that, unless y0=0, the filter output is not going to be stable
u_init=u0;
if (fabs(sum_a-a[0])>1e-9)
y_init=a[0]/(a[0]-sum_a)*y;
// if sum_a==a[0] then the filter can only be initialized to zero
}
for (size_t i=0; i<yold.cols(); i++)
yold.col(i)=toEigen(y_init);
yold_last_column_sample = 0;
for (size_t i=0; i<uold.cols(); i++)
uold.col(i)=toEigen(u_init);
uold_last_column_sample = 0;
}
/***************************************************************************/
void Filter::getCoeffs(Vector &num, Vector &den)
{
toEigen(num)=b;
toEigen(den)=a;
}
/***************************************************************************/
void Filter::setCoeffs(const Vector &num, const Vector &den)
{
b=toEigen(num);
a=toEigen(den);
uold.setZero();
yold.setZero();
m=b.size(); uold.resize(y.size(),m-1);
n=a.size(); yold.resize(y.size(),n-1);
init(y);
}
/***************************************************************************/
bool Filter::adjustCoeffs(const Vector &num, const Vector &den)
{
if ((num.length()==b.size()) && (den.length()==a.size()))
{
(b)=toEigen(num);
(a)=toEigen(den);
return true;
}
else
return false;
}
/***************************************************************************/
const Vector & Filter::filt(const Vector &u)
{
toEigen(y)=b[0]*toEigen(u);
for (size_t i=1; i<m; i++)
{
toEigen(y)+=b[i]*uold.col((m-i+uold_last_column_sample)%(m-1));
}
for (size_t i=1; i<n; i++)
{
toEigen(y)-=a[i]*yold.col((n-i+yold_last_column_sample)%(n-1));
}
toEigen(y)=(1.0/a[0])*toEigen(y);
uold_last_column_sample++;
uold_last_column_sample = uold_last_column_sample%uold.cols();
uold.col(uold_last_column_sample) = toEigen(u);
yold_last_column_sample++;
yold_last_column_sample = yold_last_column_sample%yold.cols();
yold.col(yold_last_column_sample) = toEigen(y);
return y;
}