blob: c76dd4cff5022fad6a47cc15d6d4d834a3a01419 [file] [log] [blame]
Mathias Agopianf001c922010-11-11 17:58:51 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
18#include <sys/types.h>
19#include <math.h>
20
21#include <cutils/log.h>
22
23#include "SecondOrderLowPassFilter.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070024#include "vec.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080025
26// ---------------------------------------------------------------------------
27
28namespace android {
29// ---------------------------------------------------------------------------
30
31SecondOrderLowPassFilter::SecondOrderLowPassFilter(float Q, float fc)
32 : iQ(1.0f / Q), fc(fc)
33{
34}
35
36void SecondOrderLowPassFilter::setSamplingPeriod(float dT)
37{
38 K = tanf(float(M_PI) * fc * dT);
39 iD = 1.0f / (K*K + K*iQ + 1);
40 a0 = K*K*iD;
41 a1 = 2.0f * a0;
42 b1 = 2.0f*(K*K - 1)*iD;
43 b2 = (K*K - K*iQ + 1)*iD;
44}
45
46// ---------------------------------------------------------------------------
47
Mathias Agopian984826c2011-05-17 22:54:42 -070048template<typename T>
49BiquadFilter<T>::BiquadFilter(const SecondOrderLowPassFilter& s)
Mathias Agopianf001c922010-11-11 17:58:51 -080050 : s(s)
51{
52}
53
Mathias Agopian984826c2011-05-17 22:54:42 -070054template<typename T>
55T BiquadFilter<T>::init(const T& x)
Mathias Agopianf001c922010-11-11 17:58:51 -080056{
57 x1 = x2 = x;
58 y1 = y2 = x;
59 return x;
60}
61
Mathias Agopian984826c2011-05-17 22:54:42 -070062template<typename T>
63T BiquadFilter<T>::operator()(const T& x)
Mathias Agopianf001c922010-11-11 17:58:51 -080064{
Mathias Agopian984826c2011-05-17 22:54:42 -070065 T y = (x + x2)*s.a0 + x1*s.a1 - y1*s.b1 - y2*s.b2;
Mathias Agopianf001c922010-11-11 17:58:51 -080066 x2 = x1;
67 y2 = y1;
68 x1 = x;
69 y1 = y;
70 return y;
71}
72
73// ---------------------------------------------------------------------------
Mathias Agopian87c9dbb2010-11-11 17:58:51 -080074
Mathias Agopian984826c2011-05-17 22:54:42 -070075template<typename T>
76CascadedBiquadFilter<T>::CascadedBiquadFilter(const SecondOrderLowPassFilter& s)
Mathias Agopian87c9dbb2010-11-11 17:58:51 -080077 : mA(s), mB(s)
78{
79}
80
Mathias Agopian984826c2011-05-17 22:54:42 -070081template<typename T>
82T CascadedBiquadFilter<T>::init(const T& x)
Mathias Agopian87c9dbb2010-11-11 17:58:51 -080083{
84 mA.init(x);
85 mB.init(x);
86 return x;
87}
88
Mathias Agopian984826c2011-05-17 22:54:42 -070089template<typename T>
90T CascadedBiquadFilter<T>::operator()(const T& x)
Mathias Agopian87c9dbb2010-11-11 17:58:51 -080091{
92 return mB(mA(x));
93}
94
95// ---------------------------------------------------------------------------
Mathias Agopian984826c2011-05-17 22:54:42 -070096
97template class BiquadFilter<float>;
98template class CascadedBiquadFilter<float>;
99template class BiquadFilter<vec3_t>;
100template class CascadedBiquadFilter<vec3_t>;
101
102// ---------------------------------------------------------------------------
Mathias Agopianf001c922010-11-11 17:58:51 -0800103}; // namespace android