• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 package com.google.sample.oboe.manualtest;
18 
19 /**
20  * Maps integer range info to a double value along an exponential scale.
21  *
22  * <pre>
23  *
24  *   x = ival / mResolution
25  *   f(x) = a*(root**bx)
26  *   f(0.0) = dmin
27  *   f(1.0) = dmax
28  *
29  *   f(0.0) = a * 1.0 => a = dmin
30  *   f(1.0) = dmin * root**b = dmax
31  *   b = log(dmax / dmin) / log(root)
32  *
33  * </pre>
34  */
35 
36 public class ExponentialTaper {
37     private double offset = 0.0;
38     private double a = 1.0;
39     private double b = 2.0;
40     private static final double ROOT = 10.0; // because we are using log10
41 
ExponentialTaper(double dmin, double dmax)42     public ExponentialTaper(double dmin, double dmax) {
43         this(dmin, dmax, 10000.0);
44     }
45 
ExponentialTaper(double dmin, double dmax, double maxRatio)46     public ExponentialTaper(double dmin, double dmax, double maxRatio) {
47         a = dmax;
48         double curvature;
49         if (dmax > dmin * maxRatio) {
50             offset = dmax / maxRatio;
51             a = offset;
52             curvature = (dmax + offset) / offset;
53         } else {
54             curvature = dmax / dmin;
55             a = dmin;
56         }
57         b = Math.log10(curvature);
58     }
59 
linearToExponential(double linear)60     public double linearToExponential(double linear) {
61         return a * Math.pow(ROOT,  b * linear) - offset;
62     }
63 
exponentialToLinear(double exponential)64     public double exponentialToLinear(double exponential) {
65         return Math.log((exponential + offset) / a) / (b * Math.log(ROOT));
66     }
67 }
68