from wfdb import *import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport find_filter_coefficients as find_fcimport find_wavelet_coefficients as fwcfrom scipy.signal import butter, filtfilt, resampledef read_data(dir, record_number, ch=0):"""Read ECG signals and annotationsArgs:dir: Path to the datasetrecord_number: Record Number or Namech: channel numberReturns:ecg_record, ecg_ann : ECG signal and it's annotations."""record_name = dir + str(record_number)ecg_record = rdrecord(record_name, channels=[ch])ecg_ann = rdann(record_name, extension='atr')return ecg_record, ecg_anndef lowpass(signal, cutoff, fs, order):nyquist = 0.5 * fsnormal_cutoff = cutoff / nyquistb, a = butter(order, normal_cutoff, btype='low', output='ba', analog=False)[:2]y = filtfilt(b, a, signal)return np.array(y)def highpass(signal, cutoff, fs, order):nyquist = 0.5 * fsnormal_cutoff = cutoff / nyquistb, a = butter(order, normal_cutoff, btype='high', output='ba', analog=False)[:2]y = filtfilt(b, a, signal)return np.array(y)def filter_signal(signal, low_cut, high_cut, fs):"""Find the signal without frequency components higher than 40Hz and less than 0.5Hz.Args:signal: Raw ECG Signalfs: Sampling rate of the given raw ECG signalReturns:Signal without frequency components higher than 40Hz and less than 0.5Hz as an array"""filtered_signal1 = lowpass(signal, cutoff=low_cut, fs=fs, order=5)filtered_signal2 = highpass(signal, cutoff=high_cut, fs=fs, order=5)filtered_ecg = signal - filtered_signal1 - filtered_signal2 # Subtract low frequency components and high# frequency components from the original signalreturn np.array(filtered_ecg)def load_mit_datset(records):"""Prepare the dataset for the classification task"""fs = 360 # sampling rate of the MIT-BIH recordsdirectory = "dataset//mit-bih-arrhythmia-database-1.0.0//" # path to the databasedataset = pd.DataFrameX = y = for rec_no in records:try:if rec_no == 114: # MLII is channel 1 not 0ecg_record, ecg_annotations = read_data(directory, rec_no, ch=1)else:ecg_record, ecg_annotations = read_data(directory, rec_no)except FileNotFoundError:print(f"Record {rec_no} not found")continuesamples = ecg_annotations.sampleecg_signal = ecg_record.p_signal[:, 0] # only take the first channel ( lead 1)ecg_signal_filtered = filter_signal(ecg_signal, 0.5, 40, fs) # filter the signalsymbols = ecg_annotations.symbolplt.figure(figsize=(20, 5)) # Set the figure sizeplt.plot(ecg_signal[:5000], label="Original ECG Signal")plt.plot(ecg_signal_filtered[:5000], label="Filtered ECG Signal")plt.xlabel("Samples")plt.ylabel("Amplitude")plt.title("ECG Signal and Filtered Signal")plt.legendplt.showload_mit_datset([106])摘要:from wfdb import *import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport find_filter_coefficients as find_fcim
知乎学术咨询:
https://www.zhihu.com/consult/people/792359672131756032?isMe=1担任《Mechanical System and Signal Processing》《中国电机工程学报》等期刊审稿专家,擅长领域:信号滤波/降噪,机器学习/深度学习,时间序列预分析/预测,设备故障诊断/缺陷检测/异常检测。
分割线分割线分割线
基于小波分析的时间序列降噪(Python,ipynb文件)
完整代码:
时间序列的最大重叠离散小波分解(Python)
完整代码:
基于连续小波变换的信号滤波方法(Python,ipynb文件)
完整代码:
信号的时域、频域和时频域特征提取(Python)
完整代码:
不同小波族的优缺点
完整代码:
同步压缩变换的一些应用(时频分析,盲源分离等,MATLAB)
完整代码和数据可通过知乎学术咨询获得(注意:一次知乎学术付费咨询只能获得一套代码+学术指导)
引力波信号的同步压缩变换,高阶同步压缩变换,脊线提取与信号重建(MATLAB R2021B)
基于同步压缩变换(重分配算子机制)的非平稳信号瞬时频率估计(MATLAB R2018A)
基于优化Morlet小波的一维信号瞬态特征提取方法(MATLAB )
程序运行环境为MATLAB R2018A,利用优化Morlet小波对一维信号进行瞬态特征提取。程序测试了模拟信号,地震信号,发动机销子活塞故障振动信号,发动机气门正常振动信号,发动机排气门故障振动信号,结果如下。
完整代码可通过知乎付费咨询获得(注意:一次知乎学术付费咨询只能获得一套代码+学术指导)
MATLAB环境下基于高分辨时频分析与阈值法的一维信号降噪
来源:科技浪潮之巅