Skip to content

random_sine_wave

Random Sine Wave Layer API

This module provides classes to build random sine wave layers.

Classes:

Classes

RandomSineWave

RandomSineWave(sample_rate: float = 1, frequency: float | tuple[float, float] = 100, amplitude: float | tuple[float, float] = 0.1, **kwargs)

Adds a sine wave to the input.

Parameters:

  • sample_rate (float, default: 1 ) –

    Sample rate of the input.

  • frequency (float | tuple[float, float], default: 100 ) –

    Frequency of the wave in Hz. If tuple, frequency is randomly picked between the values.

  • amplitude (float | tuple[float, float], default: 0.1 ) –

    Amplitude of the wave. If tuple, amplitude is randomly picked between the values.

Source code in neuralspot_edge/layers/preprocessing/random_sine_wave.py
def __init__(
    self,
    sample_rate: float = 1,
    frequency: float | tuple[float, float] = 100,
    amplitude: float | tuple[float, float] = 0.1,
    **kwargs,
):
    """Adds a sine wave to the input.

    Args:
        sample_rate (float): Sample rate of the input.
        frequency (float|tuple[float,float]): Frequency of the wave in Hz. If tuple, frequency is randomly picked between the values.
        amplitude (float|tuple[float,float]): Amplitude of the wave. If tuple, amplitude is randomly picked between the values.
    """
    super().__init__(**kwargs)
    if sample_rate < 0:
        raise ValueError("sample_rate must be greater than 0")
    self.sample_rate = sample_rate
    self.frequency = parse_factor(frequency, min_value=0, max_value=sample_rate / 2, param_name="frequency")
    self.amplitude = parse_factor(amplitude, min_value=0, max_value=None, param_name="amplitude")

Functions

get_random_transformations
get_random_transformations(input_shape: tuple[int, int, int]) -> dict

Generate noise distortion tensor

Parameters:

  • input_shape (tuple[int, ...]) –

    Input shape.

Returns:

  • dict ( dict ) –

    Dictionary containing the noise tensor.

Source code in neuralspot_edge/layers/preprocessing/random_sine_wave.py
def get_random_transformations(self, input_shape: tuple[int, int, int]) -> dict:
    """Generate noise distortion tensor

    Args:
        input_shape (tuple[int, ...]): Input shape.

    Returns:
        dict: Dictionary containing the noise tensor.
    """
    batch_size = input_shape[0]

    frequencies = keras.random.uniform(
        shape=(batch_size,), minval=self.frequency[0], maxval=self.frequency[1], seed=self.random_generator
    )
    amplitudes = keras.random.uniform(
        shape=(batch_size,), minval=self.amplitude[0], maxval=self.amplitude[1], seed=self.random_generator
    )

    return {"frequency": frequencies, "amplitude": amplitudes}
augment_sample
augment_sample(inputs)

Augment single sample with sine wave.

Source code in neuralspot_edge/layers/preprocessing/random_sine_wave.py
def augment_sample(self, inputs):
    """Augment single sample with sine wave."""
    sample = inputs[self.SAMPLES]
    duration_size = sample.shape[self.data_axis]
    sample_rate = keras.ops.cast(self.sample_rate, dtype=self.compute_dtype)
    if self.training:
        frequency = inputs[self.TRANSFORMS]["frequency"]
        amplitude = inputs[self.TRANSFORMS]["amplitude"]
        ts = keras.ops.arange(duration_size, dtype=self.compute_dtype) / sample_rate
        sine_wave = keras.ops.sin(2 * np.pi * frequency * ts)
        sine_wave = amplitude * sine_wave
        sine_wave = keras.ops.reshape(sine_wave, sample.shape)
        return sample + sine_wave
    return sample