liveBook · Manning

Frequency

As explained in Learn Haskell by Example

Frequency is the rate at which a periodic signal repeats, measured here in hertz and corresponding to perceived musical pitch.

Frequency measures how often a periodic signal repeats. In a sampled audio signal, it is represented in hertz (Hz) as a Double. A frequency of n Hz has a period of 1 / n seconds; at a 44.1 kHz sample rate, dividing 44,100 by n gives the integer number of samples in one period. Rounding is necessary because a signal cannot contain a fraction of a sample. section 10.1.1

Listing 10.1 Types and helper functions for dealing with signals
type Sample = Double       #1

type Signal = [Sample]    #1  

type Hz = Double          #1

type Seconds = Double     #1

sampleRate :: Double     #2
sampleRate = 44100

samplesPerPeriod :: Hz -> Int
samplesPerPeriod hz = round $ sampleRate / hz            #3

samplesPerSecond :: Seconds -> Int
samplesPerSecond duration = round $
 duration * sampleRate       #4

Use Hz, Seconds, and the fixed sample rate to calculate samples per period and samples per duration before generating a signal. section 10.1.1

Frequency controls the rate at which a waveform is scanned. For a fixed duration, higher frequencies use shorter periods and repeat more often; lower frequencies use longer periods. Repeating a waveform’s sampled period until the duration is full produces a tone. Sine, square, sawtooth, and triangle waves can therefore share the same frequency logic while producing different sound characteristics. section 10.1.2

A practical signal generator separates these responsibilities:

  • Represent a waveform as a function from a position between 0 and 1 to a sample value. section 10.1.2
  • Compute samples per period from the requested frequency. section 10.1.1
  • Wrap sample indices with modulo to rescan the waveform period across the requested duration. section 10.1.2
  • Keep samples in the -1 to 1 range before later volume processing and WAV export. section 10.1.2

Frequency also provides the numerical form of musical pitch. A Pitchable value converts to Hz: Hz values are already frequencies, while semitone distances are measured from concert A at 440 Hz. In 12-tone equal temperament, moving up 12 semitones doubles the frequency, and moving down 12 halves it. Chromatic note names and octaves can first be converted to semitone distances, then to frequency. section 10.4.1

Generate four 220 Hz tones

Apply the same 220 Hz frequency and five-second duration to sine, triangle, sawtooth, and square waves, concatenate the resulting signals, and write them to a WAV file. The pitch rate stays fixed while the waveform changes the sound character. section 10.1.2

Sampling limits which frequencies can be represented accurately. A sample rate must be at least twice the known highest frequency under the Nyquist–Shannon sampling theorem. The 44.1 kHz rate is used because audio frequencies above 22 kHz are treated as irrelevant here, while bit resolution instead affects signal-to-noise ratio. section 10.1

How Learn Haskell by Example covers it

How deep it goes
Derives samples per period, implements waveform scanning, and converts semitones and chromatic notes; defers volume modulation.
Examples
Uses 44.1 kHz sampling, four standard waveforms at 220 Hz, and concert A at 440 Hz with octave checks.
What it assumes
Introduces Hz as Double and explains rounding through numeric type classes while relying on basic arithmetic and functions.
What you can do after
Build it from scratch by calculating periodic sample counts, generating fixed-frequency tones, and converting notes into Hz.
How it connects
Connects frequency with sampled signals, sample rate, waveforms, pitch, semitones, chromatic notes, and WAV output.

Also explained in

Compare how 7 books explain frequency →

Related concepts

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.