在Python中實(shí)現(xiàn)數(shù)據(jù)降噪可以使用各種方法和庫,以下是幾種常用的方法:
convolve
函數(shù)實(shí)現(xiàn)移動(dòng)平均濾波。import numpy as np
def moving_average(data, window_size):
window = np.ones(window_size) / window_size
return np.convolve(data, window, mode='same')
medfilt
函數(shù)實(shí)現(xiàn)中值濾波。from scipy.signal import medfilt
def median_filter(data, window_size):
return medfilt(data, kernel_size=window_size)
import pywt
def wavelet_denoise(data, wavelet='db4', level=1):
coeffs = pywt.wavedec(data, wavelet, level=level)
coeffs[1:] = (pywt.threshold(coeff, value=0.5*max(coeff)) for coeff in coeffs[1:])
return pywt.waverec(coeffs, wavelet)
以上是三種常見的數(shù)據(jù)降噪方法的示例代碼,具體的選擇和調(diào)整參數(shù)需要根據(jù)數(shù)據(jù)的特性和需求進(jìn)行調(diào)整。