15 #include <yarp/os/Log.h>
16 #include <yarp/math/Math.h>
20 using namespace yarp::sig;
21 using namespace yarp::math;
26 Filter::Filter(
const Vector &num,
const Vector &den,
const Vector &y0)
31 m=b.length();
n=a.length();
32 yAssert((m>0)&&(
n>0));
34 uold.insert(uold.begin(),m-1,
zeros((
int)y0.length()));
35 yold.insert(yold.begin(),
n-1,
zeros((
int)y0.length()));
42 void Filter::init(
const Vector &y0)
49 init(y0,
zeros((
int)y0.length()));
54 void Filter::init(
const Vector &y0,
const Vector &u0)
56 Vector u_init(y0.length(),0.0);
61 for (
size_t i=0; i<b.length(); i++)
65 for (
size_t i=0; i<a.length(); i++)
69 if (fabs(sum_b)>std::numeric_limits<double>::epsilon())
70 u_init=(sum_a/sum_b)*y0;
77 if (fabs(sum_a-a[0])>std::numeric_limits<double>::epsilon())
78 y_init=a[0]/(a[0]-sum_a)*
y;
82 for (
size_t i=0; i<yold.size(); i++)
85 for (
size_t i=0; i<uold.size(); i++)
91 void Filter::getCoeffs(Vector &num, Vector &den)
99 void Filter::setCoeffs(
const Vector &num,
const Vector &den)
107 m=b.length();
n=a.length();
108 yAssert((m>0)&&(
n>0));
110 uold.insert(uold.begin(),m-1,
zeros((
int)
y.length()));
111 yold.insert(yold.begin(),
n-1,
zeros((
int)
y.length()));
118 bool Filter::adjustCoeffs(
const Vector &num,
const Vector &den)
120 if ((num.length()==b.length()) && (den.length()==a.length()))
132 void Filter::getStates(deque<Vector> &u, deque<Vector> &
y)
140 const Vector& Filter::filt(
const Vector &u)
142 yAssert(
y.length()==u.length());
143 for (
size_t j=0; j<
y.length(); j++)
146 for (
size_t i=1; i<m; i++)
147 for (
size_t j=0; j<
y.length(); j++)
148 y[j]+=b[i]*uold[i-1][j];
150 for (
size_t i=1; i<
n; i++)
151 for (
size_t j=0; j<
y.length(); j++)
152 y[j]-=a[i]*yold[i-1][j];
154 for (
size_t j=0; j<
y.length(); j++)
168 RateLimiter::RateLimiter(
const Vector &rL,
const Vector &rU) :
169 rateLowerLim(rL), rateUpperLim(rU)
204 for (
size_t i=0; i<
n; i++)
219 const double sampleTime,
248 if (cutFrequency<=0.0)
284 double tau=1.0/(2.0*
M_PI*
fc);
285 Vector num=cat(
Ts,
Ts);
286 Vector den=cat(2.0*tau+
Ts,
Ts-2.0*tau);
306 yAssert(y0.length()>0);
309 uold.assign(
m,deque<double>());
325 size_t L=v.size()>>1;
326 nth_element(v.begin(),v.begin()+L,v.end());
331 nth_element(v.begin(),v.begin()+L-1,v.end());
332 return 0.5*(v[L]+v[L-1]);
340 yAssert(
y.length()==u.length());
341 for (
size_t i=0; i<
m; i++)
342 uold[i].push_front(u[i]);
344 if (
uold[0].size()>
n)
346 for (
size_t i=0; i<
m; i++)
virtual const yarp::sig::Vector & filt(const yarp::sig::Vector &u)
Performs filtering on the actual input.
bool adjustCoeffs(const yarp::sig::Vector &num, const yarp::sig::Vector &den)
Modifies the values of existing filter coefficients without varying their lengths.
virtual void init(const yarp::sig::Vector &y0)
Internal state reset.
bool setCutFrequency(const double cutFrequency)
Change the cut frequency of the filter.
bool setSampleTime(const double sampleTime)
Change the sample time of the filter.
FirstOrderLowPassFilter(const double cutFrequency, const double sampleTime, const yarp::sig::Vector &y0=yarp::sig::Vector(1, 0.0))
Creates a filter with specified parameters.
virtual void init(const yarp::sig::Vector &y0)
Internal state reset.
virtual const yarp::sig::Vector & filt(const yarp::sig::Vector &u)
Performs filtering on the actual input.
virtual ~FirstOrderLowPassFilter()
Destructor.
void setLimits(const yarp::sig::Vector &rL, const yarp::sig::Vector &rU)
Sets new Rate limits.
yarp::sig::Vector rateLowerLim
virtual void init(const yarp::sig::Vector &u0)
Init internal state.
void getLimits(yarp::sig::Vector &rL, yarp::sig::Vector &rU)
Returns the current Rate limits.
yarp::sig::Vector rateUpperLim
virtual const yarp::sig::Vector & filt(const yarp::sig::Vector &u)
Limits the input rate.