Skip to content

Commit

Permalink
convert biquad to Direct Form II and make allpass inherit from biquad.
Browse files Browse the repository at this point in the history
…closes #2
  • Loading branch information
abaga129 committed Jan 25, 2020
1 parent cdcb056 commit e6f08f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 72 deletions.
75 changes: 15 additions & 60 deletions filter/ddsp/filter/allpass.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,80 +27,35 @@ module ddsp.filter.allpass;

import std.math;

import ddsp.effect.effect;

const float pi = 3.14159265;
import ddsp.filter.biquad;


/// Allpass filter for introducting a 180 degrees phase shift at the center
/// frequency. This is necessary for summing more than 2 bands created from
/// Linkwitz-Riley filters.
/// TODO: Inherit BiQuad directly to remove redundent code.
class Allpass(T) : AudioEffect!T
class Allpass(T) : BiQuad!T
{
public:

this() nothrow @nogc
{

}

void initialize(float frequency, float q = 0.707) nothrow @nogc
{
_frequency = frequency;
float _w0 = 2 * pi * _frequency / _sampleRate;
float cs = cos(_w0);
float sn = sin(_w0);
float AL = sn / (2 * q);

_a0 = 1 + AL;
_a1 = (-2 * cs) / _a0;
_a2 = (1 - AL) / _a0;
_b0 = (1 - AL) / _a0;
_b1 = (-2 * cs) / _a0;
_b2 = (1 + AL) / _a0;
super();
}

override T getNextSample(const T input) nothrow @nogc
override void calcCoefficients()
{
_w = input - _a1 * _w1 - _a2 * _w2;
_yn = _b0 * _w + _b1 *_w1 + _b2 * _w2;

_w2 = _w1;
_w1 = _w;

return _yn;
immutable float _w0 = 2 * PI * _frequency / _sampleRate;
immutable float cs = cos(_w0);
immutable float sn = sin(_w0);
immutable float AL = sn / (2 * 0.707f);

immutable float _b0 = 1 + AL;
_b1 = (-2 * cs) / _b0;
_b2 = (1 - AL) / _b0;
_a0 = (1 - AL) / _b0;
_a1 = (-2 * cs) / _b0;
_a2 = (1 + AL) / _b0;
}

override void reset() nothrow @nogc
{
_w = 0;
_w1 = 0;
_w2 = 0;

_yn = 0;
}

void setFrequency(float frequency) nothrow @nogc
{
initialize(frequency);
}

private:
float _a0 = 0;
float _a1 = 0;
float _a2 = 0;
float _b0 = 0;
float _b1 = 0;
float _b2 = 0;

float _w = 0;
float _w1 = 0;
float _w2 = 0;

float _yn;

float _frequency;
}

unittest
Expand Down
26 changes: 14 additions & 12 deletions filter/ddsp/filter/biquad.d
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,33 @@ class BiQuad(T) : AudioEffect!T

override T getNextSample(const T input) nothrow @nogc
{
T output = (_a0 * input + _a1 * _xn1 + _a2 * _xn2 - _b1 * _yn1 - _b2 * _yn2) * _c0 + (input * _d0);
_w = input - _b1 * _w1 - _b2 * _w2;
_yn = (_a0 * _w + _a1 *_w1 + _a2 * _w2) * _c0 + (input * _d0);

_xn2 = _xn1;
_xn1 = input;
_yn2 = _yn1;
_yn1 = output;
_w2 = _w1;
_w1 = _w;

return output;
return _yn;
}

override void reset()
{
_xn1 = 0;
_xn2 = 0;
_yn1 = 0;
_yn2 = 0;
_w = 0;
_w1 = 0;
_w2 = 0;

_yn = 0;
}

abstract void calcCoefficients() nothrow @nogc;

protected:

//Delay samples
T _xn1=0, _xn2=0;
T _yn1=0, _yn2=0;
float _w = 0;
float _w1 = 0;
float _w2 = 0;
float _yn = 0;

//Biquad Coeffecients
T _a0=0, _a1=0, _a2=0;
Expand Down

0 comments on commit e6f08f1

Please sign in to comment.