Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame^] | 24 | #include "vec.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 25 | |
| 26 | // --------------------------------------------------------------------------- |
| 27 | |
| 28 | namespace android { |
| 29 | // --------------------------------------------------------------------------- |
| 30 | |
| 31 | SecondOrderLowPassFilter::SecondOrderLowPassFilter(float Q, float fc) |
| 32 | : iQ(1.0f / Q), fc(fc) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | void 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 Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame^] | 48 | template<typename T> |
| 49 | BiquadFilter<T>::BiquadFilter(const SecondOrderLowPassFilter& s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 50 | : s(s) |
| 51 | { |
| 52 | } |
| 53 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame^] | 54 | template<typename T> |
| 55 | T BiquadFilter<T>::init(const T& x) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 56 | { |
| 57 | x1 = x2 = x; |
| 58 | y1 = y2 = x; |
| 59 | return x; |
| 60 | } |
| 61 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame^] | 62 | template<typename T> |
| 63 | T BiquadFilter<T>::operator()(const T& x) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 64 | { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame^] | 65 | T y = (x + x2)*s.a0 + x1*s.a1 - y1*s.b1 - y2*s.b2; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 66 | x2 = x1; |
| 67 | y2 = y1; |
| 68 | x1 = x; |
| 69 | y1 = y; |
| 70 | return y; |
| 71 | } |
| 72 | |
| 73 | // --------------------------------------------------------------------------- |
Mathias Agopian | 87c9dbb | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 74 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame^] | 75 | template<typename T> |
| 76 | CascadedBiquadFilter<T>::CascadedBiquadFilter(const SecondOrderLowPassFilter& s) |
Mathias Agopian | 87c9dbb | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 77 | : mA(s), mB(s) |
| 78 | { |
| 79 | } |
| 80 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame^] | 81 | template<typename T> |
| 82 | T CascadedBiquadFilter<T>::init(const T& x) |
Mathias Agopian | 87c9dbb | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 83 | { |
| 84 | mA.init(x); |
| 85 | mB.init(x); |
| 86 | return x; |
| 87 | } |
| 88 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame^] | 89 | template<typename T> |
| 90 | T CascadedBiquadFilter<T>::operator()(const T& x) |
Mathias Agopian | 87c9dbb | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 91 | { |
| 92 | return mB(mA(x)); |
| 93 | } |
| 94 | |
| 95 | // --------------------------------------------------------------------------- |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame^] | 96 | |
| 97 | template class BiquadFilter<float>; |
| 98 | template class CascadedBiquadFilter<float>; |
| 99 | template class BiquadFilter<vec3_t>; |
| 100 | template class CascadedBiquadFilter<vec3_t>; |
| 101 | |
| 102 | // --------------------------------------------------------------------------- |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 103 | }; // namespace android |