在Python中實現數據降噪可以使用各種方法和庫,以下是幾種常用的方法:
convolve
函數實現移動平均濾波。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
函數實現中值濾波。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)
以上是三種常見的數據降噪方法的示例代碼,具體的選擇和調整參數需要根據數據的特性和需求進行調整。