-
Notifications
You must be signed in to change notification settings - Fork 10
/
3.1.Fourier_series.py
83 lines (76 loc) · 2.37 KB
/
3.1.Fourier_series.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
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('1.alc_s1.csv')
df1=df["sensor value"]
df2=df["sensor position"]
FP1=[]
FP1_1=[]
FP2=[]
FP2_1=[]
s=[]
a=1
for b in range (1,460):
if df2[b]=="FP1":
FP1.append(df1[b])
FP1_1.append(df2[b])
if df2[b]=="FP2":
FP2.append(df1[b])
FP2_1.append(df2[b])
s.append(b)
#plt.plot(FP2)
# Python example - Fourier transform using numpy.fft method
import numpy as np
import matplotlib.pyplot as plotter
# How many time points are needed i,e., Sampling Frequency
samplingFrequency = 204;
# At what intervals time points are sampled
samplingInterval = 1 / samplingFrequency;
# Begin time period of the signals
beginTime = 0;
# End time period of the signals
endTime = 1;
# Frequency of the signals
signal1Frequency = 4;
signal2Frequency = 7;
# Time points
time = np.arange(beginTime, endTime, samplingInterval);
# Create two sine waves
amplitude1 = FP2
amplitude2 = np.sin(2*np.pi*signal2Frequency*time)
# Create subplot
figure, axis = plotter.subplots(4, 1)
plotter.subplots_adjust(hspace=1)
# Time domain representation for sine wave 1
axis[0].set_title('Sine wave with a frequency of 4 Hz')
axis[0].plot(time, amplitude1)
axis[0].set_xlabel('Time')
axis[0].set_ylabel('Amplitude')
# Time domain representation for sine wave 2
axis[1].set_title('Sine wave with a frequency of 7 Hz')
axis[1].plot(time, amplitude2)
axis[1].set_xlabel('Time')
axis[1].set_ylabel('Amplitude')
# Add the sine waves
amplitude = amplitude1 + amplitude2
# Time domain representation of the resultant sine wave
axis[2].set_title('Sine wave with multiple frequencies')
axis[2].plot(time, amplitude)
axis[2].set_xlabel('Time')
axis[2].set_ylabel('Amplitude')
# Frequency domain representation
fourierTransform = np.fft.fft(amplitude)/len(amplitude) # Normalize amplitude
fourierTransform = fourierTransform[range(int(len(amplitude)/2))] # Exclude sampling frequency
tpCount = len(amplitude)
values = np.arange(int(tpCount/2))
timePeriod = tpCount/samplingFrequency
frequencies = values/timePeriod
# Frequency domain representation
axis[3].set_title('Fourier transform depicting the frequency components')
axis[3].plot(frequencies, abs(fourierTransform))
axis[3].set_xlabel('Frequency')
axis[3].set_ylabel('Amplitude')
plotter.show()