-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicInput.py
90 lines (69 loc) · 3.01 KB
/
DynamicInput.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
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
####################################################################################################
# FUNCTIONS TO HANDLE DYNAMIC INPUT SPECTOGRAM SIZE #
####################################################################################################
# Author: Rajesh R (Git: its-rajesh)
# Date: 14/Januray/2023
import numpy as np
class DynamicInput:
def __init__(self):
pass
'''
FUNCTION: ENCODES (SPLITS & MERGE) THE INPUT SPECTOGRAM INTO VARIOUS BINS (See Ref. Figure)
USE: To handle dynamic inputs to neural netwok.
Parameters:
(1) array: Input spectogram of size m*n
(2) frame: No of bins to be grouped together. Default is 3, If frame = no of columns in input spect/3,
and skip=3, then output is same as input.
(3) skip: Overlap within the bins (1<=skip<=3), default is 1 to maintain loss of info.
either use skip =1 or frame = 1 for no loss of info.
Note: Batches/Grouping is always 3.
'''
def encode(self, array, frame=3, skip=1):
data = []
for i in range(0, array.shape[1]-(3*frame-1), skip):
y_ = array[:, i:i+frame]
y = array[:, i+frame:i+(2*frame)]
y__ = array[:, i+(2*frame):i+(3*frame)]
concat = np.concatenate((y_, y, y__), axis=0, casting="same_kind")
data.append(concat)
self.encoded = np.array(data)
return self.encoded
'''
FUNCTION: DECODES (REVERSE OF ENCODE) THE SPLIT BINS INTO ORIGINAL SPECTOGRAM (See Ref. Figure)
USE: To handle dynamic inputs to neural netwok.
Parameters:
(1) array: Input spectogram bins of size l*m*n
(2) frame: Same value as given in encode function
(3) skip: Same value as given in encode function
Note: Batches/Grouping is always 3.
'''
def decode(self, array, frame=3, skip=1):
reconst = []
l, m, n = array.shape
flag = False
for i in array:
y_ = i[:int(m/3)]
y = i[int(m/3):int(2*(m/3))]
y__ = i[int(2*(m/3)):m]
if skip == 1:
if flag:
t = np.array([y_[:, 2]]).T
reconst = np.concatenate((reconst, t), axis=1, casting="same_kind")
else:
reconst = y_
if skip == 2:
if flag:
t = np.array([y_[:, 1:2]]).T
reconst = np.concatenate((reconst, t), axis=1, casting="same_kind")
else:
reconst = y_
if skip == 3:
if flag:
reconst = np.concatenate((reconst, y_), axis=1, casting="same_kind")
else:
reconst = y_
flag = True
if skip == 1:
reconst = np.concatenate((reconst, y, y__), axis=1, casting="same_kind")
self.decoded = np.array(reconst)
return self.decoded