📊

Signal Generator

Sources
Run the live example Free · no sign-up · runs in your browser

The SignalGenerator block produces standard time-varying periodic waveforms for simulation excitation, dynamic frequency-response characterization, and hardware-in-the-loop (HIL) testing. It can generate sinusoidal, cosine, bipolar square, linear sawtooth, and symmetric triangular waveforms. The block features intuitive engineering parameterization—peak amplitude, frequency in Hertz, DC offset, initial phase in degrees, and square-wave duty cycle—along with an optional external reset trigger port.

Mathematical Model

Let ff be the frequency in Hertz, ϕ=phase⋅π180\phi = \text{phase} \cdot \frac{\pi}{180} the initial phase angle in radians, and tt the current continuous simulation time. The total phase angle θ(t)\theta(t) is defined as:

θ(t)=2πft+ϕ\theta(t) = 2\pi f t + \phi

The normalized cycle position n(t)∈[0,1)n(t) \in [0, 1) is evaluated via modulo arithmetic:

n(t)=(θ(t)2π) mod 1n(t) = \left( \frac{\theta(t)}{2\pi} \right) \bmod 1

The output signal y(t)y(t) is evaluated based on the selected signalType, scaled by peak amplitude AA and shifted by DC offset cc:

y(t)=c+{Asin⁡(θ(t)),sineAcos⁡(θ(t)),cosineA⋅sgn⁡(D−n(t)),squareA(2n(t)−1),sawtoothA(1−4∣n(t)−0.5∣),triangley(t) = c + \begin{cases} A \sin(\theta(t)), & \text{sine} \\ A \cos(\theta(t)), & \text{cosine} \\ A \cdot \operatorname{sgn}(D - n(t)), & \text{square} \\ A (2n(t) - 1), & \text{sawtooth} \\ A (1 - 4|n(t) - 0.5|), & \text{triangle} \end{cases}

where DD is the configured dutyCycle (0≤D≤10 \le D \le 1), and sgn⁡(x)\operatorname{sgn}(x) evaluates to +1+1 for x>0x > 0 and −1-1 for x≤0x \le 0.

When an external rising edge is detected on the optional trigger port (utrig>0u_{\text{trig}} > 0), the internal time counter resets to t=0t = 0.

Inputs & Outputs

Direction ID Label Type Status
→ In trigger Trigger number Optional
← Out out Signal number Output

Parameters

Parameters Label Type Default Description
signalType Signal Type select sine Type of periodic signal to generate
amplitude Amplitude number 1 Peak amplitude of the signal
frequency Frequency number 1 Signal frequency in Hertz
offset Offset number 0 DC offset added to the signal
phase Phase number 0 Initial phase offset in degrees
dutyCycle Duty Cycle number 0.5 Duty cycle for square wave (0-1)

Usage Examples

Example 1: Sinusoidal Excitation for Bode Frequency Response Identification

Subjecting a mechanical flexible arm to swept sinusoidal excitation to identify resonance frequencies. Configuring signalType = "sine", frequency = 25.0 Hz, amplitude = 2.0 N, and offset = 0.0. Feeding the output into an actuator model and measuring output acceleration across frequency steps enables empirical Bode magnitude and phase reconstruction.

Example 2: Bipolar Square Wave PWM Chopper with Phase Lead

Generating a bipolar reference command for an electrical drive test with signalType = "square", frequency = 50.0 Hz (T=20 msT = 20\text{ ms}), amplitude = 12.0 V, dutyCycle = 0.75 (high for 15 ms15\text{ ms}, low for 5 ms5\text{ ms}), and phase = 45.0 degrees. This produces a calibrated periodic switching waveform for inductive coil current analysis.

Remarks & Best Practices

  • Nyquist-Shannon Sampling Requirement: To ensure numerical accuracy and avoid aliasing distortion, the simulation time step TsT_s must satisfy Ts≪12fmax⁡T_s \ll \frac{1}{2 f_{\max}}. In practice, use at least 20 to 50 solver steps per waveform cycle (Ts≤0.05/fT_s \le 0.05 / f).
  • Stateless Continuous Time Evaluation: Implemented in Rust/WASM as a stateless time-dependent algebraic generator, preventing drift across long multi-hour simulation runs.
  • Bipolar vs. Unipolar Output: Unlike the unipolar PulseGenerator (which alternates between 00 and +A+A), SignalGenerator in square mode produces a bipolar waveform oscillating between −A-A and +A+A around the DC offset.
  • Negative Time Resilience: Uses double-modulo normalization ((x mod 1+1) mod 1)((x \bmod 1 + 1) \bmod 1) internally to maintain continuous waveform integrity even if time reference reverses during solver backtracking.

Related Components

  • PulseGenerator: Unipolar pulse train generator with pulse-count limit and duty cycle in percent.
  • Step: Generates a single Heaviside step transition.
  • Ramp: Unbounded linearly increasing time ramp.
  • Clock: Monotonically increasing continuous simulation time source.