interpolator_linear.h revision 6cc3a9948b51193dfdcb0c3527d7f3d1ca38aa3c
1/*
2 * Copyright (C) 2013 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#ifndef LE_FX_ENGINE_DSP_CORE_INTERPOLATOR_LINEAR_H_
18#define LE_FX_ENGINE_DSP_CORE_INTERPOLATOR_LINEAR_H_
19
20#include <math.h>
21#include "dsp/core/interpolator_base.h"
22
23namespace le_fx {
24
25namespace sigmod {
26
27// Linear interpolation class.
28//
29// The main functionality of this class is provided by it's base-class, so
30// please refer to: InterpolatorBase
31//
32// Example:
33//  InterpolatorLinear<float> interp(x_data, y_data, data_length);
34//  for (int n = 0; n < data_length; n++) Y[n] = interp.Interpolate(X[n]);
35//
36template <typename T>
37class InterpolatorLinear: public InterpolatorBase<T, InterpolatorLinear<T> > {
38 public:
39  InterpolatorLinear() { }
40  ~InterpolatorLinear() { }
41
42 protected:
43  // Provides the main implementation of the linear interpolation algorithm.
44  // Assumes that: X[cached_index_] < x < X[cached_index_ + 1]
45  T MethodSpecificInterpolation(T x);
46
47  // Pre-compute internal state_ parameters.
48  bool SetInternalState();
49
50 private:
51  friend class InterpolatorBase<T, InterpolatorLinear<T> >;
52  typedef InterpolatorBase<T, InterpolatorLinear<T> > BaseClass;
53  using BaseClass::status_;
54  using BaseClass::cached_index_;
55  using BaseClass::x_data_;
56  using BaseClass::y_data_;
57  using BaseClass::data_length_;
58  using BaseClass::state_;
59
60  LE_FX_DISALLOW_COPY_AND_ASSIGN(InterpolatorLinear<T>);
61};
62
63template <typename T>
64inline T InterpolatorLinear<T>::MethodSpecificInterpolation(T x) {
65  T dX = x_data_[cached_index_ + 1] - x_data_[cached_index_];
66  T dY = y_data_[cached_index_ + 1] - y_data_[cached_index_];
67  T dx = x - x_data_[cached_index_];
68  return y_data_[cached_index_] + (dY * dx) / dX;
69}
70
71template <typename T>
72bool InterpolatorLinear<T>::SetInternalState() {
73  state_ = NULL;
74  return true;
75}
76
77}  // namespace sigmod
78
79}  // namespace le_fx
80
81#endif  // LE_FX_ENGINE_DSP_CORE_INTERPOLATOR_LINEAR_H_
82