liveBook · Manning

Square wave

As explained in Learn Haskell by Example

A square wave is a periodic waveform that switches between two constant levels.

A square wave is a periodic waveform that switches between two constant sample levels. Represent it as a Haskell function from a phase value between 0 and 1 to a sample value. section 10.1.2

Define and use a square wave

Use a threshold at the midpoint of each period: sqw :: Wave sqw t | t <= 0.5 = -1 | otherwise = 1 tone sqw 220 5 The tone function repeatedly scans phase values across the period, maps them through sqw, and produces samples for a 220 Hz, five-second signal. section 10.1.2

Figure 10.2 Four standard waveforms
figure

Compare the square wave’s two-level shape with sine, sawtooth, and triangle waveforms. section 10.1.2

How Learn Haskell by Example covers it

How deep it goes
Defines midpoint-based case selection and applies it during periodic sample generation, without developing waveform mathematics beyond the valid phase range.
Examples
Uses sqw, a 220 Hz five-second tone, and exported audio containing four standard waveforms.
What it assumes
Assumes basic numeric functions and introduces the Wave function model, phase parameter, samples, and frequency relationship.
What you can do after
Build it from scratch as a Haskell Wave function and use it to generate audio samples.
How it connects
Connects waveforms with signals, samples, frequency, sine waves, sawtooth waves, and triangle waves.

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.