Audacity 3.2.0
Functions | Variables
Envelope.cpp File Reference
#include "Envelope.h"
#include <math.h>
#include <wx/wxcrtvararg.h>
#include <wx/brush.h>
#include <wx/pen.h>
#include <wx/textfile.h>
#include <wx/log.h>
#include <wx/utils.h>
Include dependency graph for Envelope.cpp:

Go to the source code of this file.

Functions

static double InterpolatePoints (double y1, double y2, double factor, bool logarithmic)
 
static double IntegrateInterpolated (double y1, double y2, double time, bool logarithmic)
 
static double IntegrateInverseInterpolated (double y1, double y2, double time, bool logarithmic)
 
static double SolveIntegrateInverseInterpolated (double y1, double y2, double time, double area, bool logarithmic)
 
static void checkResult (int n, double a, double b)
 

Variables

static const double VALUE_TOLERANCE = 0.001
 

Function Documentation

◆ checkResult()

static void checkResult ( int  n,
double  a,
double  b 
)
static

Definition at line 1401 of file Envelope.cpp.

1402{
1403 if( (a-b > 0 ? a-b : b-a) > 0.0000001 )
1404 {
1405 wxPrintf( "Envelope: Result #%d is: %f, should be %f\n", n, a, b );
1406 //exit( -1 );
1407 }
1408}

Referenced by Envelope::testMe().

Here is the caller graph for this function:

◆ IntegrateInterpolated()

static double IntegrateInterpolated ( double  y1,
double  y2,
double  time,
bool  logarithmic 
)
static

Definition at line 1101 of file Envelope.cpp.

1102{
1103 // Calculates: integral(interpolate(y1, y2, x), x = 0 .. time)
1104 // Integrating logarithmic interpolated segments is surprisingly simple. You can check this formula here:
1105 // http://www.wolframalpha.com/input/?i=integrate+10%5E%28log10%28y1%29*%28T-x%29%2FT%2Blog10%28y2%29*x%2FT%29+from+0+to+T
1106 // Again, the base you use for interpolation is irrelevant, the formula below should always use the natural
1107 // logarithm (i.e. 'log' in C/C++). If the denominator is too small, it's better to use linear interpolation
1108 // because the rounding errors would otherwise get too large. The threshold value is 1.0e-5 because at that
1109 // point the rounding errors become larger than the difference between linear and logarithmic (I tested this in Octave).
1110 if(logarithmic)
1111 {
1112 double l = log(y1 / y2);
1113 if(fabs(l) < 1.0e-5) // fall back to linear interpolation
1114 return (y1 + y2) * 0.5 * time;
1115 return (y1 - y2) / l * time;
1116 }
1117 else
1118 {
1119 return (y1 + y2) * 0.5 * time;
1120 }
1121}

Referenced by Envelope::Integral().

Here is the caller graph for this function:

◆ IntegrateInverseInterpolated()

static double IntegrateInverseInterpolated ( double  y1,
double  y2,
double  time,
bool  logarithmic 
)
static

Definition at line 1122 of file Envelope.cpp.

1123{
1124 // Calculates: integral(1 / interpolate(y1, y2, x), x = 0 .. time)
1125 // This one is a bit harder. Linear:
1126 // http://www.wolframalpha.com/input/?i=integrate+1%2F%28y1*%28T-x%29%2FT%2By2*x%2FT%29+from+0+to+T
1127 // Logarithmic:
1128 // http://www.wolframalpha.com/input/?i=integrate+1%2F%2810%5E%28log10%28y1%29*%28T-x%29%2FT%2Blog10%28y2%29*x%2FT%29%29+from+0+to+T
1129 // Here both cases need a special case for y1 == y2. The threshold is 1.0e5 again, this is still the
1130 // best value in both cases.
1131 double l = log(y1 / y2);
1132 if(fabs(l) < 1.0e-5) // fall back to average
1133 return 2.0 / (y1 + y2) * time;
1134 if(logarithmic)
1135 return (y1 - y2) / (l * y1 * y2) * time;
1136 else
1137 return l / (y1 - y2) * time;
1138}

Referenced by Envelope::IntegralOfInverse(), and Envelope::SolveIntegralOfInverse().

Here is the caller graph for this function:

◆ InterpolatePoints()

static double InterpolatePoints ( double  y1,
double  y2,
double  factor,
bool  logarithmic 
)
static

Definition at line 1093 of file Envelope.cpp.

1094{
1095 if(logarithmic)
1096 // you can use any base you want, it doesn't change the result
1097 return exp(log(y1) * (1.0 - factor) + log(y2) * factor);
1098 else
1099 return y1 * (1.0 - factor) + y2 * factor;
1100}

Referenced by Envelope::Integral(), Envelope::IntegralOfInverse(), and Envelope::SolveIntegralOfInverse().

Here is the caller graph for this function:

◆ SolveIntegrateInverseInterpolated()

static double SolveIntegrateInverseInterpolated ( double  y1,
double  y2,
double  time,
double  area,
bool  logarithmic 
)
static

Definition at line 1139 of file Envelope.cpp.

1140{
1141 // Calculates: solve (integral(1 / interpolate(y1, y2, x), x = 0 .. res) = area) for res
1142 // Don't try to derive these formulas by hand :). The threshold is 1.0e5 again.
1143 double a = area / time, res;
1144 if(logarithmic)
1145 {
1146 double l = log(y1 / y2);
1147 if(fabs(l) < 1.0e-5) // fall back to average
1148 res = a * (y1 + y2) * 0.5;
1149 else if(1.0 + a * y1 * l <= 0.0)
1150 res = 1.0;
1151 else
1152 res = log1p(a * y1 * l) / l;
1153 }
1154 else
1155 {
1156 if(fabs(y2 - y1) < 1.0e-5) // fall back to average
1157 res = a * (y1 + y2) * 0.5;
1158 else
1159 res = y1 * expm1(a * (y2 - y1)) / (y2 - y1);
1160 }
1161 return std::max(0.0, std::min(1.0, res)) * time;
1162}
int min(int a, int b)

References min().

Referenced by Envelope::SolveIntegralOfInverse().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ VALUE_TOLERANCE

const double VALUE_TOLERANCE = 0.001
static

Definition at line 41 of file Envelope.cpp.

Referenced by Envelope::RemoveUnneededPoints().