I am following this link to do a smoothing of my data set. The technique is based on the principle of removing the higher order terms of the Fourier Transform of the signal, and so obtaining a smoothed function. This is part of my code:
N = len(y)
y = y.astype(float) # fix issue, see below
yfft = fft(y, N)
yfft[31:] = 0.0 # set higher harmonics to zero
y_smooth = fft(yfft, N)
ax.errorbar(phase, y, yerr = err, fmt='b.', capsize=0, elinewidth=1.0)
ax.plot(phase, y_smooth/30, color='black') #arbitrary normalization, see below
However some things do not work properly. Indeed, you can check the resulting plot : The blue points are my data, while the black line should be the smoothed curve.
First of all I had to convert my array of data y
by following this discussion.
Second, I just normalized arbitrarily to compare the curve with data, since I don't know why the original curve had values much higher than the data points.
Most importantly, the curve is like "specular" to the data point, and I don't know why this happens. It would be great to have some advices especially to the third point, and more generally how to optimize the smoothing with this technique for my particular data set shape.
You can use some filters like Savitzky-Golay filter on your data before applying Fourier transform to smooth them and then use Fourier transform to find the frequencies of the discrete signal. You can also test interpolation functions like Spline functions instead of Fourier transform to interpolate your data.
A Fourier series is a representation of a wave form or other periodic function as a sum of sines and cosines. It is named after the French mathematician and physicist Jean-Baptiste Joseph Fourier (1768–1830). Since sound waves are made up of sine waves, Fourier transforms are widely used in signal processing.
Fourier analysis is a type of mathematical analysis that attempts to identify patterns or cycles in a time series data set which has already been normalized. In particular, it seeks to simplify complex or noisy data by decomposing it into a series of trigonometric or exponential functions, such as sine waves.
The Fourier filter is a type of filtering function that is based on manipulation of specific frequency components of a signal. It works by taking the Fourier transform of the signal, then attenuating or amplifying specific frequencies, and finally inverse transforming the result.
Your problem is probably due to the shifting that the standard FFT does. You can read about it here.
Your data is real, so you can take advantage of symmetries in the FT and use the special function np.fft.rfft
import numpy as np
x = np.arange(40)
y = np.log(x + 1) * np.exp(-x/8.) * x**2 + np.random.random(40) * 15
rft = np.fft.rfft(y)
rft[5:] = 0 # Note, rft.shape = 21
y_smooth = np.fft.irfft(rft)
plt.plot(x, y, label='Original')
plt.plot(x, y_smooth, label='Smoothed')
plt.legend(loc=0)
plt.show()
If you plot the absolute value of rft, you will see that there is almost no information in frequencies beyond 5, so that is why I choose that threshold (and a bit of playing around, too).
Here the results:
From what I can gather you want to build a low pass filter by doing the following:
Looking at your code, instead of doing 3) you're just doing another fourier transform. Instead, try doing an actual inverse fourier transform to move back to the time domain:
y_smooth = ifft(yfft, N)
Have a look at scipy signal to see a bunch of already available filters.
(Edit: I'd be curious to see the results, do share!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With