<jupyter_start><jupyter_text>---Let’s apply encode the waveform.<jupyter_code>transformed = torchaudio.transforms.MuLawEncoding()(waveform)

print(f"Shape of transformed waveform: {transformed.size()}" )

pyplot.figure()
pyplot.plot(transformed[0,:].numpy())<jupyter_output><empty_output><jupyter_text>---And now decode.<jupyter_code>reconstructed = torchaudio.transforms.MuLawDecoding()(transformed)

print(f"Shape of recovered waveform: {reconstructed.size()}" )

pyplot.figure()
pyplot.plot(reconstructed[0,:].numpy())<jupyter_output><empty_output><jupyter_text>---We can finally compare the original waveform with its reconstructed version.<jupyter_code># Compute median relative difference
err = ((waveform-reconstructed).abs() / waveform.abs()).median()

print(f"Median relative difference between original and MuLaw reconstucted signals: {err:.2%}" )
######################################################################
# Functional
# ---------------
#
# The transformations seen above rely on lower level stateless functions for their computations.
# These functions are available under ``torchaudio.functional``. The complete list is available
# `here <https://pytorch.org/audio/functional.html>`_ and includes:
#
# -  **istft**: Inverse short time Fourier Transform.
# -  **gain**: Applies amplification or attenuation to the whole waveform.
# -  **dither**: Increases the perceived dynamic range of audio stored at a
#    particular bit-depth.
# -  **compute_deltas**: Compute delta coefficients of a tensor.
# -  **equalizer_biquad**: Design biquad peaking equalizer filter and perform filtering.
# -  **lowpass_biquad**: Design biquad lowpass filter and perform filtering.
# -  **highpass_biquad**:Design biquad highpass filter and perform filtering.
#
# For example, let's try the `mu_law_encoding` functional:

mu_law_encoding_waveform = torchaudio.functional.mu_law_encoding(waveform, quantization_channels=256)

print(f"Shape of transformed waveform: {mu_law_encoding_waveform.size()}" )

pyplot.figure()
pyplot.plot(mu_law_encoding_waveform[0,:].numpy())
######################################################################
# You can see how the output from ``torchaudio.functional.mu_law_encoding`` is the same as
# the output from ``torchaudio.transforms.MuLawEncoding``.
#
# Now let's experiment with a few of the other functionals and visualize their output. Taking our
# spectogram, we can compute it's deltas:

computed = torchaudio.functional.compute_deltas(specgram.contiguous(), win_length=3)
print(f"Shape of computed deltas: {computed.shape}" )

pyplot.figure()
pyplot.imshow(computed.log2()[0,:,:].detach().numpy(), cmap='gray')
######################################################################
# We can take the original waveform and apply different effects to it.
#

gain_waveform = torchaudio.functional.gain(waveform, gain_db=5.0)
print(f"Min of gain_waveform: {gain_waveform.min()}\nMax of gain_waveform: {gain_waveform.max()}\nMean of gain_waveform: {gain_waveform.mean()}" )

dither_waveform = torchaudio.functional.dither(waveform)
print(f"Min of dither_waveform: {dither_waveform.min()}\nMax of dither_waveform: {dither_waveform.max()}\nMean of dither_waveform: {dither_waveform.mean()}" )
######################################################################
# Another example of the capabilities in ``torchaudio.functional`` are applying filters to our
# waveform. Applying the lowpass biquad filter to our waveform will output a new waveform with
# the signal of the frequency modified.

lowpass_waveform = torchaudio.functional.lowpass_biquad(waveform, sample_rate, cutoff_freq=3000)

print(f"Min of lowpass_waveform: {lowpass_waveform.min()}\nMax of lowpass_waveform: {lowpass_waveform.max()}\nMean of lowpass_waveform: {lowpass_waveform.mean()}" )

pyplot.figure()
pyplot.plot(lowpass_waveform.t().numpy())

######################################################################
# We can also visualize a waveform with the highpass biquad filter.
#

highpass_waveform = torchaudio.functional.highpass_biquad(waveform, sample_rate, cutoff_freq=2000)

print(f"Min of highpass_waveform: {highpass_waveform.min()}\nMax of highpass_waveform: {highpass_waveform.max()}\nMean of highpass_waveform: {highpass_waveform.mean()}" )

pyplot.figure()
pyplot.plot(highpass_waveform.t().numpy())
######################################################################
# Migrating to torchaudio from Kaldi
# ----------------------------------
#
# Users may be familiar with
# `Kaldi <http://github.com/kaldi-asr/kaldi>`_, a toolkit for speech
# recognition. ``torchaudio`` offers compatibility with it in
# ``torchaudio.kaldi_io``. It can indeed read from kaldi scp, or ark file
# or streams with:
#
# -  read_vec_int_ark
# -  read_vec_flt_scp
# -  read_vec_flt_arkfile/stream
# -  read_mat_scp
# -  read_mat_ark
#
# ``torchaudio`` provides Kaldi-compatible transforms for ``spectrogram``,
# ``fbank``, ``mfcc``, and ``resample_waveform with the benefit of GPU support, see
# `here <compliance.kaldi.html>`__ for more information.
#

n_fft = 400.0
frame_length = n_fft / sample_rate * 1000.0
frame_shift = frame_length / 2.0

params = {
    "channel": 0,
    "dither": 0.0,
    "window_type": "hanning",
    "frame_length": frame_length,
    "frame_shift": frame_shift,
    "remove_dc_offset": False,
    "round_to_power_of_two": False,
    "sample_frequency": sample_rate,
}

specgram = torchaudio.compliance.kaldi.spectrogram(waveform, **params)

print(f"Shape of spectrogram: {specgram.size()}" )

pyplot.figure()
pyplot.imshow(specgram.t().numpy(), cmap='gray')
######################################################################
# We also support computing the filterbank features from waveforms,
# matching Kaldi’s implementation.
#

fbank = torchaudio.compliance.kaldi.fbank(waveform, **params)

print(f"Shape of fbank: {fbank.size()}" )

pyplot.figure()
pyplot.imshow(fbank.t().numpy(), cmap='gray')
######################################################################
# You can create mel frequency cepstral coefficients from a raw audio signal
# This matches the input/output of Kaldi’s compute-mfcc-feats.
#

mfcc = torchaudio.compliance.kaldi.mfcc(waveform, **params)

print(f"Shape of mfcc: {mfcc.size()}" )

pyplot.figure()
pyplot.imshow(mfcc.t().numpy(), cmap='gray')
######################################################################
# Available Datasets
# -----------------
#
# If you do not want to create your own dataset to train your model, ``torchaudio`` offers a
# unified dataset interface. This interface supports lazy-loading of files to memory, download
# and extract functions, and datasets to build models.
#
# The datasets ``torchaudio`` currently supports are:
#
# -  **VCTK**: Speech data uttered by 109 native speakers of English with various accents
#    (`Read more here <https://homepages.inf.ed.ac.uk/jyamagis/page3/page58/page58.html>`_).
# -  **Yesno**: Sixty recordings of one individual saying yes or no in Hebrew; each
#    recording is eight words long (`Read more here <https://www.openslr.org/1/>`_).
# -  **Common Voice**: An open source, multi-language dataset of voices that anyone can use
#    to train speech-enabled applications (`Read more here <https://voice.mozilla.org/en/datasets>`_).
# -  **LibriSpeech**: Large-scale (1000 hours) corpus of read English speech (`Read more here <http://www.openslr.org/12>`_).
#

yesno_data = torchaudio.datasets.YESNO('./', download=True)

# A data point in Yesno is a tuple (waveform, sample_rate, labels) where labels is a list of integers with 1 for yes and 0 for no.

# Pick data point number 3 to see an example of the the yesno_data:
n = 3
waveform, sample_rate, labels = yesno_data[n]

print(f"Waveform: {waveform}\nSample rate: {sample_rate}\nLabels: {labels}" )

pyplot.figure()
pyplot.plot(waveform.t().numpy())<jupyter_output>Waveform: tensor([[ 3.0518e-05,  6.1035e-05,  3.0518e-05,  ..., -1.8311e-04,
          4.2725e-04,  6.7139e-04]])
Sample rate: 8000
Labels: [0, 0, 1, 0, 0, 0, 1, 0]
{'fraction_non_alphanumeric': 0.20313471824853838, 'fraction_numerical': 0.013310113198158975, 'mean_word_length': 5.617283950617284, 'pattern_counts': {'":': 8, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '536869', 'license': 'Apache-2.0', 'n_tokens_mistral': 2652, 'n_tokens_neox': 2444, 'n_words': 660, 'path': 'samples/unsorted/yes_no_s.ipynb', 'repo_name': 'cnheider/audition', 'size': '8039'}