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


平滑數據噪聲的一個簡單朴素的做法是,對窗口(樣本)求平均,然后僅僅繪制出給定窗口的平均值,而不是所有的數據點。

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()

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

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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM