Denoising with a Gaussian Filter (Python / Scipy)
· One min read
As an example, let's use a 1[Hz] sine wave as the signal. Add noise generated from random numbers following a normal distribution with a mean of 0 and a standard deviation of 0.5 to the signal.
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter1d
t = np.arange(1000) / 100
s = np.sin(2*np.pi*t)
noise = np.random.normal(0, 0.5, size=len(t))
x = s + noise
plt.plot(t, x, label="+noise")
plt.plot(t, s, label="signal")
plt.legend(loc=1)
plt.show()
Apply a Gaussian filter with a standard deviation of 5. The larger the standard deviation, the smoother the result, but the more it deviates from the original signal.
y = gaussian_filter1d(x, 5)
plt.plot(t, y, label="filtered")
plt.plot(t, s, label="signal")
plt.legend(loc=1)
plt.show()
