thinkdsp
index
/home/downey/ThinkDSP/code/thinkdsp.py

This file contains code used in "Think DSP",
by Allen B. Downey, available from greenteapress.com
 
Copyright 2013 Allen B. Downey
License: MIT License (https://opensource.org/licenses/MIT)

 
Modules
       
copy
math
numpy
matplotlib.pyplot
random
scipy
subprocess
warnings
scipy.io.wavfile

 
Classes
       
builtins.Exception(builtins.BaseException)
UnimplementedMethodException
builtins.object
IntegratedSpectrum
Signal
Chirp
ExpoChirp
Impulses
Noise
BrownianNoise
PinkNoise
UncorrelatedGaussianNoise
UncorrelatedUniformNoise
SilentSignal
Sinusoid
ComplexSinusoid
GlottalSignal
ParabolicSignal
CubicSignal
SawtoothSignal
SquareSignal
TriangleSignal
SumSignal
Spectrogram
WavFileWriter
Wave
_SpectrumParent(builtins.object)
Dct
Spectrum

 
class BrownianNoise(Noise)
    BrownianNoise(amp=1.0)
 
Represents Brownian noise, aka red noise.
 
 
Method resolution order:
BrownianNoise
Noise
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
Computes Brownian noise by taking the cumulative sum of
a uniform random series.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Noise:
__init__(self, amp=1.0)
Initializes a white noise signal.
 
amp: float amplitude, 1.0 is nominal max

Readonly properties inherited from Noise:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Chirp(Signal)
    Chirp(start=440, end=880, amp=1.0)
 
Represents a signal with variable frequency.
 
 
Method resolution order:
Chirp
Signal
builtins.object

Methods defined here:
__init__(self, start=440, end=880, amp=1.0)
Initializes a linear chirp.
 
start: float frequency in Hz
end: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
evaluate(self, ts)
Evaluates the signal at the given times.
 
Note: This version is a little more complicated than the one
in the book because it handles the case where the ts are
not equally spaced.
 
ts: float array of times
 
returns: float wave array

Readonly properties defined here:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ComplexSinusoid(Sinusoid)
    ComplexSinusoid(freq=440, amp=1.0, offset=0, func=<ufunc 'sin'>)
 
Represents a complex exponential signal.
 
 
Method resolution order:
ComplexSinusoid
Sinusoid
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Sinusoid:
__init__(self, freq=440, amp=1.0, offset=0, func=<ufunc 'sin'>)
Initializes a sinusoidal signal.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
func: function that maps phase to amplitude

Readonly properties inherited from Sinusoid:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class CubicSignal(ParabolicSignal)
    CubicSignal(freq=440, amp=1.0, offset=0, func=&lt;ufunc 'sin'&gt;)
 
Represents a cubic signal.
 
 
Method resolution order:
CubicSignal
ParabolicSignal
Sinusoid
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Sinusoid:
__init__(self, freq=440, amp=1.0, offset=0, func=<ufunc 'sin'>)
Initializes a sinusoidal signal.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
func: function that maps phase to amplitude

Readonly properties inherited from Sinusoid:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Dct(_SpectrumParent)
    Dct(hs, fs, framerate, full=False)
 
Represents the spectrum of a signal using discrete cosine transform.
 
 
Method resolution order:
Dct
_SpectrumParent
builtins.object

Methods defined here:
__add__(self, other)
Adds two DCTs elementwise.
 
other: DCT
 
returns: new DCT
__radd__ = __add__(self, other)
make_wave(self)
Transforms to the time domain.
 
returns: Wave

Readonly properties defined here:
amps
Returns a sequence of amplitudes (read-only property).
 
Note: for DCTs, amps are positive or negative real.

Methods inherited from _SpectrumParent:
__init__(self, hs, fs, framerate, full=False)
Initializes a spectrum.
 
hs: array of amplitudes (real or complex)
fs: array of frequencies
framerate: frames per second
full: boolean to indicate full or real FFT
copy(self)
Makes a copy.
 
Returns: new Spectrum
estimate_slope(self)
Runs linear regression on log power vs log frequency.
 
returns: slope, inter, r2, p, stderr
invert(self)
Inverts this spectrum/filter.
 
returns: new Wave
max_diff(self, other)
Computes the maximum absolute difference between spectra.
 
other: Spectrum
 
returns: float
peaks(self)
Finds the highest peaks and their frequencies.
 
returns: sorted list of (amplitude, frequency) pairs
plot(self, high=None, **options)
Plots amplitude vs frequency.
 
Note: if this is a full spectrum, it ignores low and high
 
high: frequency to cut off at
plot_power(self, high=None, **options)
Plots power vs frequency.
 
high: frequency to cut off at
ratio(self, denom, thresh=1, val=0)
The ratio of two spectrums.
 
denom: Spectrum
thresh: values smaller than this are replaced
val: with this value
 
returns: new Wave
render_full(self, high=None)
Extracts amps and fs from a full spectrum.
 
high: cutoff frequency
 
returns: fs, amps

Readonly properties inherited from _SpectrumParent:
freq_res
max_freq
Returns the Nyquist frequency for this spectrum.
power
Returns a sequence of powers (read-only property).

Data descriptors inherited from _SpectrumParent:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ExpoChirp(Chirp)
    ExpoChirp(start=440, end=880, amp=1.0)
 
Represents a signal with varying frequency.
 
 
Method resolution order:
ExpoChirp
Chirp
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Chirp:
__init__(self, start=440, end=880, amp=1.0)
Initializes a linear chirp.
 
start: float frequency in Hz
end: float frequency in Hz
amp: float amplitude, 1.0 is nominal max

Readonly properties inherited from Chirp:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class GlottalSignal(Sinusoid)
    GlottalSignal(freq=440, amp=1.0, offset=0, func=&lt;ufunc 'sin'&gt;)
 
Represents a periodic signal that resembles a glottal signal.
 
 
Method resolution order:
GlottalSignal
Sinusoid
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Sinusoid:
__init__(self, freq=440, amp=1.0, offset=0, func=<ufunc 'sin'>)
Initializes a sinusoidal signal.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
func: function that maps phase to amplitude

Readonly properties inherited from Sinusoid:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Impulses(Signal)
    Impulses(locations, amps=1)
 
Represents silence.
 
 
Method resolution order:
Impulses
Signal
builtins.object

Methods defined here:
__init__(self, locations, amps=1)
Initialize self.  See help(type(self)) for accurate signature.
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Readonly properties inherited from Signal:
period
Period of the signal in seconds (property).
 
Since this is used primarily for purposes of plotting,
the default behavior is to return a value, 0.1 seconds,
that is reasonable for many signals.
 
returns: float seconds

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class IntegratedSpectrum(builtins.object)
    IntegratedSpectrum(cs, fs)
 
Represents the integral of a spectrum.
 
  Methods defined here:
__init__(self, cs, fs)
Initializes an integrated spectrum:
 
cs: sequence of cumulative amplitudes
fs: sequence of frequencies
estimate_slope(self, low=1, high=-12000)
Runs linear regression on log cumulative power vs log frequency.
 
returns: slope, inter, r2, p, stderr
plot_power(self, low=0, high=None, expo=False, **options)
Plots the integrated spectrum.
 
low: int index to start at
high: int index to end at

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Noise(Signal)
    Noise(amp=1.0)
 
Represents a noise signal (abstract parent class).
 
 
Method resolution order:
Noise
Signal
builtins.object

Methods defined here:
__init__(self, amp=1.0)
Initializes a white noise signal.
 
amp: float amplitude, 1.0 is nominal max

Readonly properties defined here:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class ParabolicSignal(Sinusoid)
    ParabolicSignal(freq=440, amp=1.0, offset=0, func=&lt;ufunc 'sin'&gt;)
 
Represents a parabolic signal.
 
 
Method resolution order:
ParabolicSignal
Sinusoid
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Sinusoid:
__init__(self, freq=440, amp=1.0, offset=0, func=<ufunc 'sin'>)
Initializes a sinusoidal signal.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
func: function that maps phase to amplitude

Readonly properties inherited from Sinusoid:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class PinkNoise(Noise)
    PinkNoise(amp=1.0, beta=1.0)
 
Represents Brownian noise, aka red noise.
 
 
Method resolution order:
PinkNoise
Noise
Signal
builtins.object

Methods defined here:
__init__(self, amp=1.0, beta=1.0)
Initializes a pink noise signal.
 
amp: float amplitude, 1.0 is nominal max
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave

Readonly properties inherited from Noise:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class SawtoothSignal(Sinusoid)
    SawtoothSignal(freq=440, amp=1.0, offset=0, func=&lt;ufunc 'sin'&gt;)
 
Represents a sawtooth signal.
 
 
Method resolution order:
SawtoothSignal
Sinusoid
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Sinusoid:
__init__(self, freq=440, amp=1.0, offset=0, func=<ufunc 'sin'>)
Initializes a sinusoidal signal.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
func: function that maps phase to amplitude

Readonly properties inherited from Sinusoid:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Signal(builtins.object)
    Represents a time-varying signal.
 
  Methods defined here:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Readonly properties defined here:
period
Period of the signal in seconds (property).
 
Since this is used primarily for purposes of plotting,
the default behavior is to return a value, 0.1 seconds,
that is reasonable for many signals.
 
returns: float seconds

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class SilentSignal(Signal)
    Represents silence.
 
 
Method resolution order:
SilentSignal
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Readonly properties inherited from Signal:
period
Period of the signal in seconds (property).
 
Since this is used primarily for purposes of plotting,
the default behavior is to return a value, 0.1 seconds,
that is reasonable for many signals.
 
returns: float seconds

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Sinusoid(Signal)
    Sinusoid(freq=440, amp=1.0, offset=0, func=&lt;ufunc 'sin'&gt;)
 
Represents a sinusoidal signal.
 
 
Method resolution order:
Sinusoid
Signal
builtins.object

Methods defined here:
__init__(self, freq=440, amp=1.0, offset=0, func=<ufunc 'sin'>)
Initializes a sinusoidal signal.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
func: function that maps phase to amplitude
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Readonly properties defined here:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Spectrogram(builtins.object)
    Spectrogram(spec_map, seg_length)
 
Represents the spectrum of a signal.
 
  Methods defined here:
__init__(self, spec_map, seg_length)
Initialize the spectrogram.
 
spec_map: map from float time to Spectrum
seg_length: number of samples in each segment
any_spectrum(self)
Returns an arbitrary spectrum from the spectrogram.
frequencies(self)
Sequence of frequencies.
 
returns: sequence of float freqencies in Hz.
get_data(self, high=None, **options)
Returns spectogram as 2D numpy array 
 
high: highest frequency component to return
make_wave(self)
Inverts the spectrogram and returns a Wave.
 
returns: Wave
plot(self, high=None, **options)
Make a pseudocolor plot.
 
high: highest frequency component to plot
times(self)
Sorted sequence of times.
 
returns: sequence of float times in seconds

Readonly properties defined here:
freq_res
Frequency resolution in Hz.
time_res
Time resolution in seconds.

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Spectrum(_SpectrumParent)
    Spectrum(hs, fs, framerate, full=False)
 
Represents the spectrum of a signal.
 
 
Method resolution order:
Spectrum
_SpectrumParent
builtins.object

Methods defined here:
__add__(self, other)
Adds two spectrums elementwise.
 
other: Spectrum
 
returns: new Spectrum
__len__(self)
Length of the spectrum.
__mul__(self, other)
Multiplies two spectrums elementwise.
 
other: Spectrum
 
returns: new Spectrum
__radd__ = __add__(self, other)
band_stop(self, low_cutoff, high_cutoff, factor=0)
Attenuate frequencies between the cutoffs.
 
low_cutoff: frequency in Hz
high_cutoff: frequency in Hz
factor: what to multiply the magnitude by
convolve(self, other)
Convolves two Spectrums.
 
other: Spectrum
 
returns: Spectrum
differentiate(self)
Apply the differentiation filter.
 
returns: new Spectrum
high_pass(self, cutoff, factor=0)
Attenuate frequencies below the cutoff.
 
cutoff: frequency in Hz
factor: what to multiply the magnitude by
integrate(self)
Apply the integration filter.
 
returns: new Spectrum
low_pass(self, cutoff, factor=0)
Attenuate frequencies above the cutoff.
 
cutoff: frequency in Hz
factor: what to multiply the magnitude by
make_integrated_spectrum(self)
Makes an integrated spectrum.
make_wave(self)
Transforms to the time domain.
 
returns: Wave
pink_filter(self, beta=1)
Apply a filter that would make white noise pink.
 
beta: exponent of the pink noise
scale(self, factor)
Multiplies all elements by the given factor.
 
factor: what to multiply the magnitude by (could be complex)

Readonly properties defined here:
angles
Returns a sequence of angles (read-only property).
imag
Returns the imaginary part of the hs (read-only property).
real
Returns the real part of the hs (read-only property).

Methods inherited from _SpectrumParent:
__init__(self, hs, fs, framerate, full=False)
Initializes a spectrum.
 
hs: array of amplitudes (real or complex)
fs: array of frequencies
framerate: frames per second
full: boolean to indicate full or real FFT
copy(self)
Makes a copy.
 
Returns: new Spectrum
estimate_slope(self)
Runs linear regression on log power vs log frequency.
 
returns: slope, inter, r2, p, stderr
invert(self)
Inverts this spectrum/filter.
 
returns: new Wave
max_diff(self, other)
Computes the maximum absolute difference between spectra.
 
other: Spectrum
 
returns: float
peaks(self)
Finds the highest peaks and their frequencies.
 
returns: sorted list of (amplitude, frequency) pairs
plot(self, high=None, **options)
Plots amplitude vs frequency.
 
Note: if this is a full spectrum, it ignores low and high
 
high: frequency to cut off at
plot_power(self, high=None, **options)
Plots power vs frequency.
 
high: frequency to cut off at
ratio(self, denom, thresh=1, val=0)
The ratio of two spectrums.
 
denom: Spectrum
thresh: values smaller than this are replaced
val: with this value
 
returns: new Wave
render_full(self, high=None)
Extracts amps and fs from a full spectrum.
 
high: cutoff frequency
 
returns: fs, amps

Readonly properties inherited from _SpectrumParent:
amps
Returns a sequence of amplitudes (read-only property).
freq_res
max_freq
Returns the Nyquist frequency for this spectrum.
power
Returns a sequence of powers (read-only property).

Data descriptors inherited from _SpectrumParent:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class SquareSignal(Sinusoid)
    SquareSignal(freq=440, amp=1.0, offset=0, func=&lt;ufunc 'sin'&gt;)
 
Represents a square signal.
 
 
Method resolution order:
SquareSignal
Sinusoid
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Sinusoid:
__init__(self, freq=440, amp=1.0, offset=0, func=<ufunc 'sin'>)
Initializes a sinusoidal signal.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
func: function that maps phase to amplitude

Readonly properties inherited from Sinusoid:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class SumSignal(Signal)
    SumSignal(*args)
 
Represents the sum of signals.
 
 
Method resolution order:
SumSignal
Signal
builtins.object

Methods defined here:
__init__(self, *args)
Initializes the sum.
 
args: tuple of signals
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Readonly properties defined here:
period
Period of the signal in seconds.
 
Note: this is not correct; it's mostly a placekeeper.
 
But it is correct for a harmonic sequence where all
component frequencies are multiples of the fundamental.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class TriangleSignal(Sinusoid)
    TriangleSignal(freq=440, amp=1.0, offset=0, func=&lt;ufunc 'sin'&gt;)
 
Represents a triangle signal.
 
 
Method resolution order:
TriangleSignal
Sinusoid
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Sinusoid:
__init__(self, freq=440, amp=1.0, offset=0, func=<ufunc 'sin'>)
Initializes a sinusoidal signal.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
func: function that maps phase to amplitude

Readonly properties inherited from Sinusoid:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class UncorrelatedGaussianNoise(Noise)
    UncorrelatedGaussianNoise(amp=1.0)
 
Represents uncorrelated gaussian noise.
 
 
Method resolution order:
UncorrelatedGaussianNoise
Noise
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Noise:
__init__(self, amp=1.0)
Initializes a white noise signal.
 
amp: float amplitude, 1.0 is nominal max

Readonly properties inherited from Noise:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class UncorrelatedUniformNoise(Noise)
    UncorrelatedUniformNoise(amp=1.0)
 
Represents uncorrelated uniform noise.
 
 
Method resolution order:
UncorrelatedUniformNoise
Noise
Signal
builtins.object

Methods defined here:
evaluate(self, ts)
Evaluates the signal at the given times.
 
ts: float array of times
 
returns: float wave array

Methods inherited from Noise:
__init__(self, amp=1.0)
Initializes a white noise signal.
 
amp: float amplitude, 1.0 is nominal max

Readonly properties inherited from Noise:
period
Period of the signal in seconds.
 
returns: float seconds

Methods inherited from Signal:
__add__(self, other)
Adds two signals.
 
other: Signal
 
returns: Signal
__radd__ = __add__(self, other)
make_wave(self, duration=1, start=0, framerate=11025)
Makes a Wave object.
 
duration: float seconds
start: float seconds
framerate: int frames per second
 
returns: Wave
plot(self, framerate=11025)
Plots the signal.
 
The default behavior is to plot three periods.
 
framerate: samples per second

Data descriptors inherited from Signal:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class UnimplementedMethodException(builtins.Exception)
    Exception if someone calls a method that should be overridden.
 
 
Method resolution order:
UnimplementedMethodException
builtins.Exception
builtins.BaseException
builtins.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from builtins.Exception:
__init__(self, /, *args, **kwargs)
Initialize self.  See help(type(self)) for accurate signature.

Static methods inherited from builtins.Exception:
__new__(*args, **kwargs) from builtins.type
Create and return a new object.  See help(type) for accurate signature.

Methods inherited from builtins.BaseException:
__delattr__(self, name, /)
Implement delattr(self, name).
__getattribute__(self, name, /)
Return getattr(self, name).
__reduce__(...)
Helper for pickle.
__repr__(self, /)
Return repr(self).
__setattr__(self, name, value, /)
Implement setattr(self, name, value).
__setstate__(...)
__str__(self, /)
Return str(self).
with_traceback(...)
Exception.with_traceback(tb) --
set self.__traceback__ to tb and return self.

Data descriptors inherited from builtins.BaseException:
__cause__
exception cause
__context__
exception context
__dict__
__suppress_context__
__traceback__
args

 
class WavFileWriter(builtins.object)
    WavFileWriter(filename='sound.wav', framerate=11025)
 
Writes wav files.
 
  Methods defined here:
__init__(self, filename='sound.wav', framerate=11025)
Opens the file and sets parameters.
 
filename: string
framerate: samples per second
close(self, duration=0)
Closes the file.
 
duration: how many seconds of silence to append
write(self, wave)
Writes a wave.
 
wave: Wave

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class Wave(builtins.object)
    Wave(ys, ts=None, framerate=None)
 
Represents a discrete-time waveform.
 
  Methods defined here:
__add__(self, other)
Adds two waves elementwise.
 
other: Wave
 
returns: new Wave
__init__(self, ys, ts=None, framerate=None)
Initializes the wave.
 
ys: wave array
ts: array of times
framerate: samples per second
__len__(self)
__mul__(self, other)
Multiplies two waves elementwise.
 
Note: this operation ignores the timestamps; the result
has the timestamps of self.
 
other: Wave
 
returns: new Wave
__or__(self, other)
Concatenates two waves.
 
other: Wave
 
returns: new Wave
__radd__ = __add__(self, other)
apodize(self, denom=20, duration=0.1)
Tapers the amplitude at the beginning and end of the signal.
 
Tapers either the given duration of time or the given
fraction of the total duration, whichever is less.
 
denom: float fraction of the segment to taper
duration: float duration of the taper in seconds
convolve(self, other)
Convolves two waves.
 
Note: this operation ignores the timestamps; the result
has the timestamps of self.
 
other: Wave or NumPy array
 
returns: Wave
copy(self)
Makes a copy.
 
Returns: new Wave
corr(self, other)
Correlation coefficient two waves.
 
other: Wave
 
returns: float coefficient of correlation
cos_cov(self, k)
Covariance with a cosine signal.
 
freq: freq of the cosine signal in Hz
 
returns: float covariance
cos_transform(self)
Discrete cosine transform.
 
returns: list of frequency, cov pairs
cov(self, other)
Covariance of two unbiased waves.
 
other: Wave
 
returns: float
cov_mat(self, other)
Covariance matrix of two waves.
 
other: Wave
 
returns: 2x2 covariance matrix
cumsum(self)
Computes the cumulative sum of the elements.
 
returns: new Wave
diff(self)
Computes the difference between successive elements.
 
returns: new Wave
find_index(self, t)
Find the index corresponding to a given time.
get_xfactor(self, options)
hamming(self)
Apply a Hamming window to the wave.
make_audio(self)
Makes an IPython Audio object.
make_dct(self)
Computes the DCT of this wave.
make_spectrogram(self, seg_length, win_flag=True)
Computes the spectrogram of the wave.
 
seg_length: number of samples in each segment
win_flag: boolean, whether to apply hamming window to each segment
 
returns: Spectrogram
make_spectrum(self, full=False)
Computes the spectrum using FFT.
 
full: boolean, whethere to compute a full FFT
      (as opposed to a real FFT)
 
returns: Spectrum
max_diff(self, other)
Computes the maximum absolute difference between waves.
 
other: Wave
 
returns: float
normalize(self, amp=1.0)
Normalizes the signal to the given amplitude.
 
amp: float amplitude
play(self, filename='sound.wav')
Plays a wave file.
 
filename: string
plot(self, **options)
Plots the wave.
 
If the ys are complex, plots the real part.
plot_vlines(self, **options)
Plots the wave with vertical lines for samples.
quantize(self, bound, dtype)
Maps the waveform to quanta.
 
bound: maximum amplitude
dtype: numpy data type or string
 
returns: quantized signal
roll(self, roll)
Rolls this wave by the given number of locations.
scale(self, factor)
Multplies the wave by a factor.
 
factor: scale factor
segment(self, start=None, duration=None)
Extracts a segment.
 
start: float start time in seconds
duration: float duration in seconds
 
returns: Wave
shift(self, shift)
Shifts the wave left or right in time.
 
shift: float time shift
slice(self, i, j)
Makes a slice from a Wave.
 
i: first slice index
j: second slice index
truncate(self, n)
Trims this wave to the given length.
 
n: integer index
unbias(self)
Unbiases the signal.
window(self, window)
Apply a window to the wave.
 
window: sequence of multipliers, same length as self.ys
write(self, filename='sound.wav')
Write a wave file.
 
filename: string
zero_pad(self, n)
Trims this wave to the given length.
 
n: integer index

Readonly properties defined here:
duration
Duration (property).
 
returns: float duration in seconds
end
start

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Functions
       
CosSignal(freq=440, amp=1.0, offset=0)
Makes a cosine Sinusoid.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
 
returns: Sinusoid object
SinSignal(freq=440, amp=1.0, offset=0)
Makes a sine Sinusoid.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
 
returns: Sinusoid object
Sinc(freq=440, amp=1.0, offset=0)
Makes a Sinc function.
 
freq: float frequency in Hz
amp: float amplitude, 1.0 is nominal max
offset: float phase offset in radians
 
returns: Sinusoid object
apodize(ys, framerate, denom=20, duration=0.1)
Tapers the amplitude at the beginning and end of the signal.
 
Tapers either the given duration of time or the given
fraction of the total duration, whichever is less.
 
ys: wave array
framerate: int frames per second
denom: float fraction of the segment to taper
duration: float duration of the taper in seconds
 
returns: wave array
cos_wave(freq, duration=1, offset=0)
Makes a cosine wave with the given parameters.
 
freq: float cycles per second
duration: float seconds
offset: float radians
 
returns: Wave
decorate(**options)
Decorate the current axes.
 
Call decorate with keyword arguments like
 
decorate(title='Title',
         xlabel='x',
         ylabel='y')
 
The keyword arguments can be any of the axis properties
 
https://matplotlib.org/api/axes_api.html
 
In addition, you can use `legend=False` to suppress the legend.
 
And you can use `loc` to indicate the location of the legend
(the default value is 'best')
find_index(x, xs)
Find the index corresponding to a given value in an array.
infer_framerate(ts)
Given ts, find the framerate.
 
Assumes that the ts are equally spaced.
 
ts: sequence of times in seconds
 
returns: frames per second
legend(**options)
Draws a legend only if there is at least one labeled item.
 
options are passed to plt.legend()
https://matplotlib.org/api/_as_gen/matplotlib.plt.legend.html
mag(a)
Computes the magnitude of a numpy array.
 
a: numpy array
 
returns: float
main()
make_chord(midi_nums, duration, sig_cons=<function CosSignal at 0x7f8c22954940>, framerate=11025)
Make a chord with the given duration.
 
midi_nums: sequence of int MIDI note numbers
duration: float seconds
sig_cons: Signal constructor function
framerate: int frames per second
 
returns: Wave
make_note(midi_num, duration, sig_cons=<function CosSignal at 0x7f8c22954940>, framerate=11025)
Make a MIDI note with the given duration.
 
midi_num: int MIDI note number
duration: float seconds
sig_cons: Signal constructor function
framerate: int frames per second
 
returns: Wave
midi_to_freq(midi_num)
Converts MIDI note number to frequency.
 
midi_num: int MIDI note number
 
returns: float frequency in Hz
normalize(ys, amp=1.0)
Normalizes a wave array so the maximum amplitude is +amp or -amp.
 
ys: wave array
amp: max amplitude (pos or neg) in result
 
returns: wave array
play_wave(filename='sound.wav', player='aplay')
Plays a wave file.
 
filename: string
player: string name of executable that plays wav files
quantize(ys, bound, dtype)
Maps the waveform to quanta.
 
ys: wave array
bound: maximum amplitude
dtype: numpy data type of the result
 
returns: quantized signal
random_seed(x)
Initialize the random and np.random generators.
 
x: int seed
read_wave(filename='sound.wav')
Reads a wave file.
 
filename: string
 
returns: Wave
read_wave_with_scipy(filename)
Reads a wave file.
 
filename: string
 
returns: Wave
remove_from_legend(bad_labels)
Removes some labels from the legend.
 
bad_labels: sequence of strings
rest(duration)
Makes a rest of the given duration.
 
duration: float seconds
 
returns: Wave
shift_left(ys, shift)
Shifts a wave array to the left.
 
ys: wave array
shift: integer shift
 
returns: wave array
shift_right(ys, shift)
Shifts a wave array to the right and zero pads.
 
ys: wave array
shift: integer shift
 
returns: wave array
sin_wave(freq, duration=1, offset=0)
Makes a sine wave with the given parameters.
 
freq: float cycles per second
duration: float seconds
offset: float radians
 
returns: Wave
truncate(ys, n)
Trims a wave array to the given length.
 
ys: wave array
n: integer length
 
returns: wave array
unbias(ys)
Shifts a wave array so it has mean 0.
 
ys: wave array
 
returns: wave array
underride(d, **options)
Add key-value pairs to d only if key is not in d.
 
If d is None, create a new dictionary.
 
d: dictionary
options: keyword args to add to d
zero_pad(array, n)
Extends an array with zeros.
 
array: numpy array
n: length of result
 
returns: new NumPy array

 
Data
        PI2 = 6.283185307179586