Matlab Code for Rectangular Wave

4 views (last 30 days)
nisha bhatt
nisha bhatt on 2 Apr 2019
Answered: Akanksha on 28 Mar 2025
I want to generate a rectangular wave of 5 Amplitude with duty cylce 70% and for remaining 30% amplitude is zero. The wave should last for 40 seconds like this and then for 60 seconds it becomes zero. Can someone guide me how to do it?

Answers (1)

Akanksha
Akanksha on 28 Mar 2025
I hope the following code helps :
% Parameters
amplitude = 5; % Amplitude of the wave
duty_cycle = 0.7; % Duty cycle (70%)
period = 1; % Period of the rectangular wave
total_time = 100; % Total duration (40s active + 60s zero)
active_time = 40; % Duration of active wave
inactive_time = 60; % Duration of zero wave
% Time vector
dt = 0.01; % Time step
t = 0:dt:total_time; % Time vector from 0 to total_time
% Initialize wave
wave = zeros(size(t));
% Generate rectangular wave for the active period
for i = 1:length(t)
if t(i) <= active_time
% Determine the phase within the current period
phase = mod(t(i), period);
% Set amplitude based on duty cycle
if phase < duty_cycle * period
wave(i) = amplitude;
else
wave(i) = 0;
end
else
% Set wave to zero for inactive period
wave(i) = 0;
end
end
% Plot the wave
figure;
plot(t, wave, 'LineWidth', 1.5);
xlabel('Time (s)');
ylabel('Amplitude');
title('Rectangular Wave with 70% Duty Cycle and Zero Period');
grid on;
axis([0 total_time -1 amplitude+1]);
This is what I got in the output with the above code :
Hope this helps with the query.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!