How to Generate Square Wave and Sawtooth Wave Signals in MATLAB?

S
support · Jul 24, 2026 · 6 views
Question
How to create periodic square wave signals with custom frequency, amplitude, and duty cycle using MATLAB?
Expert Answer
Profile picture of John Michell
John Michell PhD Expert
Answered Aug 29, 2026

Use the square() function from Signal Processing Toolbox:

fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:1; % Time vector for 1 second
freq = 5; % 5 Hz Square wave
duty_cycle = 50; % 50% duty cycle

% Generate square wave (-1 to +1)
sq_wave = square(2*pi*freq*t, duty_cycle);

figure;
plot(t, sq_wave, 'LineWidth', 2);
grid on;
ylim([-1.5 1.5]);
title('5 Hz Square Wave Signal');
xlabel('Time (sec)'); ylabel('Amplitude');
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!