Skip to content

Commit

Permalink
Formatted with black
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenDowney committed Aug 2, 2023
1 parent 178d7aa commit 9aaf8c9
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions code/thinkdsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ def find_index(x, xs):


class _SpectrumParent:
"""Contains code common to Spectrum and DCT.
"""
"""Contains code common to Spectrum and DCT."""

def __init__(self, hs, fs, framerate, full=False):
"""Initializes a spectrum.
Expand Down Expand Up @@ -196,7 +195,7 @@ def amps(self):
@property
def power(self):
"""Returns a sequence of powers (read-only property)."""
return self.amps ** 2
return self.amps**2

def copy(self):
"""Makes a copy.
Expand Down Expand Up @@ -280,7 +279,7 @@ def plot_power(self, high=None, **options):
"""
if self.full:
fs, amps = self.render_full(high)
plt.plot(fs, amps ** 2, **options)
plt.plot(fs, amps**2, **options)
else:
i = None if high is None else find_index(high, self.fs)
plt.plot(self.fs[:i], self.power[:i], **options)
Expand Down Expand Up @@ -432,14 +431,13 @@ def integrate(self):
returns: new Spectrum
"""
new = self.copy()
zero = (new.fs == 0)
zero = new.fs == 0
new.hs[~zero] /= PI2 * 1j * new.fs[~zero]
new.hs[zero] = np.inf
return new

def make_integrated_spectrum(self):
"""Makes an integrated spectrum.
"""
"""Makes an integrated spectrum."""
cs = np.cumsum(self.power)
cs /= cs[-1]
return IntegratedSpectrum(cs, self.fs)
Expand Down Expand Up @@ -603,7 +601,7 @@ def plot(self, high=None, **options):
spectrum = self.spec_map[t]
array[:, j] = spectrum.amps[:i]

underride(options, cmap='inferno_r', shading='auto')
underride(options, cmap="inferno_r", shading="auto")
plt.pcolormesh(ts, fs, array, **options)

def get_data(self, high=None, **options):
Expand Down Expand Up @@ -658,9 +656,7 @@ def make_wave(self):


class Wave:
"""Represents a discrete-time waveform.
"""
"""Represents a discrete-time waveform."""

def __init__(self, ys, ts=None, framerate=None):
"""Initializes the wave.
Expand Down Expand Up @@ -847,8 +843,7 @@ def apodize(self, denom=20, duration=0.1):
self.ys = apodize(self.ys, self.framerate, denom, duration)

def hamming(self):
"""Apply a Hamming window to the wave.
"""
"""Apply a Hamming window to the wave."""
self.ys *= np.hamming(len(self.ys))

def window(self, window):
Expand All @@ -874,8 +869,7 @@ def shift(self, shift):
self.ts += shift

def roll(self, roll):
"""Rolls this wave by the given number of locations.
"""
"""Rolls this wave by the given number of locations."""
self.ys = np.roll(self.ys, roll)

def truncate(self, n):
Expand All @@ -902,8 +896,7 @@ def normalize(self, amp=1.0):
self.ys = normalize(self.ys, amp=amp)

def unbias(self):
"""Unbiases the signal.
"""
"""Unbiases the signal."""
self.ys = unbias(self.ys)

def find_index(self, t):
Expand Down Expand Up @@ -962,8 +955,7 @@ def make_spectrum(self, full=False):
return Spectrum(hs, fs, self.framerate, full)

def make_dct(self):
"""Computes the DCT of this wave.
"""
"""Computes the DCT of this wave."""
N = len(self.ys)
hs = scipy.fftpack.dct(self.ys, type=2)
fs = (0.5 + np.arange(N)) / 2
Expand Down Expand Up @@ -1017,9 +1009,7 @@ def plot(self, **options):
plt.plot(self.ts * xfactor, np.real(self.ys), **options)

def plot_vlines(self, **options):
"""Plots the wave with vertical lines for samples.
"""
"""Plots the wave with vertical lines for samples."""
xfactor = self.get_xfactor(options)
plt.vlines(self.ts * xfactor, 0, self.ys, **options)

Expand Down Expand Up @@ -1097,8 +1087,7 @@ def play(self, filename="sound.wav"):
play_wave(filename)

def make_audio(self):
"""Makes an IPython Audio object.
"""
"""Makes an IPython Audio object."""
audio = Audio(data=self.ys.real, rate=self.framerate)
return audio

Expand Down Expand Up @@ -1480,7 +1469,7 @@ def evaluate(self, ts):
ts = np.asarray(ts)
cycles = self.freq * ts + self.offset / PI2
frac, _ = np.modf(cycles)
ys = frac ** 2 * (1 - frac)
ys = frac**2 * (1 - frac)
ys = normalize(unbias(ys), self.amp)
return ys

Expand Down Expand Up @@ -1536,6 +1525,7 @@ def evaluate(self, ts):
returns: float wave array
"""

def interpolate(ts, f0, f1):
t0, t1 = ts[0], ts[-1]
return f0 + (f1 - f0) * (ts - t0) / (t1 - t0)
Expand Down Expand Up @@ -1764,7 +1754,7 @@ def midi_to_freq(midi_num):
returns: float frequency in Hz
"""
x = (midi_num - 69) / 12.0
freq = 440.0 * 2 ** x
freq = 440.0 * 2**x
return freq


Expand Down Expand Up @@ -1893,7 +1883,6 @@ def underride(d, **options):


def main():

cos_basis = cos_wave(440)
sin_basis = sin_wave(440)

Expand Down

0 comments on commit 9aaf8c9

Please sign in to comment.