Signal Processing¶
General-purpose DSP utilities: filtering, windowing, peak finding, dithering, Teager energy, zero-crossings, and resampling.
Filters and windows¶
v_windows
¶
V_WINDOWS - Generate a standard windowing function.
v_windows
¶
Generate a standard windowing function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wtype
|
str or int
|
Window type name or code. |
required |
n
|
int
|
Number of output points. Default 256. |
256
|
mode
|
str
|
Options string controlling scaling and sampling. |
None
|
p
|
float or array_like
|
Parameter(s) for parameterized windows. |
None
|
ov
|
int
|
Overlap for convolution with rectangle ('o' option). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
w |
ndarray
|
Window values (1D array of length n). |
Source code in pyvoicebox/v_windows.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
v_windinfo
¶
V_WINDINFO - Window information and figures of merit.
v_windinfo
¶
Calculate window information and figures of merit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
w
|
array_like
|
Window vector. |
required |
fs
|
float
|
Sampling frequency. Default 1. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
info |
dict
|
Dictionary with window properties: - nw: window length - ewgdelay: energy centroid delay from first sample - dcgain: DC gain in dB - enbw: equivalent noise bandwidth - scallop: scalloping loss in dB |
Source code in pyvoicebox/v_windinfo.py
v_filterbank
¶
V_FILTERBANK - Apply a bank of IIR filters to a signal.
v_filterbank
¶
Apply a bank of filters to a signal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
list of array_like or ndarray
|
Numerator coefficients. If 2D, each row is a filter. |
required |
a
|
list of array_like or ndarray
|
Denominator coefficients. If 2D, each row is a filter. |
required |
x
|
array_like
|
Input signal. |
required |
zi
|
list of array_like
|
Initial filter states. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
ndarray
|
Output signals, one column per filter. |
zf |
list of ndarray
|
Final filter states. |
Source code in pyvoicebox/v_filterbank.py
v_maxfilt
¶
V_MAXFILT - Find max of an exponentially weighted sliding window.
v_maxfilt
¶
Find max of an exponentially weighted sliding window.
Calculates y(p) = max(f^r * x(p-r), r=0:n-1) where x®=-inf for r<0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Input data (vector or matrix). |
required |
f
|
float
|
Exponential forgetting factor, range [0, 1]. Default 1 (no forgetting). |
1
|
n
|
int or None
|
Length of sliding window. Default is inf. |
None
|
d
|
int or None
|
Dimension to work along (0-based). Default is first non-singleton. |
None
|
x0
|
array_like or None
|
Initial values placed in front of x data. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
ndarray
|
Output array, same shape as x. |
k |
ndarray
|
Index array such that y = x[k] (0-based). |
y0 |
ndarray
|
Last n-1 values for subsequent calls (or last output if n=inf). |
Source code in pyvoicebox/v_maxfilt.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
v_momfilt
¶
V_MOMFILT - Calculate moments of a signal using a sliding window.
v_momfilt
¶
Calculate moments of a signal using a sliding window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Input signal. |
required |
r
|
array_like
|
List of moments to calculate (+ve relative to mean, -ve relative to zero). |
required |
w
|
int or array_like
|
Window or window length. Default: Hamming of length(x). |
None
|
m
|
int
|
Center sample of window (1-based). Default: ceil((1+len(w))/2). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
(ndarray, shape(len(x), len(r)))
|
Moments. |
mm |
int
|
Actual value of m used. |
Source code in pyvoicebox/v_momfilt.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
v_meansqtf
¶
V_MEANSQTF - Mean square transfer function of a filter.
v_meansqtf
¶
Calculate the mean square transfer function of a filter.
This equals the average output power when the filter is fed with unit variance white noise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
array_like
|
Numerator filter coefficients. |
required |
a
|
array_like
|
Denominator filter coefficients. Default is [1]. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
d |
float
|
Mean square transfer function. |
Source code in pyvoicebox/v_meansqtf.py
v_resample
¶
V_RESAMPLE - Resample and remove end transients.
v_resample
¶
Resample signal and remove end transients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Input signal. |
required |
p
|
int
|
Upsampling factor. |
required |
q
|
int
|
Downsampling factor. |
required |
n
|
int
|
Filter length. Default 10. |
10
|
b
|
float
|
Kaiser window beta. Default 5. |
5
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
ndarray
|
Resampled output signal. |
Source code in pyvoicebox/v_resample.py
v_dlyapsq
¶
V_DLYAPSQ - Solve discrete Lyapunov equation in square root form.
v_dlyapsq
¶
Solve the discrete Lyapunov equation AV'VA' - V'V + BB' = 0.
V is upper triangular with real non-negative diagonal entries. Equivalent to chol(dlyap(a, b@b')) but better conditioned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
(array_like, shape(n, n))
|
State matrix. |
required |
b
|
(array_like, shape(n, m))
|
Input matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
v |
(ndarray, shape(n, n))
|
Upper triangular solution. |
Source code in pyvoicebox/v_dlyapsq.py
Analysis primitives¶
v_findpeaks
¶
V_FINDPEAKS - Find peaks with optional quadratic interpolation.
v_findpeaks
¶
Find peaks with optional quadratic interpolation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
array_like
|
Input signal. |
required |
m
|
str
|
Mode string: 'f' - include first sample if downward initial slope 'l' - include last sample if upward final slope 'm' - return only the maximum peak 'q' - quadratic interpolation 'v' - find valleys instead of peaks |
''
|
w
|
float
|
Width tolerance. Peaks closer than w will have the lower one removed. |
None
|
x
|
array_like
|
X-axis values for y. Default: 0-based indices (1-based in MATLAB). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
k |
ndarray
|
Positions of peaks (1-based if x not given, fractional if 'q'). |
v |
ndarray
|
Peak amplitudes. |
Source code in pyvoicebox/v_findpeaks.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
v_zerocros
¶
V_ZEROCROS - Find zero crossings in a signal.
v_zerocros
¶
Find zero crossings in a signal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
array_like
|
Input waveform. |
required |
m
|
str
|
Mode string: 'p' - positive crossings only 'n' - negative crossings only 'b' - both (default) 'r' - round to sample values |
'b'
|
x
|
array_like
|
X-axis values for y. Default: 1-based indices (MATLAB convention). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
t |
ndarray
|
X-axis positions of zero crossings (1-based if x not given). |
s |
ndarray
|
Estimated slope of y at the zero crossings. |
Source code in pyvoicebox/v_zerocros.py
v_schmitt
¶
V_SCHMITT - Pass input signal through a Schmitt trigger.
v_schmitt
¶
Pass input signal through a Schmitt trigger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Input signal. |
required |
thresh
|
float or array_like
|
If scalar in [0,1]: hysteresis fraction. Thresholds are computed from max/min of x. If 2-element: [low, high] thresholds. Default is 0.5. |
0.5
|
minwid
|
int
|
Minimum pulse width in samples. Default is 0. |
0
|
return_transitions
|
bool
|
If True, return (y_transitions, t) where y_transitions contains alternating +1/-1 and t contains transition sample indices. If False (default), return full y signal. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
ndarray
|
If return_transitions=False: signal of same length as x with values -1, 0, or +1. If return_transitions=True: alternating +1/-1 transition values. |
t |
ndarray (only if return_transitions=True)
|
Sample indices where x crossed the thresholds (1-based). |
Source code in pyvoicebox/v_schmitt.py
v_teager
¶
V_TEAGER - Calculate Teager energy waveform.
v_teager
¶
Calculate Teager energy waveform.
y(n) = abs(x(n))^2 - x(n+1)*conj(x(n-1))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Input signal. |
required |
d
|
int or None
|
Dimension to apply filter along (0-based). Default is first non-singleton dimension. |
None
|
m
|
str
|
'x' to suppress extrapolation of end points. Output will be two samples shorter than input. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
ndarray
|
Teager energy waveform. |
Source code in pyvoicebox/v_teager.py
v_ditherq
¶
V_DITHERQ - Add dither and quantize.
v_ditherq
¶
Add dither and quantize.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Input signal. |
required |
m
|
str
|
Mode: 'w' white dither (default), 'h' high-pass dither, 'l' low-pass dither, 'n' no dither. |
'w'
|
zi
|
float
|
Initial state for filtering. Default is random. |
None
|
rng
|
Generator
|
Random number generator for reproducibility. Default uses np.random. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
ndarray
|
Dithered and quantized signal. |
zf |
float
|
Output state. |
Source code in pyvoicebox/v_ditherq.py
Array helpers¶
v_nearnonz
¶
V_NEARNONZ - Replace each zero element with nearest non-zero element.
v_nearnonz
¶
Replace each zero element with the nearest non-zero element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Input vector, matrix or larger array. |
required |
d
|
int or None
|
Dimension to apply filter along (0-based). Default is first non-singleton dimension. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
v |
ndarray
|
Same shape as x, with zeros replaced by nearest non-zero value. |
y |
ndarray
|
Same shape as x, index along dimension d from which v was taken (1-based). |
w |
ndarray
|
Same shape as x, distance to the nearest non-zero entry. |
Source code in pyvoicebox/v_nearnonz.py
v_rangelim
¶
V_RANGELIM - Limit the range of matrix elements.
v_rangelim
¶
Limit the range of matrix elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Input data. |
required |
r
|
float or array_like
|
If 2-element: explicit [min, max] limits. If scalar: range specification depending on mode. |
required |
m
|
str
|
Mode string: 'd' - range r in dB: 20*log10(max/min) 'r' - range r is max/min ratio 'l' - range r is max-min difference (default) 'p' - max(x) is top of range (default) 't' - min(x) is bottom of range 'g' - geometric mean is centre of range 'u' - mean is centre of range 'm' - median is centre of range 'c' - clip out-of-range values (default) 'n' - set out-of-range values to NaN |
'lp'
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
ndarray
|
Output data, same shape as x. |
Source code in pyvoicebox/v_rangelim.py
v_horizdiff
¶
V_HORIZDIFF - Estimate horizontal difference between two functions.
v_horizdiff
¶
Estimate horizontal difference between two functions of x.
Approximately: y(x) = v(x+z).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
(array_like, shape(n, m))
|
Each column is a function of x. |
required |
v
|
(array_like, shape(k))
|
Reference function. |
required |
x
|
(array_like, shape(n))
|
x values for y. Default (1:n). |
None
|
u
|
(array_like, shape(k))
|
x values for v. Default same as x. |
None
|
q
|
str
|
Interpolation mode: 'l' linear, 'p' pchip, 's' spline. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
z |
(ndarray, shape(n, m))
|
Horizontal difference. |
zm |
(ndarray, shape(m))
|
MMSE horizontal difference. |
Source code in pyvoicebox/v_horizdiff.py
v_interval
¶
V_INTERVAL - Classify X values into contiguous intervals.
v_interval
¶
Classify X values into contiguous intervals with boundaries from Y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Vector of test values. |
required |
y
|
array_like
|
Vector of monotonically increasing interval boundaries. Interval i is [y[i], y[i+1]). |
required |
m
|
str
|
Mode options: For x < y[0]: 'e' - extrapolate: i=1, f<0 (default) 'c' - clip: i=1, f=0 'n' - NaN: i=NaN, f=NaN 'z' - zero: i=0, f<0 For x >= y[-1]: 'E' - extrapolate: i=ny-1, f>1 (default) 'C' - clip: i=ny-1, f=1 'N' - NaN: i=NaN, f=NaN 'Z' - zero: i=ny, f>1 |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
i |
ndarray
|
Interval indices (1-based, matching MATLAB convention). |
f |
ndarray
|
Fractional position within the interval. |
Source code in pyvoicebox/v_interval.py
v_modsym
¶
V_MODSYM - Symmetric modulus function.
v_modsym
¶
Symmetric modulus function.
Adds an integer multiple of y onto x so that it lies in the range [r-y/2, r+y/2) if y is positive or (r-y/2, r+y/2] if y is negative.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Input data. |
required |
y
|
float or array_like
|
Modulus. Default is 1. |
1
|
r
|
float or array_like
|
Reference data. Default is 0. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
z |
ndarray
|
Output data. |
k |
ndarray
|
Integer multiple of y that was added. |
Source code in pyvoicebox/v_modsym.py
v_zerotrim
¶
V_ZEROTRIM - Remove trailing zero rows and columns.
v_zerotrim
¶
Remove trailing zero rows and columns from a matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array_like
|
Input matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
z |
ndarray
|
Trimmed matrix, or empty array if all zeros. |