biquad 0.3__tar.gz → 0.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: biquad
3
- Version: 0.3
3
+ Version: 0.4
4
4
  Summary: Collection of alterable digital biquad filters for dynamic audio effect creation
5
5
  Home-page: https://github.com/jurihock/biquad
6
6
  Author: Juergen Hock
@@ -24,6 +24,7 @@ Classifier: Topic :: Scientific/Engineering
24
24
  Classifier: Topic :: Software Development :: Libraries
25
25
  Requires-Python: >=3
26
26
  Description-Content-Type: text/markdown
27
+ Provides-Extra: plot
27
28
  License-File: LICENSE
28
29
 
29
30
  # Alterable biquad filters
@@ -32,7 +33,7 @@ License-File: LICENSE
32
33
  ![license](https://img.shields.io/github/license/jurihock/biquad?color=green)
33
34
  ![pypi](https://img.shields.io/pypi/v/biquad?color=gold)
34
35
 
35
- This is a collection of digital biquad filters whose parameters `frequency` and `quality` can be varied at runtime. Following filter implementations are available:
36
+ This is a collection of [digital biquad filters](https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html) whose parameters `f` (frequency in *Hz*), `g` (gain in *dB*) and `q` (quality) can be varied at runtime. Following [DF1](https://ccrma.stanford.edu/~jos/fp/Direct_Form_I.html) filter implementations are available:
36
37
 
37
38
  - Allpass
38
39
  - Bandpass
@@ -45,7 +46,7 @@ This is a collection of digital biquad filters whose parameters `frequency` and
45
46
 
46
47
  ## Basic usage
47
48
 
48
- Filter with persistent configuration:
49
+ Filter with static configuration:
49
50
 
50
51
  ```python
51
52
  import biquad
@@ -81,6 +82,18 @@ myq = np.linspace(2, 1/2, len(x))
81
82
  y = f(x, f=myf, q=myq)
82
83
  ```
83
84
 
84
- ## See also
85
+ Keep in mind:
85
86
 
86
- [Cookbook formulae for audio equalizer biquad filter coefficients](https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html)
87
+ - All filters have a default value for the persistent parameters `g` and `q`, which is set in the particular `__init__` method.
88
+ - Parameter `f` must be set either in the `__init__` or in the `__call__` method.
89
+ - The optional instantaneous parameters `f`, `g` and `q`, if specified in the `__call__` method, override the persistent ones.
90
+
91
+ ## References
92
+
93
+ 1. <span id="1">[Cookbook formulae for audio EQ biquad filter coefficients](https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html) by Robert Bristow-Johnson</span>
94
+ 2. <span id="2">[Introduction to Digital Filters with Audio Applications](https://ccrma.stanford.edu/~jos/filters/filters.html) by Julius O. Smith III</span>
95
+
96
+ ## License
97
+
98
+ [github.com/jurihock/biquad](https://github.com/jurihock/biquad) is licensed under the terms of the MIT license.
99
+ For details please refer to the accompanying [LICENSE](https://github.com/jurihock/biquad/raw/main/LICENSE) file distributed with it.
biquad-0.4/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Alterable biquad filters
2
+
3
+ ![language](https://img.shields.io/badge/languages-C%2B%2B%20Python-blue)
4
+ ![license](https://img.shields.io/github/license/jurihock/biquad?color=green)
5
+ ![pypi](https://img.shields.io/pypi/v/biquad?color=gold)
6
+
7
+ This is a collection of [digital biquad filters](https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html) whose parameters `f` (frequency in *Hz*), `g` (gain in *dB*) and `q` (quality) can be varied at runtime. Following [DF1](https://ccrma.stanford.edu/~jos/fp/Direct_Form_I.html) filter implementations are available:
8
+
9
+ - Allpass
10
+ - Bandpass
11
+ - Highpass
12
+ - Lowpass
13
+ - Highshelf
14
+ - Lowshelf
15
+ - Notch
16
+ - Peak
17
+
18
+ ## Basic usage
19
+
20
+ Filter with static configuration:
21
+
22
+ ```python
23
+ import biquad
24
+ import numpy as np
25
+
26
+ # load audio samples somehow
27
+ x, sr = np.zeros(...), 44100
28
+
29
+ # create a filter of your choice
30
+ f = biquad.bandpass(sr, f=sr/4, q=1)
31
+
32
+ # process all audio samples
33
+ y = f(x)
34
+ ```
35
+
36
+ Filter with dynamic configuration:
37
+
38
+ ```python
39
+ import biquad
40
+ import numpy as np
41
+
42
+ # load audio samples somehow
43
+ x, sr = np.zeros(...), 44100
44
+
45
+ # create a filter of your choice
46
+ f = biquad.bandpass(sr)
47
+
48
+ # create parameter modifications as you like
49
+ myf = np.linspace(1, sr/4, len(x))
50
+ myq = np.linspace(2, 1/2, len(x))
51
+
52
+ # process all audio samples
53
+ y = f(x, f=myf, q=myq)
54
+ ```
55
+
56
+ Keep in mind:
57
+
58
+ - All filters have a default value for the persistent parameters `g` and `q`, which is set in the particular `__init__` method.
59
+ - Parameter `f` must be set either in the `__init__` or in the `__call__` method.
60
+ - The optional instantaneous parameters `f`, `g` and `q`, if specified in the `__call__` method, override the persistent ones.
61
+
62
+ ## References
63
+
64
+ 1. <span id="1">[Cookbook formulae for audio EQ biquad filter coefficients](https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html) by Robert Bristow-Johnson</span>
65
+ 2. <span id="2">[Introduction to Digital Filters with Audio Applications](https://ccrma.stanford.edu/~jos/filters/filters.html) by Julius O. Smith III</span>
66
+
67
+ ## License
68
+
69
+ [github.com/jurihock/biquad](https://github.com/jurihock/biquad) is licensed under the terms of the MIT license.
70
+ For details please refer to the accompanying [LICENSE](https://github.com/jurihock/biquad/raw/main/LICENSE) file distributed with it.
@@ -58,6 +58,10 @@ install_requires =
58
58
  numba
59
59
  numpy
60
60
 
61
+ [options.extras_require]
62
+ plot =
63
+ matplotlib
64
+
61
65
  [egg_info]
62
66
  tag_build =
63
67
  tag_date = 0
@@ -1,4 +1,4 @@
1
- __version__ = "0.3"
1
+ __version__ = "0.4"
2
2
 
3
3
 
4
4
  from .allpass import allpass
@@ -6,7 +6,7 @@ SPDX-License-Identifier: MIT
6
6
  Source: https://github.com/jurihock/biquad
7
7
  """
8
8
 
9
- from .biquad import biquad, __df1__
9
+ from .biquad import biquad, __df1__, __gain__, __resize__
10
10
 
11
11
  import numba
12
12
  import numpy
@@ -17,7 +17,7 @@ class allpass(biquad):
17
17
  Allpass filter (APF).
18
18
  """
19
19
 
20
- def __init__(self, sr, *, f=None, q=1):
20
+ def __init__(self, sr, f=None, g=0, q=1):
21
21
  """
22
22
  Create a new filter instance.
23
23
 
@@ -27,15 +27,17 @@ class allpass(biquad):
27
27
  Sample rate in hertz.
28
28
  f : int or float, optional
29
29
  Persistent filter frequency parameter in hertz.
30
+ g : int or float, optional
31
+ Persistent filter gain parameter in decibel.
30
32
  q : int or float, optional
31
33
  Persistent filter quality parameter.
32
34
  """
33
35
 
34
- super().__init__(sr=sr, f=f, q=q)
36
+ super().__init__(sr=sr, f=f, g=g, q=q)
35
37
 
36
- self.__call__(0, 1) # warmup numba
38
+ self.__call__(0) # warmup numba
37
39
 
38
- def __call__(self, x, f=None, q=None):
40
+ def __call__(self, x, f=None, g=None, q=None):
39
41
  """
40
42
  Process single or multiple contiguous signal values at once.
41
43
 
@@ -45,6 +47,8 @@ class allpass(biquad):
45
47
  Filter input data.
46
48
  f : scalar or array like, optional
47
49
  Instantaneous filter frequency parameter in hertz.
50
+ g : scalar or array like, optional
51
+ Instantaneous filter gain parameter in decibel.
48
52
  q : scalar or array like, optional
49
53
  Instantaneous filter quality parameter.
50
54
 
@@ -62,21 +66,23 @@ class allpass(biquad):
62
66
  x = numpy.atleast_1d(x)
63
67
  y = numpy.zeros(x.shape, x.dtype)
64
68
 
65
- f = numpy.atleast_1d(self.f if f is None else f)
66
- q = numpy.atleast_1d(self.q if q is None else q)
67
-
68
- f = numpy.resize(f, x.shape)
69
- q = numpy.resize(q, x.shape)
69
+ f = __resize__(self.f if f is None else f, x.shape)
70
+ g = __resize__(self.g if g is None else __gain__(g), x.shape)
71
+ q = __resize__(self.q if q is None else q, x.shape)
70
72
 
71
73
  sr = self.sr
72
74
 
73
- self.__filter__(ba, xy, x, y, f, q, sr)
75
+ self.__filter__(ba, xy, x, y, f, g, q, sr)
76
+
77
+ self.f = f[-1]
78
+ self.g = g[-1]
79
+ self.q = q[-1]
74
80
 
75
81
  return y[0] if scalar else y
76
82
 
77
83
  @staticmethod
78
84
  @numba.jit(nopython=True, fastmath=True)
79
- def __filter__(ba, xy, x, y, f, q, sr):
85
+ def __filter__(ba, xy, x, y, f, g, q, sr):
80
86
 
81
87
  rs = 2 * numpy.pi / sr
82
88
 
@@ -87,18 +93,18 @@ class allpass(biquad):
87
93
  cosw = numpy.cos(w)
88
94
  sinw = numpy.sin(w)
89
95
 
90
- c = -(2 * cosw)
91
- p = sinw / (2 * q[i])
96
+ alpha = sinw / (+2 * q[i])
97
+ beta = cosw * (-2)
92
98
 
93
99
  # update b
94
- ba[0, 0] = 1 - p
95
- ba[0, 1] = c
96
- ba[0, 2] = 1 + p
100
+ ba[0, 0] = 1 - alpha
101
+ ba[0, 1] = beta
102
+ ba[0, 2] = 1 + alpha
97
103
 
98
104
  # update a
99
- ba[1, 0] = 1 + p
100
- ba[1, 1] = c
101
- ba[1, 2] = 1 - p
105
+ ba[1, 0] = 1 + alpha
106
+ ba[1, 1] = beta
107
+ ba[1, 2] = 1 - alpha
102
108
 
103
109
  # update y
104
- __df1__(ba, xy, x, y, i)
110
+ __df1__(g[i], ba, xy, x, y, i)
@@ -6,7 +6,7 @@ SPDX-License-Identifier: MIT
6
6
  Source: https://github.com/jurihock/biquad
7
7
  """
8
8
 
9
- from .biquad import biquad, __df1__
9
+ from .biquad import biquad, __df1__, __gain__, __resize__
10
10
 
11
11
  import numba
12
12
  import numpy
@@ -17,7 +17,7 @@ class bandpass(biquad):
17
17
  Bandpass filter (BPF).
18
18
  """
19
19
 
20
- def __init__(self, sr, gain='skirt', *, f=None, q=0.7071):
20
+ def __init__(self, sr, f=None, g=0, q=0.7071, *, mode='peak'):
21
21
  """
22
22
  Create a new filter instance.
23
23
 
@@ -25,23 +25,25 @@ class bandpass(biquad):
25
25
  ----------
26
26
  sr : int or float
27
27
  Sample rate in hertz.
28
- gain : str, skirt or peak, optional
29
- Choice between constant skirt gain or constant 0 dB peak gain.
30
28
  f : int or float, optional
31
29
  Persistent filter frequency parameter in hertz.
30
+ g : int or float, optional
31
+ Persistent filter gain parameter in decibel.
32
32
  q : int or float, optional
33
33
  Persistent filter quality parameter.
34
+ mode : str, peak or skirt, optional
35
+ Choice between constant 0 dB peak gain or constant skirt gain.
34
36
  """
35
37
 
36
- super().__init__(sr=sr, f=f, q=q)
38
+ super().__init__(sr=sr, f=f, g=g, q=q)
37
39
 
38
- assert gain in ['skirt', 'peak']
40
+ assert mode in ['peak', 'skirt']
39
41
 
40
- self.gain = gain
42
+ self.mode = mode
41
43
 
42
- self.__call__(0, 1) # warmup numba
44
+ self.__call__(0) # warmup numba
43
45
 
44
- def __call__(self, x, f=None, q=None):
46
+ def __call__(self, x, f=None, g=None, q=None):
45
47
  """
46
48
  Process single or multiple contiguous signal values at once.
47
49
 
@@ -51,6 +53,8 @@ class bandpass(biquad):
51
53
  Filter input data.
52
54
  f : scalar or array like, optional
53
55
  Instantaneous filter frequency parameter in hertz.
56
+ g : scalar or array like, optional
57
+ Instantaneous filter gain parameter in decibel.
54
58
  q : scalar or array like, optional
55
59
  Instantaneous filter quality parameter.
56
60
 
@@ -68,25 +72,27 @@ class bandpass(biquad):
68
72
  x = numpy.atleast_1d(x)
69
73
  y = numpy.zeros(x.shape, x.dtype)
70
74
 
71
- f = numpy.atleast_1d(self.f if f is None else f)
72
- q = numpy.atleast_1d(self.q if q is None else q)
73
-
74
- f = numpy.resize(f, x.shape)
75
- q = numpy.resize(q, x.shape)
75
+ f = __resize__(self.f if f is None else f, x.shape)
76
+ g = __resize__(self.g if g is None else __gain__(g), x.shape)
77
+ q = __resize__(self.q if q is None else q, x.shape)
76
78
 
77
79
  sr = self.sr
78
- gain = self.gain
80
+ mode = self.mode
81
+
82
+ self.__filter__(ba, xy, x, y, f, g, q, sr, mode)
79
83
 
80
- self.__filter__(ba, xy, x, y, f, q, sr, gain)
84
+ self.f = f[-1]
85
+ self.g = g[-1]
86
+ self.q = q[-1]
81
87
 
82
88
  return y[0] if scalar else y
83
89
 
84
90
  @staticmethod
85
91
  @numba.jit(nopython=True, fastmath=True)
86
- def __filter__(ba, xy, x, y, f, q, sr, gain):
92
+ def __filter__(ba, xy, x, y, f, g, q, sr, mode):
87
93
 
88
94
  rs = 2 * numpy.pi / sr
89
- skirt = gain == 'skirt'
95
+ skirt = mode == 'skirt'
90
96
 
91
97
  for i in range(x.size):
92
98
 
@@ -95,19 +101,19 @@ class bandpass(biquad):
95
101
  cosw = numpy.cos(w)
96
102
  sinw = numpy.sin(w)
97
103
 
98
- c = -(2 * cosw)
99
- p = sinw / (2 * q[i])
100
- g = sinw / 2 if skirt else p
104
+ alpha = sinw / (+2 * q[i])
105
+ beta = cosw * (-2)
106
+ gamma = sinw / (+2) if skirt else alpha
101
107
 
102
108
  # update b
103
- ba[0, 0] = +g
109
+ ba[0, 0] = +gamma
104
110
  ba[0, 1] = 0
105
- ba[0, 2] = -g
111
+ ba[0, 2] = -gamma
106
112
 
107
113
  # update a
108
- ba[1, 0] = 1 + p
109
- ba[1, 1] = c
110
- ba[1, 2] = 1 - p
114
+ ba[1, 0] = 1 + alpha
115
+ ba[1, 1] = beta
116
+ ba[1, 2] = 1 - alpha
111
117
 
112
118
  # update y
113
- __df1__(ba, xy, x, y, i)
119
+ __df1__(g[i], ba, xy, x, y, i)