-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_helpers.py
49 lines (39 loc) · 1.32 KB
/
audio_helpers.py
1
2
3
4
5
6
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
"""
File containing useful functions for audio analyzation.
"""
import numpy as np
def collect_samples_safe(arr, center: int, width: int, dtype=np.float32):
"""
Collects samples around the center. Values outside the array will be
filled with 0.
Args:
arr (np.ndarray) The 1D array to read the data from.
center (int) The center of the window for the selection.
width (int) The width of the window.
dtype (type, optional) The valuetype in the output.
Returns:
A 1d numpy array with the collected values.
"""
if arr.ndim != 1:
raise ValueError("Array is not 1D")
buffer = np.zeros(width, dtype=dtype)
for i in range(width):
sel = i - width//2 + center
if sel < 0:
continue
if sel >= len(arr):
break
buffer[i] = arr[sel]
return buffer
def blackman_harris_window(samples):
"""
Applies a Blackman-Harris over the Samples.
The passed buffer will be modified.
Args:
samples(np.ndarry) The samples that should be windowed.
"""
alpha = np.array([-0.48829, 0.14128, -0.001168])
terms = np.pi*np.arange(len(samples))/(len(samples) - 1)
window = 0.35875 + alpha[0] * np.cos(2*terms) \
+ alpha[1] * np.cos(4*terms) + alpha[2] * np.cos(6*terms)
samples *= window