From 9aaf8c90020f1fbbd99627472b47fa0a93714f39 Mon Sep 17 00:00:00 2001 From: Allen Downey Date: Tue, 1 Aug 2023 20:52:45 -0400 Subject: [PATCH] Formatted with black --- code/thinkdsp.py | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/code/thinkdsp.py b/code/thinkdsp.py index cc7e49dd..2506a6cb 100644 --- a/code/thinkdsp.py +++ b/code/thinkdsp.py @@ -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. @@ -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. @@ -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) @@ -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) @@ -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): @@ -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. @@ -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): @@ -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): @@ -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): @@ -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 @@ -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) @@ -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 @@ -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 @@ -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) @@ -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 @@ -1893,7 +1883,6 @@ def underride(d, **options): def main(): - cos_basis = cos_wave(440) sin_basis = sin_wave(440)