Search code examples
matlabsignal-processing

Generate a Rectangular Pulse in MATLAB


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.


Solution

  • 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:

    w = 7;
    T = 59;
    t = -T/2:1:T/2;
    t_size = size(t);
    N = 10;
    rect = zeros(N, t_size(2));
    interval = 20;
    
    figure
    plot(t, rectpuls(t, w));
    xlim([-20 (N + 1)*interval]);
    ylim([0 1.1]);
    hold on
    for i = 1:N
        t = (-T/2 + i*interval): 1 :(T/2 + i*interval);
        rect(i,:) = rectpuls(t - i * interval, w);
        plot(t, rect(i,:));
        hold on
    end
    

    The above should generate identical rectangular pulses every interval = 20 ms, over a time length of interval * (N + 1) = 220 ms.