Skip to content

Commit

Permalink
+ Added some tests for BiQuad Filter and Attenuator.
Browse files Browse the repository at this point in the history
* Fixed BiQuadFilter to correctly process the samples.
  • Loading branch information
MineCake147E committed Sep 2, 2019
1 parent 7aeb4fb commit 4be8ecd
Show file tree
Hide file tree
Showing 9 changed files with 537 additions and 167 deletions.
3 changes: 2 additions & 1 deletion MonoAudio.Core/Filters/BiQuadFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public int Read(Span<float> buffer)
var feedForward = v * Parameter.B; //Multiply in one go
var sum1 = v = feedForward.X + a.X;
var feedBack = sum1 * Parameter.A; //Multiply in one go
a = new Vector2(feedForward.Y + feedBack.X + a.Y, feedForward.Z + feedBack.Y);
var aY = a.Y; //Needed backup
a = new Vector2(feedForward.Y + feedBack.X + aY, feedForward.Z + feedBack.Y);
}
}
}
Expand Down
27 changes: 14 additions & 13 deletions MonoAudio.Core/Filters/BiQuadParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public readonly struct BiQuadParameter
/// <param name="a0">The a0.</param>
/// <param name="a1">The a1.</param>
/// <param name="a2">The a2.</param>
private BiQuadParameter(float b0, float b1, float b2, float a0, float a1, float a2) => (B, A) = (new Vector3(b0, b1, b2) / a0, new Vector2(a1, a2) / a0);
private BiQuadParameter(float b0, float b1, float b2, float a0, float a1, float a2) => (B, A) = (new Vector3(b0, b1, b2) / a0, new Vector2(a1, a2) / -a0); //Invert in advance

/// <summary>
/// The normalized B parameters.
Expand Down Expand Up @@ -103,7 +103,7 @@ public static BiQuadParameter CreateBPFParameterFromQuality(double samplingFrequ
/// </summary>
/// <param name="samplingFrequency">The sampling frequency.</param>
/// <param name="centerFrequency">The center frequency.</param>
/// <param name="bandWidth">The bandwidth.</param>
/// <param name="bandWidth">The bandwidth in Octaves.</param>
/// <param name="gainKind">Kind of the gain.</param>
/// <returns></returns>
/// <exception cref="ArgumentException">The kind of gain is invalid! - gainKind</exception>
Expand Down Expand Up @@ -163,7 +163,7 @@ public static BiQuadParameter CreatePeakingEqualizerParameterFromQuality(double
/// </summary>
/// <param name="samplingFrequency">The sampling frequency.</param>
/// <param name="centerFrequency">The center frequency.</param>
/// <param name="bandWidth">The bandwidth.</param>
/// <param name="bandWidth">The bandwidth in Octaves.</param>
/// <param name="dBGain">The peak gain in dB.</param>
/// <returns></returns>
public static BiQuadParameter CreatePeakingEqualizerParameterFromBandWidth(double samplingFrequency, double centerFrequency, double bandWidth, double dBGain)
Expand Down Expand Up @@ -198,7 +198,7 @@ public static BiQuadParameter CreateLowShelfFilterParameterFromQuality(double sa
/// <param name="slope">The slope in dB/Oct.</param>
/// <param name="dBGain">The peak gain in dB.</param>
/// <returns></returns>
public static BiQuadParameter CreateLowShelfFilterParameterFromBandWidth(double samplingFrequency, double cutOffFrequency, double slope, double dBGain)
public static BiQuadParameter CreateLowShelfFilterParameterFromSlope(double samplingFrequency, double cutOffFrequency, double slope, double dBGain)
{
var A = CalculateA(dBGain);
CalculateOmega0RelatedValues(samplingFrequency, cutOffFrequency, out _, out double cosW0, out double sinW0);
Expand Down Expand Up @@ -230,7 +230,7 @@ public static BiQuadParameter CreateHighShelfFilterParameterFromQuality(double s
/// <param name="slope">The slope in dB/Oct.</param>
/// <param name="dBGain">The peak gain in dB.</param>
/// <returns></returns>
public static BiQuadParameter CreateHighShelfFilterParameterFromBandWidth(double samplingFrequency, double cutOffFrequency, double slope, double dBGain)
public static BiQuadParameter CreateHighShelfFilterParameterFromSlope(double samplingFrequency, double cutOffFrequency, double slope, double dBGain)
{
var A = CalculateA(dBGain);
CalculateOmega0RelatedValues(samplingFrequency, cutOffFrequency, out _, out double cosW0, out double sinW0);
Expand Down Expand Up @@ -366,17 +366,18 @@ private static BiQuadParameter CreateLowShelfCoefficients(double cosW0, double A
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static BiQuadParameter CreateHighShelfCoefficients(double cosW0, double A, double alpha)
private static BiQuadParameter CreateHighShelfCoefficients(double cosW0, double a, double alpha)
{
var TwomSqrtAmAlpha = 2 * Math.Sqrt(A) * alpha;
var Ap1 = A + 1;
var As1 = A - 1;
var TwomSqrtAmAlpha = 2 * Math.Sqrt(a) * alpha;
var Ap1 = a + 1;
var As1 = a - 1;
double As1CosW0 = As1 * cosW0;
float b0 = (float)(A * (Ap1 + As1CosW0 + TwomSqrtAmAlpha));
float b1 = (float)(2 * A * (As1 + Ap1 * cosW0));
float b2 = (float)(A * (Ap1 + As1CosW0 - TwomSqrtAmAlpha));
double Ap1CosW0 = Ap1 * cosW0;
float b0 = (float)(a * (Ap1 + As1CosW0 + TwomSqrtAmAlpha));
float b1 = (float)(-2 * a * (As1 + Ap1CosW0));
float b2 = (float)(a * (Ap1 + As1CosW0 - TwomSqrtAmAlpha));
float a0 = (float)(Ap1 - As1CosW0 + TwomSqrtAmAlpha);
float a1 = (float)(-2 * (As1 - Ap1 * cosW0));
float a1 = (float)(2 * (As1 - Ap1CosW0));
float a2 = (float)(Ap1 - As1CosW0 - TwomSqrtAmAlpha);
return new BiQuadParameter(b0, b1, b2, a0, a1, a2);
}
Expand Down
151 changes: 125 additions & 26 deletions MonoAudio.Core/MonoAudio.Core.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4be8ecd

Please sign in to comment.