Home | History | Annotate | Download | only in core
      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_BASE_H_
     18 #define LE_FX_ENGINE_DSP_CORE_INTERPOLATOR_BASE_H_
     19 
     20 #include "common/core/types.h"
     21 
     22 namespace le_fx {
     23 
     24 namespace sigmod {
     25 
     26 // Interpolation base-class that provides the interface, while it is the derived
     27 // class that provides the specific interpolation algorithm. The following list
     28 // of interpolation algorithms are currently present:
     29 //
     30 // InterpolationSine<T>: weighted interpolation between y_data[n] and
     31 //                       y_data[n+1] using a sin(.) weighting factor from
     32 //                       0 to pi/4.
     33 // InterpolationLinear<T>: linear interpolation
     34 // InterpolationSplines<T>: spline-based interpolation
     35 //
     36 // Example (using derived spline-based interpolation class):
     37 //  InterpolatorSplines<float> interp(x_data, y_data, data_length);
     38 //  for (int n = 0; n < data_length; n++) Y[n] = interp.Interpolate(X[n]);
     39 //
     40 template <typename T, class Algorithm>
     41 class InterpolatorBase {
     42  public:
     43   InterpolatorBase();
     44   ~InterpolatorBase();
     45 
     46   // Generic random-access interpolation with arbitrary spaced x-axis samples.
     47   // Below X[0], the interpolator returns Y[0]. Above X[data_length-1], it
     48   // returns Y[data_length-1].
     49   T Interpolate(T x);
     50 
     51   bool get_status() const {
     52     return status_;
     53   }
     54 
     55   // Initializes internal buffers.
     56   //  x_data: [(data_length)x1] x-axis coordinates (searching axis)
     57   //  y_data: [(data_length)x1] y-axis coordinates (interpolation axis)
     58   //  data_length: number of points
     59   // returns `true` if everything is ok, `false`, otherwise
     60   bool Initialize(const T *x_data, const T *y_data, int data_length);
     61 
     62   // Initializes internal buffers.
     63   //  x_data: x-axis coordinates (searching axis)
     64   //  y_data: y-axis coordinates (interpolating axis)
     65   // returns `true` if everything is ok, `false`, otherwise
     66   bool Initialize(const vector<T> &x_data, const vector<T> &y_data);
     67 
     68   // Initialization for regularly sampled sequences, where:
     69   //  x_data[i] = x_start_offset + i * x_sampling_interval
     70   bool Initialize(double x_start_offset,
     71                   double x_sampling_interval,
     72                   const vector<T> &y_data);
     73 
     74   // Initialization for regularly sampled sequences, where:
     75   //  x_data[i] = x_start_offset + i * x_sampling_interval
     76   bool Initialize(double x_start_offset,
     77                   double x_sampling_interval,
     78                   const T *y_data,
     79                   int data_length);
     80 
     81  protected:
     82   // Is set to false if something goes wrong, and to true if everything is ok.
     83   bool status_;
     84 
     85   // The start-index of the previously searched interval
     86   int cached_index_;
     87 
     88   // Data points
     89   const T *x_data_;  // Externally or internally owned, depending on own_x_data_
     90   const T *y_data_;  // Externally owned (always)
     91   int data_length_;
     92   // Index of the last element `data_length_ - 1` kept here for optimization
     93   int last_element_index_;
     94   bool own_x_data_;
     95   // For regularly-samples sequences, keep only the boundaries and the intervals
     96   T x_start_offset_;
     97   float x_inverse_sampling_interval_;
     98 
     99   // Algorithm state (internally owned)
    100   double *state_;
    101 
    102  private:
    103   LE_FX_DISALLOW_COPY_AND_ASSIGN(InterpolatorBase);
    104 };
    105 
    106 }  // namespace sigmod
    107 
    108 }  // namespace le_fx
    109 
    110 #include "dsp/core/interpolator_base-inl.h"
    111 
    112 #endif  // LE_FX_ENGINE_DSP_CORE_INTERPOLATOR_BASE_H_
    113