numpy 如何减少数据抖动?

ghhaqwfi  于 2023-03-08  发布在  其他
关注(0)|答案(1)|浏览(164)

我训练了一个生成对抗网络来生成时间序列,左边是原始的时间序列,右边是生成的x1c 0d1x
如您所见,它们很相似,但我发现生成的要素在短时间间隔内有很多向上/向下的趋势,这就是线条看起来如此粗的原因。您对如何平滑每个要素的值有什么建议吗(6个特征)这样整体结构就被保留了,iIndieEe.只有线条变细了?由于numpy数组太大,无法共享,我就不包括了,不过,如果你需要小部分,我可以提供。我想知道解决这个问题的方法。

ia2d9nvy

ia2d9nvy1#

一种可能的方法是使用低通滤波器。2它允许去除产生的信号中的高频“抖动”。
请看下面的参考文献:
https://medium.com/analytics-vidhya/how-to-filter-noise-with-a-low-pass-filter-python-885223e5e9b7
您可以在下面找到一个取自此post的快速实现。

import numpy as np
from scipy.signal import butter, lfilter, freqz
import matplotlib.pyplot as plt

def butter_lowpass(cutoff, fs, order=5):
    return butter(order, cutoff, fs=fs, btype='low', analog=False)

def butter_lowpass_filter(data, cutoff, fs, order=5):
    b, a = butter_lowpass(cutoff, fs, order=order)
    y = lfilter(b, a, data)
    return y

# Filter requirements.
order = 6
fs = 30.0       # sample rate, Hz
cutoff = 3.667  # desired cutoff frequency of the filter, Hz

# Get the filter coefficients so we can check its frequency response.
b, a = butter_lowpass(cutoff, fs, order)

# Plot the frequency response.
w, h = freqz(b, a, fs=fs, worN=8000)
plt.subplot(2, 1, 1)
plt.plot(w, np.abs(h), 'b')
plt.plot(cutoff, 0.5*np.sqrt(2), 'ko')
plt.axvline(cutoff, color='k')
plt.xlim(0, 0.5*fs)
plt.title("Lowpass Filter Frequency Response")
plt.xlabel('Frequency [Hz]')
plt.grid()

# Demonstrate the use of the filter.
# First make some data to be filtered.
T = 5.0         # seconds
n = int(T * fs) # total number of samples
t = np.linspace(0, T, n, endpoint=False)
# "Noisy" data.  We want to recover the 1.2 Hz signal from this.
data = np.sin(1.2*2*np.pi*t) + 1.5*np.cos(9*2*np.pi*t) + 0.5*np.sin(12.0*2*np.pi*t)

# Filter the data, and plot both the original and filtered signals.
y = butter_lowpass_filter(data, cutoff, fs, order)

plt.subplot(2, 1, 2)
plt.plot(t, data, 'b-', label='data')
plt.plot(t, y, 'g-', linewidth=2, label='filtered data')
plt.xlabel('Time [sec]')
plt.grid()
plt.legend()

plt.subplots_adjust(hspace=0.35)
plt.show()

注:黄油低通、移动平均等都是低通滤波器,区别在于相移。滤波器的特点是波特图。完全消除抖动可能会以延迟信号(相移)为代价,因此您可以根据应用设计/选择方便的滤波器。

相关问题