pythonics1
pythonics1

Reputation: 39

Series of rectangular pulses with increasing pulse length in MATLAB

I'm trying to define a function which is a series of rectangular pulses, where the length of a pulse increases by a constant with each repeat. The distance between the pulses should remain the same. How would I do this in MATLAB?

In the Sketch d is the gap between the pulses, a is the starting pulse length, and l is the constant by which the length increases.

Upvotes: 0

Views: 333

Answers (1)

MichaelTr7
MichaelTr7

Reputation: 4767

Iteratively Concatenating Pulses to Generate Variable Pulse Train

The method below uses an iterative approach to create a pulse train signal. On each iteration, a cycle of the signal is concatenated to an array name Full_Signal in this case. Full_Signal will store the entire signal that is to be plotted. A cycle in this context is considered to be the high pulse a+l followed by an off-state d. The sizing and properties of the signal can be adjusted by using the variables a,l,d,Max_Time and Number_Of_Cycles. Some things to note is ones(m,n) creates an array of ones with m rows and n columns. Similiar zeros(m,n) follows the same structure and generates an array of zeros. Parameters a,l and d will be proportional to the time vector specifications.

Variable Pulse Train Plot

%Clearing the workspace%
clear;
clc;
clf;

%*********************************************************%
%ADJUSTABLE PARAMETERS%
%*********************************************************%
a = 15;
l = 1;
d = 40;
Max_Time = 100;
Number_Of_Cycles = 10;
%*********************************************************%

%Will hold the entire signal%
Full_Signal = [];

%Interatively concatenating cycles%
Cycle = 1;
while(Cycle < Number_Of_Cycles)

    Duty_High = a + Cycle*(Cycle - 1);
    Signal_Portion = [ones(1,Duty_High) zeros(1,d)];
    Full_Signal = [Full_Signal Signal_Portion];
    Cycle = Cycle + 1;

end

%Creating time scale/x-axis%
t = linspace(0,Max_Time,length(Full_Signal));

%Plotting the signal%
figure('Position', [300 400 500 100])
plot(t,Full_Signal);
axis([0 Max_Time 0 1.1]);
title("Variable Pulse Train");
xlabel("Time"); ylabel("Amplitude"); 

Extension: Using Interpolation to Set the Plotting Density and Units Scaled to the Time Axis

Alternatively, if you wish to have the variable a,l and d be in units of time then interpolation can be used to set the Plotting_Density and retain a pretty clear plot. Interpolation using the function interp1 to create allows you to create a time vector of varying precision and map the signal to this new interpolated time vector accordingly.

Interpolated Result

%Clearing the workspace%
clear;
clc;
clf;

%*********************************************************%
%ADJUSTABLE PARAMETERS%
%*********************************************************%
a = 15;
l = 1;
d = 40;
Max_Time = 1000;
Number_Of_Cycles = 10;
Plotting_Density = 0.0001;
%*********************************************************%

Number_Of_Cycles = Number_Of_Cycles + 1;
%Will hold the entire signal%
Full_Signal = [];

%Interatively concatenating cycles%
Cycle = 1;
while(Cycle < Number_Of_Cycles)

    Duty_High = a + Cycle*(Cycle - 1);
    Signal_Portion = [ones(1,Duty_High) zeros(1,d)];
    Full_Signal = [Full_Signal Signal_Portion];
    Cycle = Cycle + 1;

end

%Creating time scale/x-axis%
t = (0:1:length(Full_Signal)-1);
Interpolated_Time_Vector = (0:Plotting_Density:length(Full_Signal)-1);
Interpolated_Signal = interp1(t,Full_Signal,Interpolated_Time_Vector);

close all;
%Plotting the signal%
figure('Position', [300 400 500 100])
plot(Interpolated_Time_Vector,Interpolated_Signal);
axis([0 max(Interpolated_Time_Vector) 0 1.1]);
title("Variable Pulse Train");
xlabel("Time (s)"); ylabel("Amplitude"); 

Ran using MATLAB R2019b

Upvotes: 1

Related Questions