Skip to content

Commit

Permalink
add logmel spec
Browse files Browse the repository at this point in the history
  • Loading branch information
pme0 committed Jun 20, 2023
1 parent 27e9216 commit 0a2afc1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
20 changes: 19 additions & 1 deletion deeplightning/model/dcgan.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def __init__(self, num_channels, latent_dim):
nn.ConvTranspose2d(in_channels=32, out_channels=num_channels, kernel_size=4, stride=2, padding=1, bias=False),
nn.Tanh(),
)
""" Parametrize layer creation:
last_channels = 32
channels = [latent_dim] + [last_channels * (2 ** i) for i in range(num_layers-1)][::-1] + [num_channels]
layers = []
for i in range(num_layers):
layers.append(nn.ConvTranspose2d(in_channels=channels[i], out_channels=channels[i+1], kernel_size=4, stride=2, padding=0, bias=False))
layers.append(nn.BatchNorm2d(channels[i+1]))
layers.append(nn.ReLU() if i < num_layers-1 else nn.Tanh())
"""

def forward(self, x):
return self.generator(x)
Expand Down Expand Up @@ -89,7 +98,16 @@ def tensor_size_debugger(self):

class DCGAN(nn.Module):
def __init__(self, batch_size: int, sample_size: int, image_size: int, latent_dim: int):
"""
"""DCGAN: Deep Convolutional Generative Adversarial Network.
Parameters
----------
???
References
----------
> A Radford, L Metz, S Chintala (2015) "Unsupervised representation learning with
deep convolutional generative adversarial networks", arXiv:1511.06434
"""
super(DCGAN, self).__init__()

Expand Down
23 changes: 13 additions & 10 deletions deeplightning/viz/audio/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,26 @@ def spectrogram(
scale: str = None,
n_fft: int = 2048,
hop_length: int = 512,
figsize: tuple = (5,5),
figsize: tuple = (8,3),
save_plot: str = None,
show_plot: bool = True,
x_axis: str = "time",
):
"""Display Mel Frequency Cepstral Coefficients.
Parameters
----------
path : path to the audio file.
mode : the type of features to be shown in the spectrogram.
Can be Short Time Fourier Transform (STFT) amplitude
(`stft_ampl`) or decibels (`stft_db`); or Mel Frequency
Cepstral Coefficients (MFCC)
(`stft_ampl`) or decibels (`stft_db`); Log Mel spectrogram
(logmel); Mel Frequency Cepstral Coefficients (`mfcc`)
scale : the y-axis scale. Can be `linear` or `log`. For
`mode == "mfcc"` the scale is chosen automatically
n_fft :
hop_length :
figsize : the figure size
x_axis :
"""

assert scale is None or scale in ["linear", "log"]
Expand Down Expand Up @@ -121,12 +117,19 @@ def spectrogram(
frequency_type = 'MFCC'
colorbar_label = 'coefficients'
scale_type = ''
elif mode == "logmel":
S = librosa.feature.melspectrogram(y=signal, sr=sample_rate, n_fft=n_fft, hop_length=hop_length)
S = librosa.power_to_db(S, ref=np.max)
specshow(data = S, x_axis = x_axis, y_axis = 'mel', sr = sample_rate, hop_length=hop_length)
frequency_type = 'Log Mel'
colorbar_label = 'decibel (dB)'
scale_type = ''
else:
raise NotImplementedError

plt.xlabel("Time")
plt.ylabel(f"Frequency (Hz)")
plt.colorbar(label=colorbar_label, orientation="horizontal")
plt.colorbar(label=colorbar_label, orientation="vertical", pad=0.02)
plt.title("Spectrogram ({}{})".format(
frequency_type if frequency_type is not None else '',
scale_type if scale_type is not None else '',
Expand Down
9 changes: 7 additions & 2 deletions deeplightning/viz/audio/wave.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Union, Tuple
import matplotlib.pyplot as plt
import numpy as np
import librosa
Expand All @@ -9,7 +10,9 @@ def waveplot(
x_axis: str = "time",
save_plot: str = None,
show_plot: bool = True,
figsize=(8,4)
figsize: Tuple[int,int] = (8,3),
title: Union[str,None] = None,

):
"""Display waveform in the time domain.
Expand All @@ -33,7 +36,9 @@ def waveplot(
# plot
plt.figure(figsize=figsize)
waveshow(signal, sr=sample_rate, x_axis=x_axis)
plt.title("Audio waveplot")
plt.ylabel("Amplitude")
if title:
plt.title(title)

if save_plot is not None:
plt.savefig(save_plot, bbox_inches='tight')
Expand Down

0 comments on commit 0a2afc1

Please sign in to comment.