简体   繁体   English

如何通过应用汉明窗使波形更平滑?

[英]How to make smoother wave by apply hamming window?

I have try to make the smoother wave (from stock price) but I don't know how to apply it to my wave.我尝试制作更平滑的波浪(从股票价格),但我不知道如何将其应用于我的波浪。

import numpy as np
wave = [1575.7918235085228, 1574.2183726917613, 1571.9212868430398, 1569.5071067116478, 1568.4249178799716, 1567.4649192116478, 1566.7141801313921, 1565.9849054509943, 1565.4699951171874, 1566.766004527699, 1567.222733931108, 1567.4478271484375, 1566.3074729225852, 1563.4207475142045, 1559.6012806285512, 1555.0241965553978, 1552.6043723366477, 1550.9003773082386, 1549.0460116299716, 1548.2069202769887, 1549.2643687855114, 1551.792538174716, 1554.6467240767045, 1557.9874578302556, 1560.9547341086648, 1563.2614679509943, 1565.0818204012785, 1565.241825727983, 1565.4223677201705, 1566.1059992009943, 1566.3267223011364, 1566.7634410511364, 1566.691796875, 1567.2850763494318, 1567.2025368430398, 1566.6512761896306, 1567.7338201349432, 1569.591093306108, 1570.538554243608, 1571.8510919744317, 1573.8294588955966]
hamm = np.hamming(len(wave))

here is example image这是示例图像例子 Reference of hamming moving average from aspen program 来自aspen程序的汉明移动平均线的参考

my wave variable is green line after plot.我的波形变量是绘图后的绿线。 this program have function to apply hamming to it then plot the red line该程序具有对它应用汉明然后绘制红线的功能

So I just want to know how this program do.所以我只想知道这个程序是怎么做的。

Thank you.谢谢。

Typically stock prices are "smoothed" using a sliding window technique.通常使用滑动窗口技术“平滑”股票价格。 The window duration is typically 10-100 days or so.窗口期通常为 10-100 天左右。 Starting with the day 1, grab the next N days of data and calculate the average (the window of time here is day 1 to 1 + N).从第 1 天开始,抓取接下来 N 天的数据并计算平均值(这里的时间窗口是第 1 天到第 1 + N 天)。 This is your new "smoothed" value for day 1. Then move to day 2 and do the same thing, but now your window is day 2 to 2+N and you have your smoothed value for day 2. Repeat until you run out of data.这是第 1 天的新“平滑”值。然后移至第 2 天并执行相同的操作,但现在您的窗口是第 2 天到第 2+N 天,并且您拥有第 2 天的平滑值。重复直到用完数据。

Without realizing it, you have applied a rectangular window function each time you did this.不知不觉中,您每次执行此操作时都应用了一个矩形窗函数。 The rectangular window function has value 1 at all times in the window and 0 everywhere else.矩形窗口函数在窗口中始终为 1,在其他地方为 0。 When you multiply your original data by the window you have effectively sampled only the data points that fell into the window.当您将原始数据乘以窗口时,您实际上只对落入窗口的数据点进行了采样。 The values of the data points are unchanged though because with a rectangular window you multiplied by 1.数据点的值不变,因为在矩形窗口中您乘以 1。

The rectangular window function has some undesireable properties though.但是矩形窗函数有一些不受欢迎的特性。 If you take the Fourier transform of it you'll see that it has very high sidelobes.如果你对它进行傅立叶变换,你会发现它有非常高的旁瓣。 This has the effect of broadening or "smearing" frequency spikes in the original data.这会导致原始数据中的频率尖峰变宽或“模糊”。 Thus people use other windowing functions such as hamming, hanning, etc. These window functions do not have a constant value of 1 for each point in the window function.因此,人们使用其他窗口函数,例如汉明、汉宁等。这些窗口函数对于窗口函数中的每个点都没有固定值 1。

So what you need to do in your code is take your original wave array and set up a loop that extracts N samples, multiplies them by a hamming window array of length N, then calculate the average and store it in a new array.所以你需要在你的代码中做的是获取你的原始波形数组并设置一个循环来提取 N 个样本,将它们乘以长度为 N 的汉明窗数组,然后计算平均值并将其存储在一个新数组中。

from @Tom Johnson suggestion.来自@Tom Johnson 的建议。

df['wma'] = WMA(df['close'].values,N)
h_win = np.hamming(N)
close = df['close'].values

hma = [ np.nan for i in range(N-1) ]
sum_h = sum(h_win)

for i in range(N-1, len(close)) :
    summation = 0

    for j in range(N) :
        summation += h_win[j]*close[i-j]
    average = summation / sum_h

    hma.append(average)

df['hma'] = pd.Series(hma, index=df.index)

If anyone is looking for this, it is now present in the rolling function of pandas under win_type.如果有人正在寻找这个,它现在存在于 win_type 下的熊猫滚动功能中。

pandas.DataFrame.rolling pandas.DataFrame.rolling

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM