Audacity 3.2.0
Functions | Variables
Envelope.cpp File Reference
#include "Envelope.h"
#include <float.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 1417 of file Envelope.cpp.

1418{
1419 if( (a-b > 0 ? a-b : b-a) > 0.0000001 )
1420 {
1421 wxPrintf( "Envelope: Result #%d is: %f, should be %f\n", n, a, b );
1422 //exit( -1 );
1423 }
1424}

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 1117 of file Envelope.cpp.

1118{
1119 // Calculates: integral(interpolate(y1, y2, x), x = 0 .. time)
1120 // Integrating logarithmic interpolated segments is surprisingly simple. You can check this formula here:
1121 // 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
1122 // Again, the base you use for interpolation is irrelevant, the formula below should always use the natural
1123 // logarithm (i.e. 'log' in C/C++). If the denominator is too small, it's better to use linear interpolation
1124 // because the rounding errors would otherwise get too large. The threshold value is 1.0e-5 because at that
1125 // point the rounding errors become larger than the difference between linear and logarithmic (I tested this in Octave).
1126 if(logarithmic)
1127 {
1128 double l = log(y1 / y2);
1129 if(fabs(l) < 1.0e-5) // fall back to linear interpolation
1130 return (y1 + y2) * 0.5 * time;
1131 return (y1 - y2) / l * time;
1132 }
1133 else
1134 {
1135 return (y1 + y2) * 0.5 * time;
1136 }
1137}

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 1138 of file Envelope.cpp.

1139{
1140 // Calculates: integral(1 / interpolate(y1, y2, x), x = 0 .. time)
1141 // This one is a bit harder. Linear:
1142 // http://www.wolframalpha.com/input/?i=integrate+1%2F%28y1*%28T-x%29%2FT%2By2*x%2FT%29+from+0+to+T
1143 // Logarithmic:
1144 // 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
1145 // Here both cases need a special case for y1 == y2. The threshold is 1.0e5 again, this is still the
1146 // best value in both cases.
1147 double l = log(y1 / y2);
1148 if(fabs(l) < 1.0e-5) // fall back to average
1149 return 2.0 / (y1 + y2) * time;
1150 if(logarithmic)
1151 return (y1 - y2) / (l * y1 * y2) * time;
1152 else
1153 return l / (y1 - y2) * time;
1154}

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 1109 of file Envelope.cpp.

1110{
1111 if(logarithmic)
1112 // you can use any base you want, it doesn't change the result
1113 return exp(log(y1) * (1.0 - factor) + log(y2) * factor);
1114 else
1115 return y1 * (1.0 - factor) + y2 * factor;
1116}

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 1155 of file Envelope.cpp.

1156{
1157 // Calculates: solve (integral(1 / interpolate(y1, y2, x), x = 0 .. res) = area) for res
1158 // Don't try to derive these formulas by hand :). The threshold is 1.0e5 again.
1159 double a = area / time, res;
1160 if(logarithmic)
1161 {
1162 double l = log(y1 / y2);
1163 if(fabs(l) < 1.0e-5) // fall back to average
1164 res = a * (y1 + y2) * 0.5;
1165 else if(1.0 + a * y1 * l <= 0.0)
1166 res = 1.0;
1167 else
1168 res = log1p(a * y1 * l) / l;
1169 }
1170 else
1171 {
1172 if(fabs(y2 - y1) < 1.0e-5) // fall back to average
1173 res = a * (y1 + y2) * 0.5;
1174 else
1175 res = y1 * expm1(a * (y2 - y1)) / (y2 - y1);
1176 }
1177 return std::max(0.0, std::min(1.0, res)) * time;
1178}
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 42 of file Envelope.cpp.

Referenced by Envelope::RemoveUnneededPoints().