分享一个自己写的生成随机阶跃序列的代码
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]);
这个代码可以控制阶跃开始时间、阶跃的幅值和每个阶跃时间步长的范围,生成的随机阶跃序列如下: