Skip to content

Commit

Permalink
refactor IIR filters; update XML comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ar1st0crat committed Sep 30, 2021
1 parent b603b12 commit 92e4133
Show file tree
Hide file tree
Showing 36 changed files with 1,040 additions and 554 deletions.
47 changes: 35 additions & 12 deletions NWaves/Filters/Bessel/BandPassFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,60 @@
namespace NWaves.Filters.Bessel
{
/// <summary>
/// Represents band-pass Bessel filter.
/// Represents bandpass Bessel filter.
/// </summary>
public class BandPassFilter : ZiFilter
{
/// <summary>
/// Gets low cutoff frequency.
/// </summary>
public double FrequencyLow { get; private set; }

/// <summary>
/// Gets high cutoff frequency.
/// </summary>
public double FrequencyHigh { get; private set; }

/// <summary>
/// Gets filter order.
/// </summary>
public int Order => (_a.Length - 1) / 2;

/// <summary>
/// Constructs <see cref="BandPassFilter"/> of given <paramref name="order"/>
/// with given cutoff frequencies <paramref name="f1"/> and <paramref name="f2"/>.
/// with given cutoff frequencies <paramref name="frequencyLow"/> and <paramref name="frequencyHigh"/>.
/// </summary>
/// <param name="f1">First cutoff frequency</param>
/// <param name="f2">Second cutoff frequency</param>
/// <param name="frequencyLow">Normalized low cutoff frequency in range [0..0.5]</param>
/// <param name="frequencyHigh">Normalized high cutoff frequency in range [0..0.5]</param>
/// <param name="order">Filter order</param>
public BandPassFilter(double f1, double f2, int order) : base(MakeTf(f1, f2, order))
public BandPassFilter(double frequencyLow, double frequencyHigh, int order) : base(MakeTf(frequencyLow, frequencyHigh, order))
{
FrequencyLow = frequencyLow;
FrequencyHigh = frequencyHigh;
}

/// <summary>
/// Generates transfer function.
/// </summary>
/// <param name="f1">First cutoff frequency</param>
/// <param name="f2">Second cutoff frequency</param>
/// <param name="frequencyLow">Normalized low cutoff frequency in range [0..0.5]</param>
/// <param name="frequencyHigh">Normalized high cutoff frequency in range [0..0.5]</param>
/// <param name="order">Filter order</param>
private static TransferFunction MakeTf(double f1, double f2, int order)
private static TransferFunction MakeTf(double frequencyLow, double frequencyHigh, int order)
{
return DesignFilter.IirBpTf(f1, f2, PrototypeBessel.Poles(order));
return DesignFilter.IirBpTf(frequencyLow, frequencyHigh, PrototypeBessel.Poles(order));
}

/// <summary>
/// Changes filter coefficients online (preserving the state of the filter).
/// </summary>
/// <param name="f1">First cutoff frequency</param>
/// <param name="f2">Second cutoff frequency</param>
public void Change(double f1, double f2) => Change(MakeTf(f1, f2, (_a.Length - 1) / 2));
/// <param name="frequencyLow">Normalized low cutoff frequency in range [0..0.5]</param>
/// <param name="frequencyHigh">Normalized high cutoff frequency in range [0..0.5]</param>
public void Change(double frequencyLow, double frequencyHigh)
{
FrequencyLow = frequencyLow;
FrequencyHigh = frequencyHigh;

Change(MakeTf(frequencyLow, frequencyHigh, (_a.Length - 1) / 2));
}
}
}
47 changes: 35 additions & 12 deletions NWaves/Filters/Bessel/BandStopFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,60 @@
namespace NWaves.Filters.Bessel
{
/// <summary>
/// Represents band-stop Bessel filter.
/// Represents bandstop Bessel filter.
/// </summary>
public class BandStopFilter : ZiFilter
{
/// <summary>
/// Gets low cutoff frequency.
/// </summary>
public double FrequencyLow { get; private set; }

/// <summary>
/// Gets high cutoff frequency.
/// </summary>
public double FrequencyHigh { get; private set; }

/// <summary>
/// Gets filter order.
/// </summary>
public int Order => (_a.Length - 1) / 2;

/// <summary>
/// Constructs <see cref="BandStopFilter"/> of given <paramref name="order"/>
/// with given cutoff frequencies <paramref name="f1"/> and <paramref name="f2"/>.
/// with given cutoff frequencies <paramref name="frequencyLow"/> and <paramref name="frequencyHigh"/>.
/// </summary>
/// <param name="f1">First cutoff frequency</param>
/// <param name="f2">Second cutoff frequency</param>
/// <param name="frequencyLow">Normalized low cutoff frequency in range [0..0.5]</param>
/// <param name="frequencyHigh">Normalized high cutoff frequency in range [0..0.5]</param>
/// <param name="order">Filter order</param>
public BandStopFilter(double f1, double f2, int order) : base(MakeTf(f1, f2, order))
public BandStopFilter(double frequencyLow, double frequencyHigh, int order) : base(MakeTf(frequencyLow, frequencyHigh, order))
{
FrequencyLow = frequencyLow;
FrequencyHigh = frequencyHigh;
}

/// <summary>
/// Generates transfer function.
/// </summary>
/// <param name="f1">First cutoff frequency</param>
/// <param name="f2">Second cutoff frequency</param>
/// <param name="frequencyLow">Normalized low cutoff frequency in range [0..0.5]</param>
/// <param name="frequencyHigh">Normalized high cutoff frequency in range [0..0.5]</param>
/// <param name="order">Filter order</param>
private static TransferFunction MakeTf(double f1, double f2, int order)
private static TransferFunction MakeTf(double frequencyLow, double frequencyHigh, int order)
{
return DesignFilter.IirBsTf(f1, f2, PrototypeBessel.Poles(order));
return DesignFilter.IirBsTf(frequencyLow, frequencyHigh, PrototypeBessel.Poles(order));
}

/// <summary>
/// Changes filter coefficients online (preserving the state of the filter).
/// </summary>
/// <param name="f1">First cutoff frequency</param>
/// <param name="f2">Second cutoff frequency</param>
public void Change(double f1, double f2) => Change(MakeTf(f1, f2, (_a.Length - 1) / 2));
/// <param name="frequencyLow">Normalized low cutoff frequency in range [0..0.5]</param>
/// <param name="frequencyHigh">Normalized high cutoff frequency in range [0..0.5]</param>
public void Change(double frequencyLow, double frequencyHigh)
{
FrequencyLow = frequencyLow;
FrequencyHigh = frequencyHigh;

Change(MakeTf(frequencyLow, frequencyHigh, (_a.Length - 1) / 2));
}
}
}
34 changes: 25 additions & 9 deletions NWaves/Filters/Bessel/HighPassFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,49 @@
namespace NWaves.Filters.Bessel
{
/// <summary>
/// Represents high-pass Bessel filter.
/// Represents highpass Bessel filter.
/// </summary>
public class HighPassFilter : ZiFilter
{
/// <summary>
/// Constructs <see cref="HighPassFilter"/> of given <paramref name="order"/> with given cutoff <paramref name="freq"/>.
/// Gets cutoff frequency.
/// </summary>
/// <param name="freq">Cutoff frequency</param>
public double Frequency { get; private set; }

/// <summary>
/// Gets filter order.
/// </summary>
public int Order => _a.Length - 1;

/// <summary>
/// Constructs <see cref="HighPassFilter"/> of given <paramref name="order"/> with given cutoff <paramref name="frequency"/>.
/// </summary>
/// <param name="frequency">Normalized cutoff frequency in range [0..0.5]</param>
/// <param name="order">Filter order</param>
public HighPassFilter(double freq, int order) : base(MakeTf(freq, order))
public HighPassFilter(double frequency, int order) : base(MakeTf(frequency, order))
{
Frequency = frequency;
}

/// <summary>
/// Generates transfer function.
/// </summary>
/// <param name="freq">Cutoff frequency</param>
/// <param name="frequency">Normalized cutoff frequency in range [0..0.5]</param>
/// <param name="order">Filter order</param>
private static TransferFunction MakeTf(double freq, int order)
private static TransferFunction MakeTf(double frequency, int order)
{
return DesignFilter.IirHpTf(freq, PrototypeBessel.Poles(order));
return DesignFilter.IirHpTf(frequency, PrototypeBessel.Poles(order));
}

/// <summary>
/// Changes filter coefficients online (preserving the state of the filter).
/// </summary>
/// <param name="freq">Cutoff frequency</param>
public void Change(double freq) => Change(MakeTf(freq, _a.Length - 1));
/// <param name="frequency">Normalized cutoff frequency in range [0..0.5]</param>
public void Change(double frequency)
{
Frequency = frequency;

Change(MakeTf(frequency, _a.Length - 1));
}
}
}
34 changes: 25 additions & 9 deletions NWaves/Filters/Bessel/LowPassFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,49 @@
namespace NWaves.Filters.Bessel
{
/// <summary>
/// Represents low-pass Bessel filter.
/// Represents lowpass Bessel filter.
/// </summary>
public class LowPassFilter : ZiFilter
{
/// <summary>
/// Constructs <see cref="LowPassFilter"/> of given <paramref name="order"/> with given cutoff <paramref name="freq"/>.
/// Gets cutoff frequency.
/// </summary>
/// <param name="freq">Cutoff frequency</param>
public double Frequency { get; private set; }

/// <summary>
/// Gets filter order.
/// </summary>
public int Order => _a.Length - 1;

/// <summary>
/// Constructs <see cref="LowPassFilter"/> of given <paramref name="order"/> with given cutoff <paramref name="frequency"/>.
/// </summary>
/// <param name="frequency">Normalized cutoff frequency in range [0..0.5]</param>
/// <param name="order">Filter order</param>
public LowPassFilter(double freq, int order) : base(MakeTf(freq, order))
public LowPassFilter(double frequency, int order) : base(MakeTf(frequency, order))
{
Frequency = frequency;
}

/// <summary>
/// Generates transfer function.
/// </summary>
/// <param name="freq">Cutoff frequency</param>
/// <param name="frequency">Normalized cutoff frequency in range [0..0.5]</param>
/// <param name="order">Filter order</param>
private static TransferFunction MakeTf(double freq, int order)
private static TransferFunction MakeTf(double frequency, int order)
{
return DesignFilter.IirLpTf(freq, PrototypeBessel.Poles(order));
return DesignFilter.IirLpTf(frequency, PrototypeBessel.Poles(order));
}

/// <summary>
/// Changes filter coefficients online (preserving the state of the filter).
/// </summary>
/// <param name="freq">Cutoff frequency</param>
public void Change(double freq) => Change(MakeTf(freq, _a.Length - 1));
/// <param name="frequency">Normalized cutoff frequency in range [0..0.5]</param>
public void Change(double frequency)
{
Frequency = frequency;

Change(MakeTf(frequency, _a.Length - 1));
}
}
}
24 changes: 12 additions & 12 deletions NWaves/Filters/BiQuad/AllPassFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace NWaves.Filters.BiQuad
{
/// <summary>
/// Represents BiQuad all-pass filter.
/// Represents BiQuad allpass filter.
/// </summary>
public class AllPassFilter : BiQuadFilter
{
/// <summary>
/// Gets center frequency.
/// </summary>
public double Freq { get; protected set; }
public double Frequency { get; protected set; }

/// <summary>
/// Gets Q factor.
Expand All @@ -20,27 +20,27 @@ public class AllPassFilter : BiQuadFilter
/// <summary>
/// Constructs <see cref="AllPassFilter"/>.
/// </summary>
/// <param name="freq">Center frequency</param>
/// <param name="frequency">Normalized center frequency in range [0..0.5]</param>
/// <param name="q">Q factor</param>
public AllPassFilter(double freq, double q = 1)
public AllPassFilter(double frequency, double q = 1)
{
SetCoefficients(freq, q);
SetCoefficients(frequency, q);
}

/// <summary>
/// Sets filter coefficients.
/// </summary>
/// <param name="freq">Center frequency</param>
/// <param name="frequency">Normalized center frequency in range [0..0.5]</param>
/// <param name="q">Q factor</param>
private void SetCoefficients(double freq, double q)
private void SetCoefficients(double frequency, double q)
{
// The coefficients are calculated according to
// audio-eq-cookbook by R.Bristow-Johnson and WebAudio API.

Freq = freq;
Frequency = frequency;
Q = q;

var omega = 2 * Math.PI * freq;
var omega = 2 * Math.PI * frequency;
var alpha = Math.Sin(omega) / (2 * q);
var cosw = Math.Cos(omega);

Expand All @@ -58,11 +58,11 @@ private void SetCoefficients(double freq, double q)
/// <summary>
/// Changes filter coefficients online (preserving the state of the filter).
/// </summary>
/// <param name="freq">Center frequency</param>
/// <param name="frequency">Normalized center frequency in range [0..0.5]</param>
/// <param name="q">Q factor</param>
public void Change(double freq, double q = 1)
public void Change(double frequency, double q = 1)
{
SetCoefficients(freq, q);
SetCoefficients(frequency, q);
}
}
}
24 changes: 12 additions & 12 deletions NWaves/Filters/BiQuad/BandPassFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace NWaves.Filters.BiQuad
{
/// <summary>
/// Represents BiQuad band-pass filter.
/// Represents BiQuad bandpass filter.
/// </summary>
public class BandPassFilter : BiQuadFilter
{
/// <summary>
/// Gets center frequency.
/// </summary>
public double Freq { get; protected set; }
public double Frequency { get; protected set; }

/// <summary>
/// Gets Q factor.
Expand All @@ -20,27 +20,27 @@ public class BandPassFilter : BiQuadFilter
/// <summary>
/// Constructs <see cref="BandPassFilter"/>.
/// </summary>
/// <param name="freq">Center frequency</param>
/// <param name="frequency">Normalized center frequency in range [0..0.5]</param>
/// <param name="q">Q factor</param>
public BandPassFilter(double freq, double q = 1)
public BandPassFilter(double frequency, double q = 1)
{
SetCoefficients(freq, q);
SetCoefficients(frequency, q);
}

/// <summary>
/// Sets filter coefficients.
/// </summary>
/// <param name="freq">Center frequency</param>
/// <param name="frequency">Normalized center frequency in range [0..0.5]</param>
/// <param name="q">Q factor</param>
private void SetCoefficients(double freq, double q)
private void SetCoefficients(double frequency, double q)
{
// The coefficients are calculated according to
// audio-eq-cookbook by R.Bristow-Johnson and WebAudio API.

Freq = freq;
Frequency = frequency;
Q = q;

var omega = 2 * Math.PI * freq;
var omega = 2 * Math.PI * frequency;
var alpha = Math.Sin(omega) / (2 * q);
var cosw = Math.Cos(omega);

Expand All @@ -58,11 +58,11 @@ private void SetCoefficients(double freq, double q)
/// <summary>
/// Changes filter coefficients online (preserving the state of the filter).
/// </summary>
/// <param name="freq">Center frequency</param>
/// <param name="frequency">Normalized center frequency in range [0..0.5]</param>
/// <param name="q">Q factor</param>
public void Change(double freq, double q = 1)
public void Change(double frequency, double q = 1)
{
SetCoefficients(freq, q);
SetCoefficients(frequency, q);
}
}
}
Loading

0 comments on commit 92e4133

Please sign in to comment.