-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis_window.py
59 lines (44 loc) · 1.62 KB
/
analysis_window.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
50
51
52
53
54
55
56
57
58
59
import numpy as np
from scipy.signal.windows import hamming
class AnalysisWindow:
def __init__(self, sample_rate=48000, overlap=0.5):
self.sample_rate = sample_rate
self.overlap = overlap
self.size = self.calc_window_size()
self.data = self.apply_hamming_window()
self.window_overlap = int(self.size * self.overlap)
def calc_window_size(self):
"""
Size of the analysis window.
Args:
sample_rate (int): Sample rate of the signal.
Returns:
int: Size of the analysis window.
"""
window_size = round((self.sample_rate / 8000) * 256)
if window_size % 2 != 0:
window_size -= 1
return window_size
def apply_hamming_window(self):
"""
Build the analysis window data.
Args:
window_size (int): Size of the window.
Returns:
np.ndarray: The window data.
"""
return hamming(self.size, sym=False)
def calc_time_spaces(self, signal):
"""
Calculate the size of each frame.
Args:
signal (np.ndarray): The input signal.
Returns:
np.ndarray: The time spaces when frames begin (in seconds).
"""
analysis_window_len = len(self.data)
signal_len = len(signal)
n_col = (signal_len - self.window_overlap) // (analysis_window_len - self.window_overlap)
colindex = 1 + np.arange(n_col) * (analysis_window_len - self.window_overlap)
time_spaces = ((colindex - 1) + (analysis_window_len / 2)) / self.sample_rate
return time_spaces