You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MATLAB中如何模拟正弦自由呼吸与不规则呼吸曲线?

MATLAB 自由呼吸与不规则呼吸波形生成思路

自由呼吸:分段随机参数的连续正弦波实现

你提到的迭代法思路完全可行,核心要解决波形连续性问题,同时为每个呼吸周期的周期、幅度加入独立随机性。具体实现步骤如下:

  1. 基础参数设定

    • 定义总时长 total_time、采样率 fs,计算总采样点数 n_samples = total_time * fs
    • 设定随机范围:比如常规15次/分钟对应周期4秒,自由呼吸可将周期设为3-5秒随机;幅度设为基准幅度的0.8-1.2倍随机(可自定义范围)
  2. 迭代生成连续波形
    每次生成一段正弦波时,以上一段的结束相位作为下一段的起始相位,避免波形跳变。示例代码:

    fs = 100; % 采样率100Hz
    total_time = 60; % 总时长60秒
    n_samples = total_time * fs;
    breath_wave = zeros(1, n_samples);
    
    current_time = 0;
    current_phase = 0; % 初始相位
    base_amp = 1; % 基准幅度
    
    while current_time < total_time
        % 随机生成当前呼吸周期(秒)和幅度
        current_period = 3 + 2*rand(); % 3-5秒随机
        current_amp = base_amp * (0.8 + 0.4*rand()); % 0.8-1.2倍基准幅度
        
        % 计算当前段的采样点数,避免超出总时长
        segment_samples = round(current_period * fs);
        if current_time + current_period > total_time
            segment_samples = n_samples - current_time * fs;
            current_period = segment_samples / fs;
        end
        
        % 生成当前段正弦波,起始相位衔接上一段
        t_segment = linspace(0, current_period, segment_samples);
        segment_wave = current_amp * sin(2*pi*t_segment/current_period + current_phase);
        
        % 写入波形数组
        start_idx = round(current_time * fs) + 1;
        end_idx = start_idx + segment_samples - 1;
        breath_wave(start_idx:end_idx) = segment_wave;
        
        % 更新相位(取模2π避免数值过大)和时间
        current_phase = mod(current_phase + 2*pi, 2*pi);
        current_time = current_time + current_period;
    end
    
    % 绘图查看
    t = linspace(0, total_time, n_samples);
    plot(t, breath_wave);
    xlabel('时间 (s)');
    ylabel('呼吸幅度');
    title('自由呼吸波形');
    

不规则呼吸:事件驱动的特殊场景叠加

不规则呼吸可以在自由呼吸的基础上,随机触发特殊事件(咳嗽、屏息),事件的类型、触发时机、持续时间都加入随机性,比卷积方法更灵活可控。

1. 屏息事件实现

屏息时呼吸幅度保持不变,持续一段随机时间:

% 随机触发1-3次屏息事件
n_hold_events = randi([1,3]);
for i = 1:n_hold_events
    % 随机选择触发时刻(避开首尾5秒)
    hold_start_time = 5 + (total_time - 10)*rand();
    hold_duration = 2 + 3*rand(); % 2-5秒屏息
    hold_end_time = hold_start_time + hold_duration;
    if hold_end_time > total_time - 5
        hold_end_time = total_time - 5;
        hold_duration = hold_end_time - hold_start_time;
    end
    
    % 替换对应时间段的波形为屏息起始幅度
    start_idx = round(hold_start_time * fs) + 1;
    end_idx = round(hold_end_time * fs);
    hold_amp = breath_wave(start_idx);
    breath_wave(start_idx:end_idx) = hold_amp;
end

2. 咳嗽事件实现

咳嗽用短时间高幅度高频正弦波模拟,持续0.2-0.5秒,幅度为基准的2-3倍:

% 随机触发1-2次咳嗽事件
n_cough_events = randi([1,2]);
base_cough_amp = base_amp * 2.5; % 咳嗽幅度为基准的2.5倍
cough_period = 0.1; % 咳嗽高频周期0.1秒
for i = 1:n_cough_events
    cough_start_time = 5 + (total_time - 10)*rand();
    cough_duration = 0.2 + 0.3*rand(); % 0.2-0.5秒咳嗽
    cough_end_time = cough_start_time + cough_duration;
    if cough_end_time > total_time - 5
        cough_end_time = total_time - 5;
        cough_duration = cough_end_time - cough_start_time;
    end
    
    % 生成咳嗽波形并替换原有波形
    start_idx = round(cough_start_time * fs) + 1;
    end_idx = round(cough_end_time * fs);
    segment_samples = end_idx - start_idx + 1;
    t_cough = linspace(0, cough_duration, segment_samples);
    cough_wave = base_cough_amp * (0.9 + 0.2*rand()) * sin(2*pi*t_cough/cough_period);
    breath_wave(start_idx:end_idx) = cough_wave;
end

% 绘制最终不规则呼吸波形
figure;
plot(t, breath_wave);
xlabel('时间 (s)');
ylabel('呼吸幅度');
title('不规则呼吸波形(含屏息、咳嗽)');

额外优化点

  • 给事件触发加入概率权重,比如每分钟触发咳嗽的概率20%、屏息15%,更贴近随机场景
  • 咳嗽波形可替换为三角波或方波,根据需求调整形态
  • 若需要更混沌的效果,可扩大自由呼吸周期、幅度的随机范围,或加入缓慢趋势变化(如一段时间内呼吸逐渐变快/变慢)

内容的提问来源于stack exchange,提问作者Aegis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 06:15:39