-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsignalSmooth.py
137 lines (104 loc) · 3.69 KB
/
signalSmooth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
cookb_signalsmooth.py
from: http://scipy.org/Cookbook/SignalSmooth
"""
import numpy as np
import matplotlib.pyplot as plt
def smooth(x, window_len=10, window='hanning'):
"""smooth the data using a window with requested size.
This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the begining and end part of the output signal.
input:
x: the input signal
window_len: the dimension of the smoothing window
window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
flat window will produce a moving average smoothing.
output:
the smoothed signal
example:
import numpy as np
t = np.linspace(-2,2,0.1)
x = np.sin(t)+np.random.randn(len(t))*0.1
y = smooth(x)
see also:
numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
scipy.signal.lfilter
TODO: the window parameter could be the window itself if an array instead of a string
"""
x = np.array(x)
if x.ndim != 1:
raise(ValueError, "smooth only accepts 1 dimension arrays.")
if x.size < window_len:
raise ValueError, "Input vector needs to be bigger than window size."
if window_len < 3:
return x
if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'"
s=np.r_[2*x[0]-x[window_len:1:-1], x, 2*x[-1]-x[-1:-window_len:-1]]
#print(len(s))
if window == 'flat': #moving average
w = np.ones(window_len,'d')
else:
w = getattr(np, window)(window_len)
y = np.convolve(w/w.sum(), s, mode='same')
return y[window_len-1:-window_len+1]
#*********** part2: 2d
from scipy import signal
def gauss_kern(size, sizey=None):
""" Returns a normalized 2D gauss kernel array for convolutions """
size = int(size)
if not sizey:
sizey = size
else:
sizey = int(sizey)
x, y = np.mgrid[-size:size+1, -sizey:sizey+1]
g = np.exp(-(x**2/float(size) + y**2/float(sizey)))
return g / g.sum()
def blur_image(im, n, ny=None) :
""" blurs the image by convolving with a gaussian kernel of typical
size n. The optional keyword argument ny allows for a different
size in the y direction.
"""
g = gauss_kern(n, sizey=ny)
improc = signal.convolve(im, g, mode='valid')
return(improc)
def smooth_demo():
t = np.linspace(-4,4,100)
x = np.sin(t)
xn = x + np.random.randn(len(t)) * 0.1
y = smooth(x)
ws = 31
plt.subplot(211)
plt.plot(np.ones(ws))
windows=['flat', 'hanning', 'hamming', 'bartlett', 'blackman']
plt.hold(True)
for w in windows[1:]:
#eval('plt.plot('+w+'(ws) )')
plt.plot(getattr(np, w)(ws))
plt.axis([0,30,0,1.1])
plt.legend(windows)
plt.title("The smoothing windows")
plt.subplot(212)
plt.plot(x)
plt.plot(xn)
for w in windows:
plt.plot(smooth(xn,10,w))
l = ['original signal', 'signal with noise']
l.extend(windows)
plt.legend(l)
plt.title("Smoothing a noisy signal")
#plt.show()
if __name__=='__main__':
# part 1: 1d
smooth_demo()
# part 2: 2d
X, Y = np.mgrid[-70:70, -70:70]
Z = np.cos((X**2+Y**2)/200.)+ np.random.normal(size=X.shape)
Z2 = blur_image(Z, 3)
plt.figure()
plt.imshow(Z)
plt.figure()
plt.imshow(Z2)
plt.show()