时间序列降噪保持锐利边缘
Noise reduction in time series keeping sharp edges
在来自功率计的时间序列中,过程和传感器都有噪声。为了识别步骤,我想在不牺牲边缘陡度的情况下过滤噪声。
想法是做一个 rolling(window).mean()
=> 消除边缘或 rolling(window).median()
=> 但是如果 window 尺寸需要小,这会产生谐波噪声问题。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# create a reference signal
xrng = 50
sgn = np.zeros(xrng)
sgn[10:xrng//2] = 1
sgn[xrng//2:xrng-10]=0.5
fig = plt.figure(figsize=(10,6))
plt.plot(sgn, label='raw')
T=3 # period of the sine like noise (random phase shifts not modeled)
noise1 = (np.random.rand(xrng)-0.5)*0.2 # sensor noise
noise2 = np.sin(np.arange(xrng)*2*np.pi/T)*0.1 # harmonic noise
sg_n = sgn + noise1 + noise2 # noised signal
plt.plot(sg_n, label='noised')
# 1. filter mean (good for hamonic)
mnfltr = np.ones(7)/7
sg_mn = np.convolve(mnfltr,sg_n, 'same')
plt.plot(sg_mn, label='roll_mean')
# 2. filter median (good for edges)
median = pd.Series(sg_n).rolling(9).median().shift(-4)
plt.plot(median, label='roll_med')
plt.legend()
plt.show()
输出如下:
有没有一种方法可以结合这两种过滤器来获得两种好处或任何其他方法?
如果噪声幅度不会掩盖步长,您可以使用完全不同的方法重建步进信号。
您的设置:
import numpy as np
import matplotlib.pyplot as plt
xrng = 50
sgn = np.zeros(xrng)
sgn[10:xrng//2] = 1
sgn[xrng//2:xrng-10]=0.5
fig = plt.figure(figsize=(10,6))
plt.plot(sgn, label='raw')
T=3 # period of the sine like noise (random phase shifts not modeled)
noise1 = (np.random.rand(xrng)-0.5)*0.2 # sensor noise
noise2 = np.sin(np.arange(xrng)*2*np.pi/T)*0.1 # harmonic noise
sg_n = sgn + noise1 + noise2 # noised signal
plt.plot(sg_n, label='noised')
噪声信号可以数字化
bins = np.arange(-.25, 2, .5)
plt.plot((np.digitize(sg_n, bins)-1)/2, '.', markersize=8, label='reconstructed from noiced')
plt.legend();
结果:
在来自功率计的时间序列中,过程和传感器都有噪声。为了识别步骤,我想在不牺牲边缘陡度的情况下过滤噪声。
想法是做一个 rolling(window).mean()
=> 消除边缘或 rolling(window).median()
=> 但是如果 window 尺寸需要小,这会产生谐波噪声问题。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# create a reference signal
xrng = 50
sgn = np.zeros(xrng)
sgn[10:xrng//2] = 1
sgn[xrng//2:xrng-10]=0.5
fig = plt.figure(figsize=(10,6))
plt.plot(sgn, label='raw')
T=3 # period of the sine like noise (random phase shifts not modeled)
noise1 = (np.random.rand(xrng)-0.5)*0.2 # sensor noise
noise2 = np.sin(np.arange(xrng)*2*np.pi/T)*0.1 # harmonic noise
sg_n = sgn + noise1 + noise2 # noised signal
plt.plot(sg_n, label='noised')
# 1. filter mean (good for hamonic)
mnfltr = np.ones(7)/7
sg_mn = np.convolve(mnfltr,sg_n, 'same')
plt.plot(sg_mn, label='roll_mean')
# 2. filter median (good for edges)
median = pd.Series(sg_n).rolling(9).median().shift(-4)
plt.plot(median, label='roll_med')
plt.legend()
plt.show()
输出如下:
有没有一种方法可以结合这两种过滤器来获得两种好处或任何其他方法?
如果噪声幅度不会掩盖步长,您可以使用完全不同的方法重建步进信号。
您的设置:
import numpy as np
import matplotlib.pyplot as plt
xrng = 50
sgn = np.zeros(xrng)
sgn[10:xrng//2] = 1
sgn[xrng//2:xrng-10]=0.5
fig = plt.figure(figsize=(10,6))
plt.plot(sgn, label='raw')
T=3 # period of the sine like noise (random phase shifts not modeled)
noise1 = (np.random.rand(xrng)-0.5)*0.2 # sensor noise
noise2 = np.sin(np.arange(xrng)*2*np.pi/T)*0.1 # harmonic noise
sg_n = sgn + noise1 + noise2 # noised signal
plt.plot(sg_n, label='noised')
噪声信号可以数字化
bins = np.arange(-.25, 2, .5)
plt.plot((np.digitize(sg_n, bins)-1)/2, '.', markersize=8, label='reconstructed from noiced')
plt.legend();
结果: