-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfourier_transform.py
63 lines (53 loc) · 1.46 KB
/
fourier_transform.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
# ------------------------------------------------------------------------------
#
# blind chees: fourier_transform.py
#
# ------------------------------------------------------------------------------
import numpy as np
def dft(s):
"""
Get the Discrete Fourier Transform from the input signal.
Time complexity: O(n ^ 2)
Arguments:
s -- A numpy array representing the signal.
Return:
S -- A numpy array with the dft from the signal.
"""
N = s.shape[0]
n = np.arange(N)
k = n.reshape((N, 1))
M = np.exp(-2j * np.pi * k * n / N)
S = np.dot(M, s)
return S
def fft(s):
"""
Get the Discrete Fourier Transform using the fast
Cooley - Tukey Algorithm from the input signal.
Time complexity: O(n * log(n))
note: n has to be a power of two
Arguments:
s -- A numpy array representing the signal.
Return:
S -- A numpy array with the fft from the signal.
"""
n = s.shape[0]
if n == 1:
return s
wi = np.exp(-2j * np.pi * np.arange(n) / n)
s_even = fft(s[::2])
s_odd = fft(s[1::2])
S0 = s_even + wi[:n//2] * s_odd
S1 = s_even + wi[n//2:] * s_odd
S = np.concatenate((S0, S1))
return S
def dct(s):
"""
Get the Discrete Cosine Transform from the input signal.
Time complexity: O(n ^ 2)
Arguments:
s -- A numpy array representing the signal.
Return:
S -- A numpy array with the dct from the signal.
"""
S = dft(s)
return S.real