-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFourier-Transform.py
More file actions
50 lines (36 loc) · 1.38 KB
/
Fourier-Transform.py
File metadata and controls
50 lines (36 loc) · 1.38 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
#Fourier Transform
# F(ω) = ∫[−∞ to ∞] f(t) * e^(-j * ω * t) dt
# where:
# - F(ω) is the Fourier Transform (frequency domain representation)
# - f(t) is the original function (time domain representation)
# - ω is the angular frequency (ω = 2πf, f in Hz)
# - e^(-j * ω * t) is the complex exponential
# - ∫ is the integral over time (t)
def fourier_transform(f_t, t, omega):
dt = t[1] - t[0] # time step
result = 0 # initialize integral result
# This loop is the discrete version of the integral ∫
for i in range(len(t)):
# f(t) * e^(-j * ω * t) * dt
result += f_t[i] * (cos(-omega * t[i]) + 1j * sin(-omega * t[i])) * dt
return result
'''
The Fourier Transform integral ∫ is just:
1. A for-loop that goes through time points
2.Multiplies your signal f(t) by e^(-jωt)
3. Adds up all the pieces (dt)
Boils down to:
Loop through time
Multiply stuff
Add it up
The only slightly tricky part is e^(-jωt), which is just:
cos(-ωt) + j*sin(-ωt) [Euler's formula]
'''
# Example usage:
from math import cos, sin, pi
# Create time points
t = [i/100 for i in range(-100, 101)] # -1 to 1 seconds
f_t = [cos(2*pi*t[i]) for i in range(len(t))] # test signal: cos(2πt)
omega = 2*pi # test frequency: 2π rad/s
F_omega = fourier_transform(f_t, t, omega)
print(f"F({omega}) = {F_omega}")