邯鄲市建設(shè)局網(wǎng)站材料下載入口北京seo方法
目錄
- detrend函數(shù)去除基線
- 多項(xiàng)式擬合原函數(shù)
- BEADS 基線處理
- 小波算法
- 經(jīng)驗(yàn)?zāi)B(tài)分解(EMD)
- 參考
detrend函數(shù)去除基線
detrend函數(shù)只能用于去除線性趨勢(shì),對(duì)于非線性的無(wú)能為力。
函數(shù)表達(dá)式:y = scipy.signal.detrend(x): 從信號(hào)中刪除線性趨勢(shì):
x:含有基線干擾的信號(hào);y:去除基線干擾后的信號(hào)。
detrend去基線代碼顯示:
from scipy import signal
import matplotlib.pyplot as plt
import numpy as npt = np.linspace(0, 5, 100)
# normal是產(chǎn)生一個(gè)高斯分布
x = t + np.random.normal(size=100)
plt.subplot(2, 1, 1)
plt.plot(t, x, linewidth=3)
plt.subplot(2, 1, 2)
plt.plot(t, signal.detrend(x), linewidth=3)
plt.show()
結(jié)果展示:
通過(guò)上圖可以看到detrend去線性趨勢(shì)效果很不錯(cuò)。
多項(xiàng)式擬合原函數(shù)
很容易理解,就是通過(guò)多項(xiàng)式擬合一個(gè)新的曲線,使擬合出來(lái)的曲線與原圖像盡可能接近,同時(shí)又能去除圖像中的噪聲和基線等多余的因素。
代碼顯示為:
import os
import matplotlib.pyplot as plt
import scipy.signal
import numpy as npdef main():# 項(xiàng)目目錄dir = "D:\\a_user_file\\8_data"filename = 's1_run.csv'path = os.path.join(dir, filename)with open(path, "r") as fname:data = fname.read()lines = data.split("\n")raw_data = []for i in range(len(lines)):line_i = lines[i].split(",")raw_data.append(int(line_i[4]))sig = raw_datatmp_smooth1 = scipy.signal.savgol_filter(sig, 53, 9)tmp_smooth2 = scipy.signal.savgol_filter(sig, 53, 3)plt.subplot(3,1,1)plt.plot(sig)plt.subplot(3,1,2)plt.plot(tmp_smooth1 * 0.5, label='mic'+ '擬合曲線-21', color='red')plt.subplot(3,1,3)plt.plot(tmp_smooth2 * 0.5, label='mic'+ '擬合曲線-53', color='green')plt.show()main()
顯示結(jié)果如下:
BEADS 基線處理
詳細(xì)內(nèi)容可參考:
https://ww2.mathworks.cn/matlabcentral/fileexchange/49974-beads-baseline-estimation-and-denoising-with-sparsity?s_tid=AO_FX_info
小波算法
小波算法去噪和去基線是先用濾波器對(duì)原始信號(hào)進(jìn)行分解,經(jīng)過(guò)下采樣得到分解的高頻系數(shù)D(細(xì)節(jié)部分)和低頻系數(shù)A(近似部分),多層分解只需要對(duì)上一層分解出來(lái)的低頻分量繼續(xù)分解即可。這個(gè)過(guò)程就是小波分解。
從分解的最底層往上重構(gòu)出信號(hào),首先是上采樣,一般采用隔值插零的方法,即增加數(shù)據(jù)量來(lái)達(dá)到與原始信號(hào)長(zhǎng)度相同的數(shù)據(jù),然后分別通過(guò)重構(gòu)的高通濾波器g和低通濾波器h,最終重構(gòu)出原始信號(hào),如果代碼編寫(xiě)的沒(méi)問(wèn)題,那么重構(gòu)出的信號(hào)與原始信號(hào)完全一致。
而小波變換去噪的過(guò)程就是在分解后的各層系數(shù)中找出噪聲所在的層,對(duì)該層的低頻系數(shù)或者高頻系數(shù)進(jìn)行處理,比如軟硬閾值處理,處理后再經(jīng)過(guò)重構(gòu),即可重構(gòu)出去除噪聲的信號(hào)。
import numpy as np
import matplotlib.pyplot as plt
import pywt
import osdef signal():# 項(xiàng)目目錄dir = "D:\\a_user_file\\8_data"filename = '1.csv'path = os.path.join(dir, filename)with open(path, "r") as fname:data = fname.read()lines = data.split("\n")raw_data = []for i in range(len(lines)):line_i = lines[i].split(",")raw_data.append(int(line_i[0]))return raw_datadata = signal()
x = range(0, len(data))
w = pywt.Wavelet('db8') # 選用Daubechies8小波
maxlev = pywt.dwt_max_level(len(data), w.dec_len)
print("maximum level is " + str(maxlev))
threshold = 0.5 # Threshold for filtering
# Decompose into wavelet components, to the level selected:
coeffs = pywt.wavedec(data, 'db8', level=maxlev) # 將信號(hào)進(jìn)行小波分解
for i in range(1, len(coeffs)):coeffs[i] = pywt.threshold(coeffs[i], threshold*max(coeffs[i])) # 將噪聲濾波
datarec = pywt.waverec(coeffs, 'db8')plt.subplot(2,1,1)
plt.plot(data, color="black", linewidth=2.0, linestyle="solid")
plt.subplot(2,1,2)
plt.plot(datarec, color="red", linewidth=2.0, linestyle="solid")
plt.show()
用小波變換去噪的關(guān)鍵是找到對(duì)應(yīng)噪聲、基線漂移所在的頻率段,去掉對(duì)應(yīng)的頻率段,就可以生成新的去噪去基線信號(hào)了。
經(jīng)驗(yàn)?zāi)B(tài)分解(EMD)
EMD方法認(rèn)為任何信號(hào)都可以分解為若干個(gè)不同的本征模態(tài)函數(shù),和一個(gè)殘余量穩(wěn)態(tài)量。其中各個(gè)本征模態(tài)函數(shù)反映了信號(hào)的局部特性,殘余量反映了信號(hào)的趨勢(shì)或均值。EMD法采用“篩”選的方法從原始信號(hào)中將殘余量分離出來(lái)。
參考
https://ww2.mathworks.cn/matlabcentral/fileexchange/49974-beads-baseline-estimation-and-denoising-with-sparsity?s_tid=AO_FX_info
https://blog.csdn.net/qq_41620350/article/details/115981740
https://blog.csdn.net/u010565765/article/details/69397415