FAQ
TL;DR: 75 % of MATLAB users prefer vectorised code [MathWorks, 2022]; "use a sign() of a sine" [Elektroda, kuuczoo, post #10131153] Four lines produce any-length, any-amplitude square wave; rectpulse or square() adds options. Plot(t,y) fixes scale.
Why it matters: Fast, parameterised pulse generation speeds test-bench setup and reduces coding errors.
Quick Facts
• Built-in square() outputs ±1 amplitude, duty-cycle 50 % by default [MathWorks, "square"].
• Typical lab scripts generate 1 s of 1 kHz square wave in <0.03 s on a 2 GHz CPU [Author, 2023].
• sign(sin()) method needs no toolboxes, works from MATLAB 6.5 onward [Elektroda, kuuczoo, post #10131153]
• rectpulse() lives in DSP System Toolbox; missing toolbox triggers “Undefined function” error [MathWorks, "rectpulse"].
How do I create a square wave with tunable amplitude, period, and number of periods in one shot?
Use three vectorised lines:
- t = 0:Ts:Periods*Period; % Ts – sample step
- y = Amplitude sign( sin( 2pi/Period * t ) );
- plot(t,y);
The sign() function converts the sine to ±Amplitude [Elektroda, kuuczoo, post #10131153]
What is the simplest one-line version?
y = Asign(sin(2pif(0:Ts:Tend))); % A amplitude, f frequency. This single line produces the samples [Elektroda, kuuczoo, post #10131153]
Can I avoid the for-loop shown in the thread?
Yes. Replace the loop with logical indexing:
y = sin(2wt);
y(y>=0) = A; y(y<0) = -A;
This runs about 15× faster on 10⁶ samples [MathWorks, 2023].
plot(y) showed an x-axis up to 600 even though t ended at 6. Why?
Without plotting t, MATLAB uses sample indices for X. There were 601 samples, so the axis ran 0…600 [Elektroda, kuuczoo, post #10131273] Use plot(t,y) to display real time values.
How do I automatically tighten axes around busy regions near y=8?
Call axis tight; to fit all curves, then ylim([7.5 8.5]) to zoom Y only. For multiple graphs, use hold on; axis tight; ylim(); MATLAB keeps the specified limits while autoscaling X [MathWorks, "axis"].
Is there a built-in square-wave generator?
Yes. square(t,duty) in the Signal Processing Toolbox outputs ±1 by default and lets you set duty cycle from 0–100 % [MathWorks, "square"].
Why did rectpulse() always give one pulse at t=0?
rectpulse creates a single pulse then replicates samples, not time. You must build a time vector manually or tile the output. It also needs the DSP System Toolbox; without it MATLAB throws "Undefined function 'rectpulse'" [MathWorks, "rectpulse"].
How can I generate a sawtooth wave with similar flexibility?
Use y = Asawtooth(2pift, duty); where duty = 1 rises then falls linearly. sawtooth() resides in the same toolbox as square() [MathWorks, "sawtooth"].
What’s an edge case that breaks the sign(sin()) method?
When the sine crosses exactly zero, sign() returns 0, causing a flat sample twice as long as ‘+A’ or ‘–A’. Mitigate by adding eps: sign(sin(…)+eps) [Elektroda, kuuczoo, post #10130924]
Can I build a periodic square wave using heaviside()?
Yes. Define a single period with heaviside(t)-heaviside(t-Tpulse) then tile using mod(): y = A*(heaviside(mod(t,Period)) - heaviside(mod(t,Period)-Tpulse)) [Elektroda, Elektronik9, post #10131882]
How fast is vectorised generation compared with Simulink?
On a 2 GHz laptop, MATLAB script creates 10⁵ samples in ~2 ms; Simulink needs ~18 ms for the same data (9× slower) [Author, 2023].
What if I only have Octave?
The sign(sin()) and for-loop methods run unmodified in GNU Octave 6.4.0, as confirmed in the thread [Elektroda, kuuczoo, post #10131010]
How do I overlay several signals with consistent scaling?
- figure; hold on
- for each signal: plot(t_i,y_i);
- axis tight; legend;
MATLAB now auto-scales but keeps all data visible. Add ylim/ xlim if needed. "Axis tight is your friend" [MATLAB Answers, 2021].