-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFtdemo_func.py
More file actions
54 lines (45 loc) · 1.73 KB
/
Ftdemo_func.py
File metadata and controls
54 lines (45 loc) · 1.73 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
50
51
52
53
54
# ftdemo - Discrete Fourier transform demonstration program
# updated to be compatible with Python3, 20201026 - PB
# Set up configuration options and special features
import numpy as np
import matplotlib.pyplot as plt
import time
def time_fft(N, freq, phase, method):
"""Calculate a Fourier transform of a sine wave time series
N: number of data points
freq: frequency of sine wave
phase: phase in radians
method: 1 Direct summation, 2 FFT
Output: time taken to compute
"""
#* Initialize the sine wave time series to be transformed.
tau = 1. # Time increment
t = np.arange(N)*tau # t = [0, tau, 2*tau, ... ]
y = np.empty(N)
for i in range(N): # Sine wave time series
y[i] = np.sin(2*np.pi*t[i]*freq + phase)
f = np.arange(N)/(N*tau) # f = [0, 1/(N*tau), ... ]
#* Compute the transform using desired method: direct summation
# or fast Fourier transform (FFT) algorithm.
yt = np.zeros(N,dtype=complex)
startTime = time.time()
if int(method) == 1 : # Direct summation
twoPiN = -2. * np.pi * (1j) /N # (1j) = sqrt(-1)
for k in range(N):
for j in range(N):
expTerm = np.exp( twoPiN*j*k )
yt[k] += y[j] * expTerm
else: # Fast Fourier transform
yt = np.fft.fft(y)
stopTime = time.time()
return(stopTime - startTime)
npts = np.logspace(2.5,5.7, base=10.0)
fft_times = np.empty(len(npts),dtype='float64')
for i,n in enumerate(npts):
# print(i,n)
fft_times[i] = time_fft(int(n), 0.2, 0, 2)
fig, ax = plt.subplots()
ax.scatter(npts, fft_times)
ax.set_xlabel('Number of points')
ax.set_ylabel('Time to compute [s]')
plt.show()