 clear
 T = 0 : 1/1E3 : 2; % 1 kHz sample freq for 1 sec
 N=length(T);
 Tp=1/3;
 D=0:Tp:2;   % pulse centers 3 Hz repetition freq
 % coherent pulse train
 Y = pulstran(T,D,'rectpuls',0.1).*cos(2*pi*20*T);
 M=length(D);   % first and last pulses are only half
 Np=N/M;  % number of time samples per pulse
 % noncoherent pulse train
 % build a pulse train with random start times for each pulse
 for m=1:M
 m1=(m-1)*Np+1;
 m2=m1+Np;
 P=(rand(1,1)-.5)*2*pi;   % random phase for this pulse
 tau=0.1;
 if m==1 | m==M, tau=0.1; end
 Z(m1:m2) = pulstran(T(m1:m2),D(m),'rectpuls',tau).*cos(2*pi*20*T(m1:m2)+P);
 end
 % continuous sinusoid
 X = cos(2*pi*20*T);
 figure(1)
 plot(T,Y,'b-x',T,Z,'r-',T,X,'g-'), axis([0,2,-2.1,2.1])
 legend('coherent','noncoherent','sinusoid')