Qu

pwm

Builtin index · Plotting

result = pwm(modulator, carrier_freq, fs, [carrier=])

Input: modulator, carrier_freq, fs, carrier · Output: result

What it is

Natural-sampling pulse-width modulation: modulator is a Vec of numbers (any signal, conventionally in [-1, 1]), carrier_freq and fs are numbers (Hz). Emits +1 where the modulator exceeds a carrier wave oscillating in [-1, 1] at carrier_freq, else -1. carrier= (string, "triangle" default or "sawtooth"/"saw") picks the carrier shape.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = pwm(modulator, carrier_freq, fs, [carrier=])
modulator Vec or numberNatural-sampling pulse-width modulation: modulator is a Vec of numbers (any signal, conventionally in [-1, 1]), carrier_freq and fs are numbers (Hz).
carrier_freq Vec or numberNatural-sampling pulse-width modulation: modulator is a Vec of numbers (any signal, conventionally in [-1, 1]), carrier_freq and fs are numbers (Hz).
fs Vec or numberNatural-sampling pulse-width modulation: modulator is a Vec of numbers (any signal, conventionally in [-1, 1]), carrier_freq and fs are numbers (Hz).
carrier string, optionalcarrier= (string, "triangle" default or "sawtooth"/"saw") picks the carrier shape.
result Signal
Returns a Signal the same length as modulator.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

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

pwm(modulator, carrier_freq, fs, [carrier=]) — natural- sampling pulse-width modulation: +1 where modulator (any signal, conventionally in [-1, 1]) exceeds a carrier wave oscillating in [-1, 1] at carrier_freq, else -1. carrier="triangle" (default, the standard choice — see pwm_wave's own doc comment for why) or "sawtooth".

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.