3.1 Generate and plot one pulse signal of different width and amplitude

Problem: Generate one signal of some width and height.

Mathematica

Remove["Global`*"] 
f[x_,w_,h_]:=If[0<=x<w, 
      h*If[x-IntegerPart[x]<=0.5, 
      SquareWave[x], 
      -SquareWave[x] 
      ], 
      0] 
w = {0.5,1,2};   (*pulse width *) 
h = {1,1.5,0.4}; (*pulse height *) 
plots = MapThread[Plot[ 
          f[x,#1,#2],{x,-2,2.5}, 
            PlotRange->{{-1,2.5},{-0.3,2.5}}, 
            Frame->True, 
            Axes->False, 
            GridLines->Automatic, 
            GridLinesStyle->Dashed, 
            PlotStyle->{Thick,Red}, 
            FrameLabel->{{"y(t)",None}, 
    {"t","pulse of "<>ToString[#1]<> 
    " sec width and amplitude"<> 
    ToString[#2]}}, 
            ImageSize -> 160, 
            AspectRatio->1]&,{w,h}]; 
 
Grid[{plots},Frame->All]
 

pict

 

Matlab

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

pict