心音信号分割matlab代码
时间: 2023-07-26 12:30:05 浏览: 63
以下是一个基于MATLAB语言的心音信号分割代码示例:
```matlab
% 加载心音信号数据
signal_data = load('signal.txt');
fs = 1000; % 采样频率
% 心音信号的滤波
nyquist_freq = 0.5 * fs;
lowcut_freq = 20 / nyquist_freq;
highcut_freq = 2000 / nyquist_freq;
[b, a] = butter(4, [lowcut_freq, highcut_freq], 'band');
filtered_signal = filtfilt(b, a, signal_data);
% 心音信号的检测
threshold = 0.25 * max(filtered_signal);
peak_indices = findpeaks(filtered_signal, 'MinPeakHeight', threshold);
% 心音信号的分割
s1_indices = [];
s2_indices = [];
for i = 1:length(peak_indices)-1
peak_diff = peak_indices(i+1) - peak_indices(i);
if peak_diff > 0.2 * fs && peak_diff < 0.4 * fs
s1_indices = [s1_indices, peak_indices(i)];
s2_indices = [s2_indices, peak_indices(i+1)];
end
end
s1_signal = filtered_signal(s1_indices(1):s1_indices(end));
s2_signal = filtered_signal(s2_indices(1):s2_indices(end));
% 可视化结果
figure;
plot(signal_data)
hold on
plot(filtered_signal, 'r')
plot(peak_indices, filtered_signal(peak_indices), 'go')
plot(s1_signal, 'b')
plot(s2_signal, 'g')
hold off
```
上述代码中,`butter`函数实现了心音信号的带通滤波,`findpeaks`函数实现了心音信号的检测,`s1_indices`和`s2_indices`分别存储了检测结果中的S1和S2位置,最后使用MATLAB内置函数`plot`函数对结果进行了可视化展示。