Source code for VuVoPy.data.containers.prepocessing

import numpy as np
import matplotlib.pyplot as plt
#from .sample import VoiceSample
from VuVoPy.data.containers.sample import VoiceSample

[docs] class Preprocessed(VoiceSample): """ The `Preprocessed` class represents a preprocessed version of a voice sample, extending the `VoiceSample` class. It includes functionality for normalization and pre-emphasis of the waveform. Attributes: x (numpy.ndarray): The original waveform of the voice sample. fs (int): The sampling rate of the voice sample. xnorm (numpy.ndarray): The normalized waveform. Defaults to the original waveform if not provided. preem (numpy.ndarray): The pre-emphasized waveform. Defaults to the original waveform if not provided. alpha (float): The pre-emphasis coefficient. Defaults to 0.94. Methods: from_voice_sample(cls, voice_sample, alpha=0.94): Creates a `Preprocessed` object from a `VoiceSample` object by applying normalization and pre-emphasis. get_preemphasis(alpha=None): Returns the pre-emphasized waveform as a NumPy array. If an `alpha` value is provided, it applies pre-emphasis with the given coefficient. get_normalization(): Returns the normalized waveform as a NumPy array. get_waveform(): Returns the original waveform as a NumPy array. get_sampling_rate(): Returns the sampling rate of the voice sample. """ def __init__(self, x, fs, xnorm, preem, alpha=0.94): super().__init__(x, fs) self.xnorm = xnorm if xnorm is not None else x # Default to x if not provided self.preem = preem if preem is not None else x # Default to x if not provided self.alpha = alpha
[docs] @classmethod def from_voice_sample(cls, voice_sample, alpha=0.94): """Apply normalization and pre-emphasis to a VoiceSample and return a Preprocessed object.""" x = voice_sample.get_waveform() fs = voice_sample.get_sampling_rate() # Apply pre-emphasis x_preem = np.append(x[0], x[1:] - alpha * x[:-1]) # Apply normalization x_norm = x / np.max(np.abs(x)) if np.max(np.abs(x)) > 0 else x return cls(x, fs, xnorm=x_norm, preem=x_preem, alpha = alpha)
[docs] def get_preemphasis(self, alpha = None): """Return the waveform with applied pre-emphasis as a NumPy array.""" if alpha is None: return self.preem return np.append(self.x[0], self.x[1:] - alpha * self.x[:-1])
[docs] def get_normalization(self): """Return the normalized waveform as a NumPy array.""" return self.xnorm
[docs] def get_waveform(self): """Return the waveform as a NumPy array.""" return self.x
[docs] def get_sampling_rate(self): """Return the sampling rate.""" return self.fs