-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFT_functions.py
138 lines (101 loc) · 3.61 KB
/
FFT_functions.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
""" Functions for computing the Fast Fourier Transform
Developed by Qikai Wu from The O'Hern Group at Yale University <https://jamming.research.yale.edu/>
"""
__author__ = 'Qikai Wu'
__credits__ = ['Qikai Wu']
__license__ = 'MIT License'
__version__ = '0.0.1'
__maintainer__ = 'Atoosa Parsa'
__email__ = '[email protected]'
__status__ = "Dev"
import numpy as np
import matplotlib.pyplot as plt
from plot_functions import Line_multi, Line_single
def FFT_Fup(Nt, F, dt, Freq_Vibr):
sampling_rate = 1/dt
t = np.arange(Nt)*dt
fft_size = Nt
xs = F[:fft_size]
xf = np.absolute(np.fft.rfft(xs)/fft_size)
freqs = (2*np.pi)*np.linspace(0, sampling_rate/2, fft_size//2+1)
ind = freqs<100
freqs = freqs[ind]
xf = xf[ind]
if 1 == 0:
Line_multi([freqs[1:], [Freq_Vibr, Freq_Vibr]], [xf[1:], [min(xf[1:]), max(xf[1:])]], ['o', 'r--'], 'Frequency', 'FFT', 'linear', 'log')
return freqs[1:], xf[1:]
def FFT_Fup_RealImag(Nt, F, dt, Freq_Vibr):
sampling_rate = 1/dt
t = np.arange(Nt)*dt
fft_size = Nt
xs = F[:fft_size]
xf = np.fft.rfft(xs)/fft_size
freqs = (2*np.pi)*np.linspace(0, sampling_rate/2, fft_size/2+1)
ind = freqs<70
freqs = freqs[ind]
xf = xf[ind]
xf_real = xf.real
xf_imag = xf.imag
if 1 == 0:
Line_multi([freqs[1:], [Freq_Vibr, Freq_Vibr]], [xf[1:], [min(xf[1:]), max(xf[1:])]], ['o', 'r--'], 'Frequency', 'FFT')
return freqs[1:], xf_real[1:], xf_imag[1:]
def vCorr_Cal(fft_size, Nt, y_raw):
y_fft = np.zeros(fft_size)
for jj in np.arange(fft_size):
sum_vcf = 0
sum_tt = 0
count = 0
for kk in np.arange(Nt-jj):
count = count+1
sum_vcf += y_raw[kk]*y_raw[kk+jj];
sum_tt = sum_tt+y_raw[kk]*y_raw[kk];
y_fft[jj] = sum_vcf/sum_tt;
return y_fft
def FFT_vCorr(Nt, N, vx_rec, vy_rec, dt):
sampling_rate = 1/dt
fft_size = Nt-1
freqs = (2*np.pi)*np.linspace(0, sampling_rate/2, fft_size/2+1)
for ii in np.arange(2*N):
#for ii in [0,4]:
if np.mod(ii, 10) == 0:
print('ii=%d\n' % (ii))
if ii >= N:
y_raw = vy_rec[:, ii-N]
else:
y_raw = vx_rec[:, ii]
y_fft = vCorr_Cal(fft_size, Nt, y_raw)
if ii == 0:
xf = np.absolute(np.fft.rfft(y_fft)/fft_size)
else:
xf += np.absolute(np.fft.rfft(y_fft)/fft_size)
ind = freqs<30
freqs = freqs[ind]
xf = xf[ind]
if 1 == 1:
Line_single(freqs[1:], xf[1:], 'o', 'Frequency', 'FFT')
return freqs[1:], xf[1:]
def FFT_vCorr_3D(Nt, N, vx_rec, vy_rec, vz_rec, dt):
sampling_rate = 1/dt
fft_size = Nt-1
freqs = (2*np.pi)*np.linspace(0, sampling_rate/2, fft_size/2+1)
for ii in np.arange(3*N):
#for ii in [0,4]:
if np.mod(ii, 10) == 0:
print('ii=%d\n' % (ii))
if ii >= 2*N:
y_raw = vz_rec[:, ii-2*N]
elif ii < N:
y_raw = vx_rec[:, ii]
else:
y_raw = vy_rec[:, ii-N]
y_fft = vCorr_Cal(fft_size, Nt, y_raw)
if ii == 0:
xf = np.absolute(np.fft.rfft(y_fft)/fft_size)
else:
xf += np.absolute(np.fft.rfft(y_fft)/fft_size)
ind = freqs<30
freqs = freqs[ind]
xf = xf[ind]
if 1 == 1:
Line_single(freqs[1:], xf[1:], 'o', 'Frequency', 'FFT')
return freqs[1:], xf[1:]