3.2 Generate and plot train of pulses of different width and amplitude

Problem: Generate a pulse train of pulses of certain width and height.

Mathematica

Remove["Global`*"] 
 
w = {0.25,0.125}; 
h = {1,1.5}; 
plots = MapThread[Plot[#2 SquareWave[#1 x], 
         {x,-10,10}, 
  PlotRange->{All,{-2,2}}, 
  Frame->True, 
  Axes->False, 
  PlotStyle->{Thick,Red}, 
  FrameLabel->{{"y(t)",None}, 
   {"t","pulse of "<>ToString[1/(2*#1)]<> 
   " sec width and amplitude"<>ToString[#2]}}, 
  ImageSize->200, 
  AspectRatio->1, 
  ExclusionsStyle->Dotted]&,{w,h}]; 
 
Grid[{plots},Frame->All]
 

pict

 

Matlab

clear all; close all; 
f = [1 0.5]; 
h = [1 1.5]; 
t = -4:0.01:4; 
 
figure; 
 
for i=1:2 
  y = h(i)*square(2*pi*f(i)*t); 
  subplot(2,1,i); 
  plot(t,y,'r','LineWidth',3); 
  grid on; 
  xlim([-2 2.5]); 
  ylim([-2 2.5]); 
  title(sprintf... 
 ('pulse of %2.1f sec width and amplitude=%2.1f',.. 
   1/(2*f(i)),h(i))); 
end
 

pict