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

1407{
1408 if( (a-b > 0 ? a-b : b-a) > 0.0000001 )
1409 {
1410 wxPrintf( "Envelope: Result #%d is: %f, should be %f\n", n, a, b );
1411 //exit( -1 );
1412 }
1413}

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

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

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

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

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

1099{
1100 if(logarithmic)
1101 // you can use any base you want, it doesn't change the result
1102 return exp(log(y1) * (1.0 - factor) + log(y2) * factor);
1103 else
1104 return y1 * (1.0 - factor) + y2 * factor;
1105}

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

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