Advertisement
Guest User

Untitled

a guest
May 26th, 2023
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import librosa
  5.  
  6. from ssqueezepy import ssq_cwt, ssq_stft, stft, cwt, Wavelet
  7. from ssqueezepy.experimental import scale_to_freq
  8. from ssqueezepy.visuals import plot, imshow
  9.  
  10.  
  11. IDX = 0
  12. path = [
  13. r"vhs_noisy.mp3",
  14. r"vhs_clean.mp3",
  15. r"vhs_pure_noise.mp3",
  16. ][IDX]
  17. offset = [
  18. 0,
  19. 20*22050,
  20. 0
  21. ][IDX]
  22. ecwt_factor = [
  23. 1/500,
  24. 1/500,
  25. 1/500,
  26. ][IDX]
  27. zoom_size = [
  28. 16384*2,
  29. 16384*2,
  30. 16384*2
  31. ][IDX]
  32. last_norm_scaling = [
  33. .7,
  34. .7,
  35. .5,
  36. ][IDX]
  37. second_norm_scaling = [
  38. .3,
  39. .4,
  40. 1,
  41. ][IDX]
  42.  
  43. #%%
  44. x, fs = librosa.load(path)
  45. print(len(x), fs)
  46.  
  47. N = len(x)
  48. t = np.linspace(0, N/fs, N, 0)
  49.  
  50. zoom = zoom_size
  51. x_slc = x[offset:offset+zoom]
  52. t_slc = t[offset:offset+zoom]
  53.  
  54. #%%
  55. wavelet = Wavelet()
  56. Tsx, Sx, tsx_freqs, *_ = ssq_stft(x_slc-x_slc.mean(), fs=fs, n_fft=2048, window='hann')
  57. Twx, Wx, twx_freqs, *_ = ssq_cwt(x_slc, wavelet, fs=fs, scales='log')
  58. Tsx, Sx = Tsx[::-1], Sx[::-1]
  59. Tsxo, Sxo = Tsx.copy(), Sx.copy()
  60. Twxo, Wxo = Twx.copy(), Wx.copy()
  61.  
  62. #%%
  63. escale = np.linspace(0, 1, len(Tsx), 1)[:, None][::-1]
  64. escale_cwt = np.logspace(np.log10(ecwt_factor), np.log10(1), len(Wx), 1, base=10
  65. )[:, None][::-1]
  66. Tsx, Sx, Twx, Wx = [g.copy() for g in (Tsx, Sx, Twx, Wx)]
  67. Tsx, Sx = Tsxo * escale, np.log10(1 + Sxo * escale)
  68. Twx, Wx = Twxo * escale_cwt, Wxo * escale_cwt
  69.  
  70. #%%
  71. fig, axes = plt.subplots(5, 1, layout='constrained', figsize=(7, 30))
  72. pkw = dict(fig=fig)
  73. ikw = dict(abs=1, fig=fig, show=0, xticks=t_slc)
  74.  
  75. # plot(t, x, **pkw, ax=axes[0])
  76. plot(t_slc, x_slc, **pkw, ax=axes[0])
  77. imshow(Sxo, **ikw, ax=axes[1], title="|STFT|", yticks=tsx_freqs[::-1])
  78. imshow(Sx, **ikw, ax=axes[2], title="|STFT|, 1/f^2 energy-scaled",
  79. yticks=tsx_freqs[::-1], norm_scaling=second_norm_scaling)
  80. imshow(Wxo, **ikw, ax=axes[3], title="|CWT|", yticks=twx_freqs)
  81. imshow(Wx, **ikw, ax=axes[4], title="|CWT|, 1/f^2 energy-scaled",
  82. yticks=twx_freqs, xlabel="time [sec]", norm_scaling=last_norm_scaling)
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement