-- | Beq filter coefficient calculations.
module Sound.Sc3.Common.Math.Filter.Beq where

-- | Beq coefficients, (a0, a1, a2, b1, b2).
type Beq t = (t, t, t, t, t)

-- | Calculate coefficients for bi-quad low pass filter.
bLowPassCoef :: Floating t => t -> t -> t -> Beq t
bLowPassCoef :: forall t. Floating t => t -> t -> t -> Beq t
bLowPassCoef t
sr t
freq t
rq =
  let w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      cos_w0 :: t
cos_w0 = t -> t
forall a. Floating a => a -> a
cos t
w0
      i :: t
i = t
1 t -> t -> t
forall a. Num a => a -> a -> a
- t
cos_w0
      alpha :: t
alpha = t -> t
forall a. Floating a => a -> a
sin t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t
rq
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
alpha)
      a0 :: t
a0 = t
i t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a1 :: t
a1 = t
i t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t
cos_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- t
alpha) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t
a1, t
a0, t
b1, t
b2)

-- | Calculate coefficients for bi-quad high pass filter.
bHiPassCoef :: Floating t => t -> t -> t -> Beq t
bHiPassCoef :: forall t. Floating t => t -> t -> t -> Beq t
bHiPassCoef t
sr t
freq t
rq =
  let w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      cos_w0 :: t
cos_w0 = t -> t
forall a. Floating a => a -> a
cos t
w0
      i :: t
i = t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
cos_w0
      alpha :: t
alpha = t -> t
forall a. Floating a => a -> a
sin t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t
rq
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
alpha)
      a0 :: t
a0 = t
i t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a1 :: t
a1 = t -> t
forall a. Num a => a -> a
negate t
i t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t
cos_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- t
alpha) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t
a1, t
a0, t
b1, t
b2)

-- | Calculate coefficients for bi-quad all pass filter.
bAllPassCoef :: Floating t => t -> t -> t -> Beq t
bAllPassCoef :: forall t. Floating t => t -> t -> t -> Beq t
bAllPassCoef t
sr t
freq t
rq =
  let w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      alpha :: t
alpha = t -> t
forall a. Floating a => a -> a
sin t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t
rq
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
alpha)
      a0 :: t
a0 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- t
alpha) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
cos t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
  in (t
a0, t -> t
forall a. Num a => a -> a
negate t
b1, t
1.0, t
b1, t -> t
forall a. Num a => a -> a
negate t
a0)

-- | Calculate coefficients for bi-quad band pass filter.
bBandPassCoef :: Floating t => t -> t -> t -> Beq t
bBandPassCoef :: forall t. Floating t => t -> t -> t -> Beq t
bBandPassCoef t
sr t
freq t
bw =
  let w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      sin_w0 :: t
sin_w0 = t -> t
forall a. Floating a => a -> a
sin t
w0
      alpha :: t
alpha = t
sin_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sinh (t
0.34657359027997 t -> t -> t
forall a. Num a => a -> a -> a
* t
bw t -> t -> t
forall a. Num a => a -> a -> a
* t
w0 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sin_w0)
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
alpha)
      a0 :: t
a0 = t
alpha t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t -> t
forall a. Floating a => a -> a
cos t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- t
alpha) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t
0.0, t -> t
forall a. Num a => a -> a
negate t
a0, t
b1, t
b2)

-- | Calculate coefficients for bi-quad stop band filter.
bBandStopCoef :: Floating t => t -> t -> t -> Beq t
bBandStopCoef :: forall t. Floating t => t -> t -> t -> Beq t
bBandStopCoef t
sr t
freq t
bw =
  let w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      sin_w0 :: t
sin_w0 = t -> t
forall a. Floating a => a -> a
sin t
w0
      alpha :: t
alpha = t
sin_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sinh (t
0.34657359027997 t -> t -> t
forall a. Num a => a -> a -> a
* t
bw t -> t -> t
forall a. Num a => a -> a -> a
* t
w0 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sin_w0)
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ t
alpha)
      b1 :: t
b1 = t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
cos t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- t
alpha) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
b0rz, t -> t
forall a. Num a => a -> a
negate t
b1, t
b0rz, t
b1, t
b2)

-- | Calculate coefficients for bi-quad peaking Eq filter.
bPeakEqCoef :: Floating t => t -> t -> t -> t -> Beq t
bPeakEqCoef :: forall t. Floating t => t -> t -> t -> t -> Beq t
bPeakEqCoef t
sr t
freq t
rq t
db =
  let a :: t
a = t
10 t -> t -> t
forall a. Floating a => a -> a -> a
** (t
db t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
40)
      w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      alpha :: t
alpha = t -> t
forall a. Floating a => a -> a
sin t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t
rq
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ (t
alpha t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
a))
      a0 :: t
a0 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
+ (t
alpha t -> t -> t
forall a. Num a => a -> a -> a
* t
a)) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a2 :: t
a2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- (t
alpha t -> t -> t
forall a. Num a => a -> a -> a
* t
a)) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
cos t
w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = (t
1 t -> t -> t
forall a. Num a => a -> a -> a
- (t
alpha t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
a)) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t -> t
forall a. Num a => a -> a
negate t
b1, t
a2, t
b1, t
b2)

-- | Calculate coefficients for bi-quad low shelf filter.
bLowShelfCoef :: Floating t => t -> t -> t -> t -> Beq t
bLowShelfCoef :: forall t. Floating t => t -> t -> t -> t -> Beq t
bLowShelfCoef t
sr t
freq t
rs t
db =
  let a :: t
a = t
10 t -> t -> t
forall a. Floating a => a -> a -> a
** (t
db t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
40)
      w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      cos_w0 :: t
cos_w0 = t -> t
forall a. Floating a => a -> a
cos t
w0
      sin_w0 :: t
sin_w0 = t -> t
forall a. Floating a => a -> a
sin t
w0
      alpha :: t
alpha = t
sin_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sqrt ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t -> t
forall a. Fractional a => a -> a
recip t
a) t -> t -> t
forall a. Num a => a -> a -> a
* (t
rs t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
2.0)
      i :: t
i = (t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
* t
cos_w0
      j :: t
j = (t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
* t
cos_w0
      k :: t
k = t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sqrt t
a t -> t -> t
forall a. Num a => a -> a -> a
* t
alpha
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
j t -> t -> t
forall a. Num a => a -> a -> a
+ t
k)
      a0 :: t
a0 = t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
j t -> t -> t
forall a. Num a => a -> a -> a
+ t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a1 :: t
a1 = t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
i) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a2 :: t
a2 = t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
j t -> t -> t
forall a. Num a => a -> a -> a
- t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
i) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
j t -> t -> t
forall a. Num a => a -> a -> a
- t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t
a1, t
a2, t
b1, t
b2)

-- | Calculate coefficients for bi-quad high shelf filter.
bHiShelfCoef :: Floating t => t -> t -> t -> t -> Beq t
bHiShelfCoef :: forall t. Floating t => t -> t -> t -> t -> Beq t
bHiShelfCoef t
sr t
freq t
rs t
db =
  let a :: t
a = t
10 t -> t -> t
forall a. Floating a => a -> a -> a
** (t
db t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
40)
      w0 :: t
w0 = t
forall a. Floating a => a
pi t -> t -> t
forall a. Num a => a -> a -> a
* t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t
freq t -> t -> t
forall a. Num a => a -> a -> a
* (t
1 t -> t -> t
forall a. Fractional a => a -> a -> a
/ t
sr)
      cos_w0 :: t
cos_w0 = t -> t
forall a. Floating a => a -> a
cos t
w0
      sin_w0 :: t
sin_w0 = t -> t
forall a. Floating a => a -> a
sin t
w0
      alpha :: t
alpha = t
sin_w0 t -> t -> t
forall a. Num a => a -> a -> a
* t
0.5 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sqrt ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t -> t
forall a. Fractional a => a -> a
recip t
a) t -> t -> t
forall a. Num a => a -> a -> a
* (t
rs t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
2.0)
      i :: t
i = (t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
* t
cos_w0
      j :: t
j = (t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
* t
cos_w0
      k :: t
k = t
2 t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Floating a => a -> a
sqrt t
a t -> t -> t
forall a. Num a => a -> a -> a
* t
alpha
      b0rz :: t
b0rz = t -> t
forall a. Fractional a => a -> a
recip ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
j t -> t -> t
forall a. Num a => a -> a -> a
+ t
k)
      a0 :: t
a0 = t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
j t -> t -> t
forall a. Num a => a -> a -> a
+ t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a1 :: t
a1 = -t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
i) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      a2 :: t
a2 = t
a t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
+ t
j t -> t -> t
forall a. Num a => a -> a -> a
- t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b1 :: t
b1 = -t
2.0 t -> t -> t
forall a. Num a => a -> a -> a
* ((t
a t -> t -> t
forall a. Num a => a -> a -> a
- t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
i) t -> t -> t
forall a. Num a => a -> a -> a
* t
b0rz
      b2 :: t
b2 = ((t
a t -> t -> t
forall a. Num a => a -> a -> a
+ t
1) t -> t -> t
forall a. Num a => a -> a -> a
- t
j t -> t -> t
forall a. Num a => a -> a -> a
- t
k) t -> t -> t
forall a. Num a => a -> a -> a
* t -> t
forall a. Num a => a -> a
negate t
b0rz
  in (t
a0, t
a1, t
a2, t
b1, t
b2)