-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzero_hold.py
53 lines (39 loc) · 1.07 KB
/
zero_hold.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
import numpy
def fractional_repeat(x, r, out_len=None):
'''
Upsample x by repeating its values (zero-hold)
Parameters
----------
x: array_like
the signal to upsample
r: float
the ratio of the sampling rates
out_len: int, optional
desired output length
'''
if out_len is None:
out_len = int(r * x.shape[0])
if x.ndim == 1:
y = numpy.zeros(out_len, dtype=x.dtype)
else:
y = numpy.zeros((out_len,) + x.shape[1:], dtype=x.dtype)
length = 0
i = 0
sample_error = 0
while length < out_len and i < x.shape[0]:
end = min(out_len, length + int(r))
L = end - length
y[length:end,] = x[i,]
length = end
# finish early if necessary
if end == out_len:
break
# keep track of fractional sample error accumulation
sample_error += r - L
# adjust when necessary
if sample_error > 1.:
y[length,] = x[i,]
sample_error -= 1
length += 1
i += 1
return y