分享一个自己写的生成随机阶跃序列的代码

function [time_step, rand_se] = rand_step(tmin,tmax,dt,step_min,step_max,dt_min,dt_max)
% example: [time_step, rand_se] = rand_step(1,2000,0.2,2,12,8,30)
% example: [time_step, rand_se] = rand_step(0,2000,0.2,2,12,8,28)
% 作者:一一数二三 
% 时间:20190327
% tmax:序列终止的时间
% tmin:序列开始阶跃的时间,阶跃前输出为0,tmin也可以设置为0
% dt:时间步长
% step_min和step_max:阶跃的界限
% dt_min  和dt_max  :每一次阶跃持续的最小和最大时间
time_step = 0;
rand_se = [];
if tmin>0
    time_se = tmin/dt;
    time_step = [time_step time_step(end)+dt:dt:time_step(end)+time_se*dt];
    rand_se = [rand_se zeros(1,time_se+1)];
    while time_step(end)<tmax
        if (tmax-time_step(end))<=dt_max
            time_se = ceil((tmax - time_step(end))/dt);
        else
            time_se = dt_min/dt + ceil(((dt_max-dt_min)/dt)*rand);
        end
        time_step = [time_step time_step(end)+dt:dt:time_step(end)+time_se*dt];
        rand_se = [rand_se step_min + (step_max-step_min) * rand * ones(1,time_se)];
    end
end
while time_step(end)<tmax && tmin==0
    if (tmax-time_step(end))<=dt_max
        time_se = ceil((tmax - time_step(end))/dt);
    else
        time_se = dt_min/dt + ceil(((dt_max-dt_min)/dt)*rand);
    end
    if time_step == 0
        time_step = [time_step time_step(end)+dt:dt:time_step(end)+time_se*dt];
        rand_se = [rand_se step_min + (step_max-step_min) * rand * ones(1,time_se+1)];
    else
        time_step = [time_step time_step(end)+dt:dt:time_step(end)+time_se*dt];
        rand_se = [rand_se step_min + (step_max-step_min) * rand * ones(1,time_se)];
    end
end
figure()
plot(time_step,rand_se);xlim([0 tmax]);


这个代码可以控制阶跃开始时间、阶跃的幅值和每个阶跃时间步长的范围,生成的随机阶跃序列如下:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值