10.数据平滑处理

10.数据平滑处理

简单移动平均线

简单移动平均线是计算与等权重的指示函数的卷积,也可以不等权重.
1.用ones函数创建一个元素均为1的数组,然后对整个数组除以N,得到等权重.
2.使用权值,调用convolve函数.
3.从convolve函数分安徽的数组中取出中间的长度为N的部分(即两者作卷积运算时完全重叠的区域.)
4.使用matplotlib绘图

import numpy as np
import matplotlib.pyplot as plt
import sys

N=int(sys.argv[1])
weights=np.ones(N)/N
print("WEIGHTS",weights)

c=np.loadtxt('/home/syd/Documents/stockdata.csv',delimiter=',',
skiprows=(2),usecols=(2,),unpack=True)
sam=np.convolve(weights,c)[N-1:-N+1]
t=np.arange(N-1,len(c))

plt.plot(t,c[N-1:],lw=1.0)
plt.plot(t,sam,lw=2.0)
plt.show()

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这里写图片描述

窗函数:hanning汉宁窗

汉宁窗是一个加权余弦窗函数.numpy.hanning(M) Return the Hanning window.

Parameters: M : int
Number of points in the output window. If zero or
Returns: out : ndarray, shape(M,)
The window, with the maximum value normalized to one (the value one
appears only if M is odd).

1.调用hanning函数计算权重,生成一个长度为N的窗口,输入参数N

N=int(sys.argv[1])
weights=np.hanning(N)
print(weights)
 
 
  • 1
  • 2
  • 3

2.使用convolve,进行卷积运算.然后绘图.

import numpy as np
import matplotlib.pyplot as plt
import sys

N=int(sys.argv[1])
weights=np.hanning(N)
print("WEIGHTS",weights)

c=np.loadtxt('/home/syd/Documents/stockdata.csv',delimiter=',',
skiprows=(2),usecols=(2,),unpack=True)
sam=np.convolve(weights/weights.sum(),c)[N-1:-N+1]
t=np.arange(N-1,len(c))

plt.plot(t,c[N-1:],lw=1.0)
plt.plot(t,sam,lw=2.0)
plt.show()

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这里写图片描述
这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值