Last active
March 30, 2024 11:10
-
-
Save tam17aki/bc68abec0af67fedde99257f1d5cef46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# MIT License | |
# Copyright (C) 2024 by Akira TAMAMORI | |
# Permission is hereby granted, free of charge, to any person | |
# obtaining a copy of this software and associated documentation files | |
# (the Software"), to deal in the Software without restriction, | |
# including without limitation the rights to use, copy, modify, merge, | |
# publish, distribute, sublicense, and/or sell copies of the Software, | |
# and to permit persons to whom the Software is furnished to do so, | |
# subject to the following conditions: | |
# The above copyright notice and this permission notice shall be | |
# included in all copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
import librosa | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import soundfile as sf | |
from scipy import signal | |
WIN_LEN = 512 | |
HOP_LEN = 128 | |
N_FFT = WIN_LEN | |
def plot_stft_spec(stft_spec, fs, out_figfile): | |
"""Plot amplitude & phase spectrum.""" | |
fig, ax = plt.subplots(2, 1, figsize=(12, 8), sharex=True) | |
img1 = librosa.display.specshow( | |
librosa.amplitude_to_db(np.abs(stft_spec), ref=np.max), | |
sr=fs, | |
hop_length=HOP_LEN, | |
n_fft=N_FFT, | |
win_length=WIN_LEN, | |
y_axis="mel", | |
x_axis="time", | |
cmap="jet", | |
ax=ax[0], | |
fmax=8192, | |
) | |
ax[0].set_xlabel("") | |
ax[0].set_ylabel("Frequency [Hz]") | |
cbar = fig.colorbar( | |
img1, | |
ax=ax[0], | |
format="%+2.f", | |
pad=0.02, | |
fraction=0.02, | |
) | |
cbar.set_label("Amplitude [dB]", rotation=270, labelpad=15) | |
pos2 = librosa.display.specshow( | |
np.angle(stft_spec), | |
sr=fs, | |
hop_length=HOP_LEN, | |
n_fft=N_FFT, | |
win_length=WIN_LEN, | |
y_axis="mel", | |
x_axis="time", | |
cmap="hsv", | |
ax=ax[1], | |
fmax=8192, | |
) | |
ax[1].set_xlabel("Time (s)") | |
ax[1].set_ylabel("Frequency [Hz]") | |
cbar = fig.colorbar( | |
pos2, ax=ax[1], ticks=[-np.pi + 0.0001, 0, np.pi], pad=0.02, fraction=0.02 | |
) | |
cbar.ax.set_yticklabels(["-$\pi$", "0", "$\pi$"]) | |
cbar.set_label("Phase [rad]", rotation=270, labelpad=15) | |
plt.tight_layout() | |
plt.savefig(out_figfile) | |
plt.show() | |
def main(wav_file): | |
"""Plot figures.""" | |
audio, fs = sf.read(wav_file) | |
if audio.dtype in [np.int16, np.int32]: | |
audio = (audio / np.iinfo(audio.dtype).max).astype(np.float64) | |
audio = audio.astype(np.float64) | |
stft_old_scipy = signal.stft( | |
audio, fs=fs, nperseg=WIN_LEN, noverlap=(WIN_LEN - HOP_LEN), nfft=WIN_LEN | |
) | |
plot_stft_spec(stft_old_scipy[-1], fs, "scipy_spec.png") | |
SFT = signal.ShortTimeFFT( | |
win=signal.windows.hann(WIN_LEN), | |
hop=HOP_LEN, | |
fs=fs, | |
mfft=WIN_LEN, | |
phase_shift=0, | |
) | |
stft_scipy = SFT.stft(audio) | |
plot_stft_spec(stft_scipy, fs, "scipy_spec_phaseshift_0.png") | |
SFT = signal.ShortTimeFFT( | |
win=signal.windows.hann(WIN_LEN), | |
hop=HOP_LEN, | |
fs=fs, | |
mfft=WIN_LEN, | |
phase_shift=None, | |
) | |
stft_scipy = SFT.stft(audio) | |
plot_stft_spec(stft_scipy, fs, "scipy_spec_phaseshift_none.png") | |
if __name__ == "__main__": | |
main(wav_file="./ONOMATOPEE300_001.wav") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment