Reputation: 949
I have some data which is shown in the below figure and am interested in finding some of its Fourier series coefficients.
r = np.array([119.80601628, 119.84629291, 119.85290735, 119.45778804,
115.64497439, 105.58519852, 100.72765819, 100.04327702,
100.08590518, 100.35824977, 101.58424993, 105.47976376,
112.27556007, 117.07679226, 118.99998888, 119.60458086,
119.78624424, 119.83022022, 119.36116943, 115.72323767,
106.58946834, 101.19479124, 100.11537349, 100.13313755,
100.41846106, 101.42255377, 104.33650237, 109.73625492,
115.14763728, 118.24665037, 119.35359999, 119.68061835])
z = np.array([-411.42980545, -384.98596279, -358.13032372, -330.89578468,
-303.39129113, -275.76248957, -248.24478443, -221.07069838,
-194.33260984, -168.05271807, -142.19357982, -116.62090103,
-91.15354178, -65.56745626, -39.65284757, -13.29632162,
13.54374939, 40.84929432, 68.50496394, 96.33720787,
124.08525182, 151.36802193, 177.98791952, 204.0805317 ,
229.85399128, 255.44727674, 281.02166554, 306.75399703,
332.74638285, 359.05528646, 385.74336711, 412.8189858 ])
plt.plot(z, r, label='data')
plt.legend()
Then I calculate the average sampling period, since it is not constant as seen in the Z variable:
l = []
for i in range(32-1):
l.append(z[i]-z[i+1])
Ts = np.mean(l)
Then I calculate the fft: from scipy.fftpack import fft
rf = scipy.fftpack.fft(r)
For reconstruction of the signal then:
fs = 1/Ts
amp = np.abs(rf)/r.shape[0]
n = r.shape[0]
s = 0
for i in range(n//2):
phi = np.angle(rf[i], deg=False)
a = amp[i]
k = i*fs/n
s += a*np.cos(2*np.pi*k *(z) +phi)
plt.plot(z, s, label='fft result')
plt.plot(z, r, label='data')
plt.legend()
The result is strange however both in terms of amplitude and frequency.
Upvotes: 0
Views: 883
Reputation: 684
The complex spectrum is a symmetric spectrum with the range of (-fMax/2, ..., +fMax/2). You only used the right hand positive part of the spectrum. This means, your reconstructed signal contains only half of the spectrums frequencies. Because the spectrum is symmetric, all you have to do is to double the calculated absolute values. However, there is an important exception. The DC value amplitude[0] must not be doubled.
Upvotes: 2