Home > Net >  Generate a Rectangular Pulse in MATLAB
Generate a Rectangular Pulse in MATLAB

Time:05-18

I need to create a rectangular pulse with width = 7 and a range (-T/2, T/2) where T 59 msec.

I wrote this code but I'm not sure if that's correct.

w = 7;
T = 59;
t = -T/2:1:T/2;

rect = rectpuls(t, w);
plot(t, rect);

This code generates a rectangular pulse but I'm not sure if it's right. Also, I'm not quite sure what the t = -T/2:1:T/2; means. I mean the range is from -29.5 to 29.5 with step 1. When I set this to 0.1 or 0.01 my pulse is better. Why does this affect my output?

Note that the second thing I have to do is to create a periodic sequence of clock pulses. I don't know if this affects the way I must implement my initial rectangular pulse.

CodePudding user response:

When you increase the number of increments a numerical function (such as Matlab rectpuls) uses in its process of discretizing the continuous, you'll have as consequence that the accuracy of said function is going to improve, at the expense (in this case, negligible) of added computational cost. You are doing exactly this, when you discretize employing smaller time-steps, from 1 to 0.1 to 0.01.

To create a periodic sequence of identical rectangular pulses, you can call the function in a loop:

N = 1E6;
rect = zeros(N, size(t));
for i = 1:N
    t -= 200;
    rect[i] = rectpuls(t, w);
end

figure
for i = 1:N
    plot(t, rect[i,:]);
    hold on
end

The above should generate identical rectangular pulses every 200 ms, over a time length of 5000 s.

  • Related