-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Cavern.Utilities; | ||
|
||
namespace Cavern.QuickEQ.Equalization { | ||
public static partial class EQGenerator { | ||
/// <summary> | ||
/// Get the average gains of multiple equalizers. The averaging happens in linear space. | ||
/// </summary> | ||
/// <remarks>All <paramref name="sources"/> must have an equal number of bands at the same frequencies.</remarks> | ||
public static Equalizer Average(params Equalizer[] sources) { | ||
double mul = 1.0 / sources[0].Bands.Count; | ||
return Average(QMath.DbToGain, x => QMath.GainToDb(x * mul), sources); | ||
} | ||
|
||
/// <summary> | ||
/// Get the average gains of multiple equalizers. The averaging happens in linear space and an RMS value is taken. | ||
/// </summary> | ||
/// <remarks>All <paramref name="sources"/> must have an equal number of bands at the same frequencies.</remarks> | ||
public static Equalizer AverageRMS(params Equalizer[] sources) { | ||
double mul = 1.0 / sources.Length; | ||
return Average(RMSAddition, x => QMath.GainToDb(Math.Sqrt(x) * mul), sources); | ||
} | ||
|
||
/// <summary> | ||
/// Get the average gains of multiple equalizers, regardless of how many bands they have. The averaging happens in linear space. | ||
/// </summary> | ||
public static Equalizer AverageSafe(params Equalizer[] sources) { | ||
List<Band> bands = new List<Band>(); | ||
double div = 1.0 / sources.Length; | ||
for (int i = 0; i < sources.Length; i++) { | ||
IReadOnlyList<Band> source = sources[i].Bands; | ||
for (int j = 0, c = source.Count; j < c; j++) { | ||
double freq = source[j].Frequency, | ||
gain = 0; | ||
for (int other = 0; other < sources.Length; other++) { | ||
gain += Math.Pow(10, sources[other][freq] * .05f); | ||
} | ||
bands.Add(new Band(freq, 20 * Math.Log10(gain * div))); | ||
} | ||
} | ||
|
||
bands.Sort(); | ||
return new Equalizer(bands.Distinct().ToList(), true); | ||
} | ||
|
||
/// <summary> | ||
/// Get the average gains of multiple equalizers by a custom averaging function. | ||
/// </summary> | ||
/// <param name="addition">Transformation when a value is added to the average calculation</param> | ||
/// <param name="division">Transformation when the average from the accumulator is taken</param> | ||
/// <param name="sources">Curves to get the average of</param> | ||
/// <remarks>All <paramref name="sources"/> must have an equal number of bands at the same frequencies.</remarks> | ||
static Equalizer Average(Func<double, double> addition, Func<double, double> division, params Equalizer[] sources) { | ||
double[] bands = new double[sources[0].Bands.Count]; | ||
IReadOnlyList<Band> source = sources[0].Bands; | ||
for (int i = 0; i < bands.Length; i++) { | ||
bands[i] = addition(source[i].Gain); | ||
} | ||
for (int i = 1; i < sources.Length; i++) { | ||
source = sources[i].Bands; | ||
for (int j = 0; j < bands.Length; j++) { | ||
bands[j] += addition(source[j].Gain); | ||
} | ||
} | ||
|
||
List<Band> result = new List<Band>(bands.Length); | ||
for (int i = 0; i < bands.Length; i++) { | ||
result.Add(new Band(source[i].Frequency, division(bands[i]))); | ||
} | ||
return new Equalizer(result, true); | ||
} | ||
|
||
/// <summary> | ||
/// Helper function for <see cref="AverageRMS(Equalizer[])"/>, adds a decibel value to the accumulator as a squared voltage. | ||
/// </summary> | ||
static double RMSAddition(double x) { | ||
x = QMath.DbToGain(x); | ||
return x * x; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Tests/Test.Cavern.QuickEQ/Equalization/EQGenerator.Averaging_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Cavern.QuickEQ.Equalization; | ||
|
||
namespace Test.Cavern.QuickEQ.Equalization { | ||
/// <summary> | ||
/// Tests the <see cref="EQGenerator"/> class's averaging functions. | ||
/// </summary> | ||
[TestClass] | ||
public class EQGeneratorAveraging_Tests { | ||
/// <summary> | ||
/// Tests if <see cref="EQGenerator.AverageRMS(Equalizer[])"/> works as intended. | ||
/// </summary> | ||
[TestMethod, Timeout(1000)] | ||
public void AverageRMS() { | ||
Equalizer a = new Equalizer([new Band(20, 1)], true), | ||
b = new Equalizer([new Band(20, 10)], true), | ||
avg = EQGenerator.AverageRMS(a, b); | ||
Assert.AreEqual(4.494369506972679, avg.Bands[0].Gain); | ||
} | ||
} | ||
} |