Search code examples
matlabamplitude

How to change the amplitude of pulsetrain of rectangular pulses in Matlab


I am plotting a pulsetrain of rectangular pulses.

pulse_periods = [0:128]*period; %128 pps 
%works for Ampl. default  = 1, 
r1 = pulstran(t,pulse_periods,'rectpuls', w); 

This gives a default amplitude of 1 for the rectangular pulses.

I need to change it to 0.5

I tried

    pulse_periods = [[0:128]*period;0.5 * [0:128]]' %128 pps 
    %does not work for Ampl. = 0.5, 
    r1 = pulstran(t,pulse_periods,'rectpuls', w); 

This is a modification of the periodic gaussian pulse example given in Matlab https://www.mathworks.com/help/signal/ref/pulstran.html?searchHighlight=pulstran

I am unable to change the amplitude for the required rectangular pulses .

What is the mistake I am doing ?


Solution

  • The second column of pulse_periods should be the amplitude of each pulse. In the example from the documentation, they wanted the pulse amplitude to change. If you want the pulse amplitude to stay at a constant 0.5, then you should do instead:

    pulse_periods = [(0:128)*period; 0.5 * ones(1,129)]';
    

    As part of a minimum working example:

    period = 1/128;
    pulse_periods = [(0:128)*period; 0.5 * ones(1,129)]';
    w = period * 0.5;
    t = linspace(0, 1, 2e3)';
    r1 = pulstran(t,pulse_periods,'rectpuls', w);
    plot(t,r1);
    

    Note that you could have also simply scaled the output from your "default amplitude of 1" case (i.e. r1 = r1 * 0.5);