Added biquad filter code

This commit is contained in:
Anne de Jong 2021-04-20 22:25:43 +02:00
parent 2d33110138
commit 3a894879f0

43
lasp/filter/biquad.py Normal file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""!
Author: J.A. de Jong - ASCEE V.O.F.
Description: Filter design implementation of common biquad filters that are
often used in parametric equalizers.
"""
__all__ = ['peaking', 'biquadTF']
from scipy.signal import bilinear_zpk, zpk2sos, freqz_zpk, sosfreqz
from scipy.interpolate import interp1d
import numpy as np
def peaking(fs, f0, Q, gain):
"""
Design of peaking biquad filter
Args:
fs: Sampling frequency [Hz]
f0: Center frequency
Q: Quality factor (~ inverse of bandwidth)
gain: Increase in level at the center frequency
"""
A = np.sqrt(10**(gain/20))
omg0 = 2*np.pi*f0/fs
alpha = np.sin(omg0)/Q/2
b0 = 1+alpha*A
b1 = -2*np.cos(omg0)
b2 = 1-alpha*A
a0 = 1 + alpha/A
a1 = -2*np.cos(omg0)
a2 = 1-alpha/A
return np.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.
"""
freq2, h = sosfreqz(ba, worN=48000, fs=fs)
interpolator = interp1d(freq2, h, kind='quadratic')
return interpolator(freq)