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






