square
result = square(freq, fs, n, [duty=])
Input: freq, fs, n, duty · Output: result
What it is
A bipolar (±1) periodic pulse train. freq (number, Hz) is the repetition rate; fs (number, Hz) is the sample rate the train is generated at; n (number) is how many samples to produce; duty= (number, 0–1, default 0.5) is the fraction of each period spent at +1 rather than -1.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = square(freq, fs, n, [duty=]) | freq numberfreq (number, Hz) is the repetition rate.fs numberfs (number, Hz) is the sample rate the train is generated at.n numbern (number) is how many samples to produce.duty number, optionalduty= (number, 0–1, default 0.5) is the fraction of each period spent at +1 rather than -1. | result Signal | Returns a Signal of length n. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
a = square(100, 1000, 8, duty=0.25)
b = signals.square(100, 1000, 8, duty=0.25)
print("a = {a}")
print("b = {b}")
print("identical = {a == b}")
from tour
a = square(100, 1000, 20, duty=0.25)
b = signals.square(100, 1000, 20, duty=0.25) # identical to `a`
print("square: {a}")
print("signals.square matches: {max(abs(a - b)) < 1e-12}")
square: signal(Fs=1000) [1, 1, 1, -1, -1, -1, -1, -1, ... (20 elements)] signals.square matches: true
from signal-processing
s = sine(1 kHz, 48 kHz, 4800) # a single tone
c = chirp(20, 20000, 48 kHz, 48000) # linear sweep, 20Hz to 20kHz
lc = chirp(20, 20000, 48 kHz, 48000, method="logarithmic")
sq = square(100, 1000, 1000, duty=0.3) # bipolar (+-1) pulse train
p = pwm(modulator, 5000, 48000) # natural-sampling PWM against
# a triangle carrier (duty
# follows the modulator's value)
from numerics-dsp
How it works
square(freq, fs, n, [duty=]) — a bipolar (±1) periodic pulse train ("binary pulses"), duty (default 0.5) the fraction of each period spent at +1.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.