38 Practical MATLAB® Applications for Engineers
plot(t,f3)
axis([-6 6 -0.5 4.5])
ylabel(‘ Amplitude [f3(t)]’);xlabel(‘ t (time)’)
title(‘3*Rectpuls(t,6) vs. t’)
subplot(2,2,4)
plot(t,f4)
axis([-6 6 -0.5 5.5])
title(‘4*Rectpuls(t,9) vs. t’)
ylabel(‘ Amplitude [f4(t)] ‘); xlabel(‘ t (time) ‘)The script fi le rect_pulses is executed and the results are shown in Figure 1.34.
R.1.115 The MATLAB function y = pulstran(t, d, ’f’) returns a symmetric train of continu-
ous or discrete functions ‘f ’ with d periods over the range defi ned by t.
R.1.116 A more general MATLAB function is [f, t] = gensig(‘type’, T, range, Ts) that returns
the periodic function f defi ned by type (sin, square, or pulse), over the range t, with a
sampling rate Ts.
R.1.117 For example, use the gensig command to create three cycles of a square periodic
wave, with period T = 3 and a sampling rate Ts = 0.1 (Figure 1.35).
MATLAB Solution
>> [squar,t] = gensig(‘square’,3,9,0.1);
>> plot(t,squar)
>> axis([0 9 0 1.2])
>> ylabel(‘Amplitude’);xlabel(‘time’)
>> title(‘Plot using [squar,t] = gensig(square,3,9,0.1)’)Rectpuls(t,1) versus t 2*Rectpuls(t,3) versus t3*Rectpuls(t,6) versus t 4*Rectpuls(t,9) versus tAmplitude [f2(t)]Amplitude [f4(t)]1.510.50−0.5
− 5 0 5t (time)t (time)Amplitude [f1(t)]2.5
2
1.5
1
0.5−0.55 4 3 2 1 00
t (time)− (^505)
− 5 0 5 − 5 0 5
t (time)
4
3
2
1
0
Amplitude [f3(t)]
FIGURE 1.34
Plots of the function rectpuls of R.1.114.