sine wave
frequency modulation
signal processing
sound engineering
ramp function

sine wave that slowly ramps up frequency from f1 to f2 for a given time

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A sine wave whose frequency changes gradually over time is usually called a chirp or sweep. The important detail is that once frequency varies with time, you cannot just drop a changing f(t) into the ordinary sin(2 * pi * f * t) formula and expect a correct result.

The right way to build the signal is to define instantaneous frequency first, integrate it into phase, and then evaluate the sine wave. That distinction is what makes a frequency ramp physically and mathematically correct.

Instantaneous Frequency First

For a linear sweep from f1 to f2 over a duration T, define the instantaneous frequency as:

f(t) = f1 + (f2 - f1) * t / T

This gives you a straight-line ramp:

  • at t = 0, the signal starts at f1
  • at t = T, it reaches f2
  • in between, it changes linearly

That part is simple. The subtle part is phase. Frequency is the rate of phase change, so when frequency varies, phase is the integral of that varying frequency.

Convert Frequency into Phase

For a constant-frequency sine wave, you can write:

x(t) = A * sin(2 * pi * f * t + phi0)

But for a time-varying frequency, the correct phase for a linear ramp is:

phase(t) = 2 * pi * (f1 * t + ((f2 - f1) * t * t) / (2 * T)) + phi0

Then the waveform becomes:

x(t) = A * sin(phase(t))

That 1 / 2 inside the quadratic term matters. A common incorrect shortcut is to write sin(2 * pi * f(t) * t). That formula does not correctly represent the accumulated phase of a chirp.

Generating the Sweep in Python

Here is a small runnable example that creates a one-second chirp from 440 Hz to 880 Hz and writes it to a WAV file:

python
1import math
2import struct
3import wave
4
5
6def linear_chirp(duration, sample_rate, f1, f2, amplitude=0.6, phi0=0.0):
7    sample_count = int(duration * sample_rate)
8    output = []
9
10    for n in range(sample_count):
11        t = n / sample_rate
12        phase = 2 * math.pi * (
13            f1 * t + ((f2 - f1) * t * t) / (2 * duration)
14        ) + phi0
15        output.append(amplitude * math.sin(phase))
16
17    return output
18
19
20samples = linear_chirp(
21    duration=1.0,
22    sample_rate=44100,
23    f1=440.0,
24    f2=880.0,
25)
26
27with wave.open("chirp.wav", "w") as wav_file:
28    wav_file.setnchannels(1)
29    wav_file.setsampwidth(2)
30    wav_file.setframerate(44100)
31
32    for sample in samples:
33        pcm = int(max(-1.0, min(1.0, sample)) * 32767)
34        wav_file.writeframes(struct.pack("<h", pcm))

If you listen to the result, the pitch rises smoothly. If you inspected the waveform visually, the oscillations would appear more tightly packed as time increases because the frequency is getting higher.

When a Library Is Better

If you are doing serious DSP work, use a tested library rather than deriving every sweep by hand. In Python, scipy.signal.chirp is a good example. It supports linear, logarithmic, and other sweep laws while keeping the phase math correct.

Even when you rely on a library, the integral idea still matters. It explains why chirp APIs are defined in terms of start frequency, end frequency, and sweep duration instead of asking you to write an ad hoc sinusoid.

Where Chirps Are Used

Frequency ramps show up in more places than audio demos:

  • radar and sonar sweeps
  • speaker and room measurements
  • filter testing and system identification
  • sound design and synthesizers

In many of those cases, the chirp is useful precisely because it covers a range of frequencies in a controlled way.

Common Pitfalls

The biggest mistake is using sin(2 * pi * f(t) * t) instead of integrating frequency into phase first.

Another common problem is mixing units. If frequency is in hertz, then time must be in seconds for the formula to be meaningful.

A third issue is aliasing. If the sweep approaches or crosses half the sample rate, the generated digital signal will not represent the intended high-frequency content correctly.

In audio applications, abrupt starts and stops can also create clicks. A short fade-in and fade-out often makes the result cleaner.

Summary

  • A sine wave with a slow frequency ramp is usually called a chirp.
  • Define instantaneous frequency first, then integrate it into phase.
  • For a linear chirp, the phase contains a quadratic term in time.
  • The shortcut sin(2 * pi * f(t) * t) is not the correct general formula.
  • Libraries can generate chirps for you, but understanding the phase model prevents subtle bugs.

Course illustration
Course illustration

All Rights Reserved.