librosa.chirp#

librosa.chirp(*, fmin, fmax, sr=22050, length=None, duration=None, linear=False, phi=None, weighting=None)[source]#

Construct a “chirp” or “sine-sweep” signal.

The chirp sweeps from frequency fmin to fmax (in Hz).

Parameters:
fminfloat > 0 or str

initial frequency (in Hz) or note name in SPN format (e.g., ‘A2’)

fmaxfloat > 0 or str

final frequency (in Hz) or note name in SPN format (e.g., ‘A8’)

srnumber > 0

desired sampling rate of the output signal

lengthint > 0

desired number of samples in the output signal. When both duration and length are defined, length takes priority.

durationfloat > 0

desired duration in seconds. When both duration and length are defined, length takes priority.

linearbool
  • If True, use a linear sweep, i.e., frequency changes linearly with time

  • If False, use a exponential sweep.

Default is False.

phifloat or None

phase offset, in radians. If unspecified, defaults to -np.pi * 0.5.

weightingstr or None

Type of perceptual weighting to apply (e.g., 'A', 'B', 'C', 'D', 'Z'). If None, no weighting is applied.

Returns:
chirp_signalnp.ndarray [shape=(length,), dtype=float64]

Synthesized chirp signal

Raises:
ParameterValueError
  • If either fmin or fmax are not provided.

  • If min(fmin, fmax) meets or exceeds Nyquist frequency (sr / 2).

  • If neither length nor duration are provided.

Examples

Generate an exponential chirp from A2 to A8

>>> exponential_chirp = librosa.chirp(fmin="A2", fmax="A8", duration=1, sr=22050)

Or generate the same signal using length

>>> exponential_chirp = librosa.chirp(fmin="A2", fmax="A8", sr=22050, length=22050)

Or generate a linear chirp instead

>>> linear_chirp = librosa.chirp(
...     fmin="A2", fmax="A8", duration=1, linear=True, sr=22050
... )

Display spectrogram for both exponential and linear chirps.

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
>>> S_exponential = np.abs(librosa.stft(y=exponential_chirp))
>>> librosa.display.specshow(
...     S_exponential, vscale="dBFS", x_axis="time", y_axis="linear", ax=ax[0]
... )
>>> ax[0].set(title="Exponential chirp", xlabel=None)
>>> ax[0].label_outer()
>>> S_linear = np.abs(librosa.stft(y=linear_chirp))
>>> librosa.display.specshow(
...     S_linear, vscale="dBFS", x_axis="time", y_axis="linear", ax=ax[1]
... )
>>> ax[1].set(title="Linear chirp")
../../_images/librosa-chirp-1.png