SquareWave

A source unit that generates a square wave signal. The signal can be tuned by changing the period and/or frequency of the oscillation, as well as the width.

_images/Plaquette-SquareWave.png

The width represents the proportion of time (expressed as a percentage) in each cycle (period) during which the wave is “on”.

_images/Plaquette-SquareWave-Width.png

Example

Makes the built-in LED blink with a period of 4 seconds. Because the duty cycle is set to 25%, the LED will stay on for 1 second and then off for 3 seconds.

#include <Plaquette.h>

DigitalOut led(13);

SquareWave blinkOsc(4.0);

void begin() {
  blinkOsc.width(0.25); // Sets the duty cycle to 25%
}

void step() {
  blinkOsc >> led;
}
class SquareWave : public AbstractWave

Square oscillator. Duty cycle is expressed as % of period.

Public Functions

SquareWave(float period = 1.0f, float width = 0.5f)

Constructor.

Parameters:
  • period – the period of oscillation (in seconds)

  • width – the duty-cycle as a value in [0, 1]

virtual void period(float period)

Sets the period (in seconds).

Parameters:

period – the period of oscillation (in seconds)

virtual void frequency(float frequency)

Sets the frequency (in Hz).

Parameters:

frequency – the frequency of oscillation (in Hz)

inline virtual float frequency() const

Returns the frequency (in Hz).

virtual void bpm(float bpm)

Sets the frequency in beats-per-minute.

Parameters:

bpm – the frequency of oscillation (in BPM)

inline virtual float bpm() const

Returns the frequency (in BPM).

virtual void width(float width)

Sets the width of the signal as a % of period.

Parameters:

width – the width as a value in [0, 1]

inline virtual float width() const

Returns the width of the signal.

virtual void amplitude(float amplitude)

Sets the amplitude of the wave.

Parameters:

amplitude – a value in [0, 1] that determines the amplitude of the wave (centered at 0.5).

inline virtual float amplitude() const

Returns the amplitude of the wave.

virtual void phase(float phase)

Sets the phase (ie.

the offset, in % of period).

Parameters:

phase – the phase (in % of period)

inline virtual float phase() const

Returns the phase (in % of period).

virtual float shiftBy(float phaseShift)

Returns oscillator’s value with given phase shift (in %).

Supports negative phase shifts. Eg. shiftBy(0.2) returns future value of oscillator after 20% of its period would have passed.

Parameters:

phase – the phase shift (in % of period)

Returns:

the value of oscillator with given phase shift

inline virtual float get()

Returns value in [0, 1].

inline virtual float mapTo(float toLow, float toHigh)

Maps value to new range.

See Also