-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMDCT.py
More file actions
49 lines (41 loc) · 1.24 KB
/
MDCT.py
File metadata and controls
49 lines (41 loc) · 1.24 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
import numpy as np
def mdct4(x):
N = x.shape[0]
if N % 4 != 0:
raise ValueError("MDCT4 only defined for vectors of length multiple of four.")
M = N // 2
N4 = N // 4
rot = np.roll(x, N4)
rot[:N4] = -rot[:N4]
t = np.arange(0, N4)
w = np.exp(-1j * 2 * np.pi * (t + 1. / 8.) / N)
c = np.take(rot, 2 * t) - np.take(rot, N - 2 * t - 1) \
- 1j * (np.take(rot, M + 2 * t) - np.take(rot, M - 2 * t - 1))
c = (2. / np.sqrt(N)) * w * np.fft.fft(0.5 * c * w, N4)
y = np.zeros(M)
y[2 * t] = np.real(c[t])
y[M - 2 * t - 1] = -np.imag(c[t])
return y
def imdct4(x):
N = x.shape[0]
if N % 2 != 0:
raise ValueError("iMDCT4 only defined for even-length vectors.")
M = N // 2
N2 = N * 2
t = np.arange(0, M)
w = np.exp(-1j * 2 * np.pi * (t + 1. / 8.) / N2)
c = np.take(x, 2 * t) + 1j * np.take(x, N - 2 * t - 1)
c = 0.5 * w * c
c = np.fft.fft(c, M)
c = ((8 / np.sqrt(N2)) * w) * c
rot = np.zeros(N2)
rot[2 * t] = np.real(c[t])
rot[N + 2 * t] = np.imag(c[t])
t = np.arange(1, N2, 2)
rot[t] = -rot[N2 - t - 1]
t = np.arange(0, 3 * M)
y = np.zeros(N2)
y[t] = rot[t + M]
t = np.arange(3 * M, N2)
y[t] = -rot[t - 3 * M]
return y