forked from sri-kankanahalli/autoencoder-speech-compression
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindowing.py
More file actions
95 lines (69 loc) · 2.93 KB
/
windowing.py
File metadata and controls
95 lines (69 loc) · 2.93 KB
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
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
# ==========================================================================
# functions to handle windowing of waveforms (extracting windows from a
# waveform, and reconstructing a waveform from individual windows)
# ==========================================================================
import numpy as np
import math
from consts import *
# extract overlapping windows from waveform
def extract_windows(data):
numWindows = int(math.ceil(float(len(data)) / STEP_SIZE))
# loop over every window
windows = []
for i in xrange(0, numWindows):
# get the frame
startOfFrame = STEP_SIZE * i
endOfFrame = startOfFrame + WINDOW_SIZE
frame = data[startOfFrame:endOfFrame]
# pad frame to proper size, if there's not enough data
if len(frame) < WINDOW_SIZE:
frame = np.pad(frame, (0, WINDOW_SIZE - len(frame)), \
'constant', constant_values=[0])
if (i == 0):
windows = np.reshape(np.array(frame), (1, len(frame)))
else:
tmp = np.reshape(np.array(frame), (1, len(frame)))
windows = np.append(windows, tmp, axis = 0)
windows = windows.astype(np.float32)
if (EXTRACT_MODE == 0):
for i in xrange(0, windows.shape[0]):
windows[i] *= WINDOWING_MULT
return windows
# reconstruct waveform from overlapping windows
def reconstruct_from_windows(windows):
reconstruction = []
lastWindow = []
for i in xrange(0, windows.shape[0]):
r = windows[i, :]
if (i == 0):
reconstruction = r
else:
overlapLastWindow = reconstruction[-OVERLAP_SIZE:]
overlapThisWindow = r[:OVERLAP_SIZE]
unmodifiedPart = r[OVERLAP_SIZE:]
overlappedPart = np.copy(overlapLastWindow)
for j in xrange(0, OVERLAP_SIZE):
if (EXTRACT_MODE == 1):
thisMult = OVERLAP_FUNC[j]
lastMult = OVERLAP_FUNC[j + OVERLAP_SIZE]
else:
thisMult = 1.0
lastMult = 1.0
# use windowing function
overlappedPart[j] = overlapThisWindow[j] * thisMult + \
overlapLastWindow[j] * lastMult
reconstruction[-OVERLAP_SIZE:] = overlappedPart
reconstruction = np.concatenate([reconstruction, unmodifiedPart])
return reconstruction
# extract windows for list of waveforms
def extract_windows_multiple(wavelist, collapse = False):
windowlist = []
for waveform in wavelist:
windows = extract_windows(waveform)
if (windowlist == []):
windowlist = [windows]
else:
windowlist += [windows]
if (collapse):
windowlist = np.array([i for z in windowlist for i in z])
return windowlist