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

1442{
1443 if( (a-b > 0 ? a-b : b-a) > 0.0000001 )
1444 {
1445 wxPrintf( "Envelope: Result #%d is: %f, should be %f\n", n, a, b );
1446 //exit( -1 );
1447 }
1448}

◆ IntegrateInterpolated()

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

Definition at line 1159 of file Envelope.cpp.

1160{
1161 // Calculates: integral(interpolate(y1, y2, x), x = 0 .. time)
1162 // Integrating logarithmic interpolated segments is surprisingly simple. You can check this formula here:
1163 // 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
1164 // Again, the base you use for interpolation is irrelevant, the formula below should always use the natural
1165 // logarithm (i.e. 'log' in C/C++). If the denominator is too small, it's better to use linear interpolation
1166 // because the rounding errors would otherwise get too large. The threshold value is 1.0e-5 because at that
1167 // point the rounding errors become larger than the difference between linear and logarithmic (I tested this in Octave).
1168 if(logarithmic)
1169 {
1170 double l = log(y1 / y2);
1171 if(fabs(l) < 1.0e-5) // fall back to linear interpolation
1172 return (y1 + y2) * 0.5 * time;
1173 return (y1 - y2) / l * time;
1174 }
1175 else
1176 {
1177 return (y1 + y2) * 0.5 * time;
1178 }
1179}

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

1181{
1182 // Calculates: integral(1 / interpolate(y1, y2, x), x = 0 .. time)
1183 // This one is a bit harder. Linear:
1184 // http://www.wolframalpha.com/input/?i=integrate+1%2F%28y1*%28T-x%29%2FT%2By2*x%2FT%29+from+0+to+T
1185 // Logarithmic:
1186 // 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
1187 // Here both cases need a special case for y1 == y2. The threshold is 1.0e5 again, this is still the
1188 // best value in both cases.
1189 double l = log(y1 / y2);
1190 if(fabs(l) < 1.0e-5) // fall back to average
1191 return 2.0 / (y1 + y2) * time;
1192 if(logarithmic)
1193 return (y1 - y2) / (l * y1 * y2) * time;
1194 else
1195 return l / (y1 - y2) * time;
1196}

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

1152{
1153 if(logarithmic)
1154 // you can use any base you want, it doesn't change the result
1155 return exp(log(y1) * (1.0 - factor) + log(y2) * factor);
1156 else
1157 return y1 * (1.0 - factor) + y2 * factor;
1158}

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

1198{
1199 // Calculates: solve (integral(1 / interpolate(y1, y2, x), x = 0 .. res) = area) for res
1200 // Don't try to derive these formulas by hand :). The threshold is 1.0e5 again.
1201 double a = area / time, res;
1202 if(logarithmic)
1203 {
1204 double l = log(y1 / y2);
1205 if(fabs(l) < 1.0e-5) // fall back to average
1206 res = a * (y1 + y2) * 0.5;
1207 else if(1.0 + a * y1 * l <= 0.0)
1208 res = 1.0;
1209 else
1210 res = log1p(a * y1 * l) / l;
1211 }
1212 else
1213 {
1214 if(fabs(y2 - y1) < 1.0e-5) // fall back to average
1215 res = a * (y1 + y2) * 0.5;
1216 else
1217 res = y1 * expm1(a * (y2 - y1)) / (y2 - y1);
1218 }
1219 return std::max(0.0, std::min(1.0, res)) * time;
1220}
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().