python 數據可視化 -- 真實數據的噪聲平滑處理

平滑數據噪聲的一個簡單樸素的作法是,對窗口(樣本)求平均,而後僅僅繪製出給定窗口的平均值,而不是全部的數據點。windows

import matplotlib.pyplot as plt import numpy as np def moving_average(interval, window_size): window = np.ones(int(window_size)) / float(window_size) return np.convolve(interval, window, 'same')  # numpy的卷積函數
 t = np.linspace(start = -4, stop = 4, num = 100) y = np.sin(t) + np.random.randn(len(t)) * 0.1 y_av = moving_average(interval = y, window_size = 10) plt.plot(t, y, "b.-", t, y_av, "r.-") plt.xlabel('Time') plt.ylabel('Value') plt.legend(['original data', 'smooth data']) plt.grid(True) plt.show()

如下方法是基於信號(數據點)窗口的卷積(函數的總和)dom

import matplotlib.pyplot as plt import numpy as np WINDOWS = ['flat', 'hanning', 'hamming', 'bartlett', 'blackman'] def smooth(x, window_len = 11, window = 'hanning'): if x.ndim != 1: raise ValueError('smooth only accepts 1 dimension arrays.') if x.size < window_len: raise ValueError('Input vector needs to be bigger than window size.') if window_len < 3: return x if not window in WINDOWS: raise ValueError('Window is one of "flat", "hanning", "hamming", "bartlett", "blackman"') s = np.r_[x[window_len-1:0:-1], x, x[-1:-window_len:-1]] if window == 'flat': w = np.ones(window_len, 'd') else: w = eval('np.' + window + '(window_len)') y = np.convolve(w/w.sum(), s, mode='valid') return y t = np.linspace(-4, 4, 100) x = np.sin(t) xn = x + np.random.randn(len(t))*0.1 y = smooth(x) ws = 31 plt.figure() plt.subplot(211) plt.plot(np.ones(ws)) for w in WINDOWS[1:]: eval('plt.plot(np.' + w + '(ws))') plt.axis([0, 30, 0, 1.1]) plt.legend(WINDOWS) plt.title('Smoothing windows') plt.subplot(212) plt.plot(x) plt.plot(xn) for w in WINDOWS: plt.plot(smooth(xn, 10, w)) l = ['original signal', 'signal with noise'] l.extend(WINDOWS) plt.legend(l) plt.title('Smoothed signal') plt.show()

中值過濾,即逐項的遍歷信號,並用相鄰信號項中的中值替代當前項

import matplotlib.pyplot as plt import numpy as np import scipy.signal as signal x = np.linspace(start=0, stop=1, num=51) x[3::5] = 1.5  # 從第4個 數開始,每一個5個數,將其值改成 1.5
 plt.plot(x, 'k.') plt.plot(signal.medfilt(volume=x, kernel_size=3), 'b.-')  # 在給定大小的鄰域內取中值替代數據值,在鄰域中沒有元素的位置補0
plt.plot(signal.medfilt(volume=x, kernel_size=15), 'r.-') plt.legend(['original signal', 'length 3', 'length 15']) plt.show()

相關文章
相關標籤/搜索