#!/usr/bin/env python
# -*- coding:utf-8 -*-
from pylab import *
from numpy import *
import numpy
# 数据点图-数据点平滑处理
def moveing_average(ineterval,window_size):
window=ones(int(window_size))/float(window_size)
return convolve(ineterval,window,'same')
t=linspace(-4,4,100)
y=sign(t)+randn(len(t))*0.1
plot(t,y,'k.')
y_av=moveing_average(y,10)
plot(t,y_av,'r')
xlabel('time')
ylabel('value')
grid(True)
show()
# 图2-一个为曲线图 一个为折线图
windows=['flat','hanning','hamming','bartlett','blackman']
def smooth(x,window_len=11,window='hanning'):
if x.ndim!=1:
print('ere')
if x.size<window_len:
print('ee2')
if window_len<3:
return x
if not window in windows:
print('4')
s=numpy.r_[x[window_len-1:0:-1],x,x[-1:-window_len:-1]]
if window=='flat':
w=numpy.ones(window_len,'d')
else:
w=eval('numpy.'+window+'(window_len)')
y=numpy.convolve(w/w.sum(),s,mode='valid')
return y
t=linspace(-4,4,100)
x=sign(t)
xn=x+randn(len(t))*0.1
y=smooth(x)
ws=31
subplot(211)
plot(ones(ws))
for w in windows[1:]:
eval('plot('+w+'(ws))')
axis([0,30,0,1.1])
legend(windows)
title('smoothing')
subplot(212)
plot(x)
plot(xn)
for w in windows[1:]:
plot(smooth(xn,10,w))
I=['original ','noise']
I.extend(windows)
legend(I)
title('signal')
show()