In [1]:
import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage.filters as scnf
import sys
# Add a new path with needed .py files.
sys.path.insert(0, 'C:\Users\Dominik\Documents\GitRep\kt-2015-DSPHandsOn\MedianFilter\Python')
import gitInformation
In [2]:
gitInformation.printInformation()
In [3]:
% matplotlib inline
I am trying to remove white noise from the original wave with different filters.
In [4]:
# Creates a sine wave with wave number 5.
data = np.fromfunction(lambda x: np.sin(x/1024*2*np.pi*5), (1024,))
# Creates an array with random values and the same length as the data array.
noise = np.random.normal(0,1.0,len(data))
signal = data + noise
signal1 = signal
In [5]:
plt.plot(data)
Out[5]:
In [6]:
plt.plot(signal1)
plt.title("Noised sine wave")
Out[6]:
In [7]:
# Moving averege filter with a length of 30, 5 times calculated.
for i in range (0,5):
signal = np.convolve(signal, np.ones(30)/30, mode = 'same')
In [8]:
plt.plot(signal)
plt.title("Result of the moving averege filter")
Out[8]:
In [9]:
# Gaussian filter of the noised signal with a standard deviation for Gaussian kernel of 30.
smoothed = scnf.gaussian_filter(signal1, 30)
In [10]:
plt.plot(smoothed)
plt.title("Result of the gaussian filter")
Out[10]:
Here you can see the results of both filters. The results are nearly the same and depend on the noise. In fact that the moving averege filter must be calculated multiple times to get a smooth result, the gaussian filter maybe is the better option.