|
7 | 7 |
|
8 | 8 | """
|
9 | 9 |
|
10 |
| -# When running this tutorial in Google Colab, install the required packages |
11 |
| -# with the following. |
12 |
| -# !pip install torchaudio librosa |
13 |
| - |
14 | 10 | import torch
|
15 | 11 | import torchaudio
|
16 | 12 | import torchaudio.transforms as T
|
| 13 | +import numpy as np |
17 | 14 |
|
18 | 15 | print(torch.__version__)
|
19 | 16 | print(torchaudio.__version__)
|
|
23 | 20 | # -----------
|
24 | 21 | #
|
25 | 22 |
|
26 |
| -import librosa |
27 | 23 | import matplotlib.pyplot as plt
|
28 | 24 | from IPython.display import Audio
|
29 | 25 | from torchaudio.utils import download_asset
|
@@ -98,10 +94,16 @@ def get_spectrogram(
|
98 | 94 | ######################################################################
|
99 | 95 | # Visualization
|
100 | 96 | # ~~~~~~~~~~~~~
|
| 97 | + |
| 98 | +def power_to_db(S): |
| 99 | + S = np.asarray(S) |
| 100 | + return 10.0 * np.log10(np.maximum(1e-10, S)) |
| 101 | + |
| 102 | + |
101 | 103 | def plot():
|
102 | 104 | def plot_spec(ax, spec, title):
|
103 | 105 | ax.set_title(title)
|
104 |
| - ax.imshow(librosa.amplitude_to_db(spec), origin="lower", aspect="auto") |
| 106 | + ax.imshow(power_to_db(spec**2), origin="lower", aspect="auto") |
105 | 107 |
|
106 | 108 | fig, axes = plt.subplots(3, 1, sharex=True, sharey=True)
|
107 | 109 | plot_spec(axes[0], torch.abs(spec_12[0]), title="Stretched x1.2")
|
@@ -157,7 +159,7 @@ def preview(spec, rate=16000):
|
157 | 159 | def plot():
|
158 | 160 | def plot_spec(ax, spec, title):
|
159 | 161 | ax.set_title(title)
|
160 |
| - ax.imshow(librosa.power_to_db(spec), origin="lower", aspect="auto") |
| 162 | + ax.imshow(power_to_db(spec), origin="lower", aspect="auto") |
161 | 163 |
|
162 | 164 | fig, axes = plt.subplots(3, 1, sharex=True, sharey=True)
|
163 | 165 | plot_spec(axes[0], spec[0], title="Original")
|
|
0 commit comments