-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadaptive_codebook.py
More file actions
34 lines (27 loc) · 984 Bytes
/
adaptive_codebook.py
File metadata and controls
34 lines (27 loc) · 984 Bytes
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
import numpy as np
class AdaptiveCodebook(object):
def __init__(self, vector_size, cb_size, min_period, np=True):
self.vector_size = vector_size
self.cb_size = cb_size
self.min_period = min_period
self.max_period = min_period + cb_size
self.np = np
self.samples = [0] * self.max_period
def add_vector(self, vector):
# Drop last vector_size samples and add vector to top
self.samples.extend(vector)
self.samples = self.samples[len(vector):]
def __len__(self):
return self.cb_size
def __getitem__(self, key):
if key >= self.cb_size:
raise IndexError
pos = key + self.min_period
if pos > self.vector_size:
ret = self.samples[-pos:-pos+self.vector_size]
else:
last = self.samples[-pos:]
ret = last + last[:self.vector_size-pos]
if self.np:
ret = np.asarray(ret,"f")
return ret