-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft.py
58 lines (41 loc) · 1.12 KB
/
fft.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
from scipy.fft import fft, fftfreq
import numpy as np
import matplotlib.pyplot as plt
from functools import cached_property
class FFTResults:
def __init__(self, xf, yf):
self.__xf = xf
self.__yf = yf
self.__N = len(xf)
@property
def xf(self):
return self.__xf
@property
def yf(self):
return self.__yf
@property
def N(self):
return self.__N
@cached_property
def positive_frequencies(self):
pos_xf = self.xf[:self.N//2]
pos_yf = 2.0/self.N * np.abs(self.yf[0:self.N//2])
return pos_xf, pos_yf
def plot(self):
plt.plot(*self.positive_frequencies)
plt.grid()
plt.show()
def signal_fft(signal, sample_rate):
N = len(signal)
yf = fft(signal)
xf = fftfreq(N, sample_rate)
return FFTResults(xf, yf)
if __name__ == "__main__":
# Number of sample points
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N, endpoint=False)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
fft_arr = signal_fft(y, T)
fft_arr.plot()