Basic test: Smoothing the noised signal

2015.10.19 DW


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()


Information about this notebook
============================================================
Date: 2015-11-09
Python Version: 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)]
Git directory: C:\Users\Dominik\Documents\GitRep\kt-2015-DSPHandsOn\.git
Current git SHA: d39aba3cf35a90ec9d97eb4730a62dc77a29240f
Current remote: fork
Current branch: master
origin remote URL: https://github.com/ktakagaki/kt-2015-DSPHandsOn.git

In [3]:
% matplotlib inline

I am trying to remove white noise from the original wave with different filters.

Define the sine wave with wave number 5 and add white noise.


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]:
[<matplotlib.lines.Line2D at 0xa9503c8>]

In [6]:
plt.plot(signal1)
plt.title("Noised sine wave")


Out[6]:
<matplotlib.text.Text at 0x131aa5c0>

Smooth the signal with a moving averege filter.


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]:
<matplotlib.text.Text at 0x136304a8>

Smooth the signal with a gaussian filter.


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]:
<matplotlib.text.Text at 0x13680cf8>

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.