Some markup improvements

This commit is contained in:
Anne de Jong 2021-04-22 12:10:42 +02:00
parent 4e9f975aed
commit e268a55680

View File

@ -25,9 +25,10 @@ y[n] = 1/ba[3] * ( ba[0] * x[n] + ba[1] * x[n-1] + ba[2] * x[n-2] +
__all__ = ['peaking', 'biquadTF', 'notch', 'lowpass', 'highpass',
'highshelve', 'lowshelve']
from scipy.signal import sosfreqz
from numpy import array, cos, pi, sin, sqrt
from scipy.interpolate import interp1d
from numpy import sin, cos, sqrt, pi, array
from scipy.signal import sosfreqz
def peaking(fs, f0, Q, gain):
"""
@ -51,6 +52,7 @@ def peaking(fs, f0, Q, gain):
return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
def notch(fs, f0, Q):
"""
Notch filter
@ -62,14 +64,15 @@ def notch(fs, f0, Q):
"""
omg0 = 2*pi*f0/fs
alpha = sin(omg0)/Q/2
b0 = 1
b1 = -2*cos(omg0)
b2 = 1
a0 = 1 + alpha
a1 = -2*cos(omg0)
a2 = 1 - alpha
b0 = 1
b1 = -2*cos(omg0)
b2 = 1
a0 = 1 + alpha
a1 = -2*cos(omg0)
a2 = 1 - alpha
return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
def lowpass(fs, f0, Q):
"""
Second order low pass filter
@ -81,14 +84,15 @@ def lowpass(fs, f0, Q):
"""
w0 = 2*pi*f0/fs
alpha = sin(w0)/Q/2
b0 = (1 - cos(w0))/2
b1 = 1 - cos(w0)
b2 = (1 - cos(w0))/2
a0 = 1 + alpha
a1 = -2*cos(w0)
a2 = 1 - alpha
b0 = (1 - cos(w0))/2
b1 = 1 - cos(w0)
b2 = (1 - cos(w0))/2
a0 = 1 + alpha
a1 = -2*cos(w0)
a2 = 1 - alpha
return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
def highpass(fs, f0, Q):
"""
Second order high pass filter
@ -101,12 +105,12 @@ def highpass(fs, f0, Q):
w0 = 2*pi*f0/fs
alpha = sin(w0)/Q/2
b0 = (1 + cos(w0))/2
b0 = (1 + cos(w0))/2
b1 = -(1 + cos(w0))
b2 = (1 + cos(w0))/2
a0 = 1 + alpha
a1 = -2*cos(w0)
a2 = 1 - alpha
b2 = (1 + cos(w0))/2
a0 = 1 + alpha
a1 = -2*cos(w0)
a2 = 1 - alpha
return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
@ -123,14 +127,15 @@ def highshelve(fs, f0, Q, gain):
w0 = 2*pi*f0/fs
alpha = sin(w0)/Q/2
A = 10**(gain/40)
b0 = A*( (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha )
b1 = -2*A*( (A-1) + (A+1)*cos(w0) )
b2 = A*( (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha )
a0 = (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha
a1 = 2*( (A-1) - (A+1)*cos(w0) )
a2 = (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha
b0 = A*((A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha)
b1 = -2*A*((A-1) + (A+1)*cos(w0))
b2 = A*((A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha)
a0 = (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha
a1 = 2*((A-1) - (A+1)*cos(w0))
a2 = (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha
return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
def lowshelve(fs, f0, Q, gain):
"""
Low shelving filter
@ -144,14 +149,15 @@ def lowshelve(fs, f0, Q, gain):
w0 = 2*pi*f0/fs
alpha = sin(w0)/Q/2
A = 10**(gain/40)
b0 = A*( (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha )
b1 = 2*A*( (A-1) - (A+1)*cos(w0) )
b2 = A*( (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha )
a0 = (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha
a1 = -2*( (A-1) + (A+1)*cos(w0) )
a2 = (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha
b0 = A*((A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha)
b1 = 2*A*((A-1) - (A+1)*cos(w0))
b2 = A*((A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha)
a0 = (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha
a1 = -2*((A-1) + (A+1)*cos(w0))
a2 = (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha
return array([b0/a0, b1/a0, b2/a0, a0/a0, a1/a0, a2/a0])
def biquadTF(fs, freq, ba):
"""
Computes the transfer function of the biquad.
@ -166,7 +172,5 @@ def biquadTF(fs, freq, ba):
TODO: This code is not yet tested
"""
freq2, h = sosfreqz(ba, worN=freq, fs=fs)
interpolator = interp1d(freq2, h, kind='quadratic')
interpolator = interp1d(freq2, h, kind='quadratic')
return interpolator(freq)