public final class

GaussianFunction

extends java.lang.Object

implements ConvolutionFunction1D

 java.lang.Object

↳androidx.media3.effect.GaussianFunction

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-effect', version: '1.5.0-alpha01'

  • groupId: androidx.media3
  • artifactId: media3-effect
  • version: 1.5.0-alpha01

Artifact androidx.media3:media3-effect:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Implementation of a symmetric Gaussian function with a limited domain.

The half-width of the domain is sigma times numStdDev. Values strictly outside of that range are zero.

Summary

Constructors
publicGaussianFunction(float sigma, float numStandardDeviations)

Creates an instance.

Methods
public floatdomainEnd()

public floatdomainStart()

public booleanequals(java.lang.Object o)

public inthashCode()

public floatvalue(float samplePosition)

from java.lang.Objectclone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait

Constructors

public GaussianFunction(float sigma, float numStandardDeviations)

Creates an instance.

Parameters:

sigma: The one standard deviation, in pixels.
numStandardDeviations: The half-width of function domain, measured in the number of standard deviations.

Methods

public float domainStart()

public float domainEnd()

public float value(float samplePosition)

public boolean equals(java.lang.Object o)

public int hashCode()

Source

/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.effect;

import static androidx.media3.common.util.Assertions.checkArgument;
import static java.lang.Math.PI;
import static java.lang.Math.exp;
import static java.lang.Math.sqrt;

import androidx.annotation.FloatRange;
import androidx.annotation.Nullable;
import androidx.media3.common.util.UnstableApi;
import java.util.Objects;

/**
 * Implementation of a symmetric Gaussian function with a limited domain.
 *
 * <p>The half-width of the domain is {@code sigma} times {@code numStdDev}. Values strictly outside
 * of that range are zero.
 */
@UnstableApi
public final class GaussianFunction implements ConvolutionFunction1D {
  private final float sigma;
  private final float numStdDev;

  /**
   * Creates an instance.
   *
   * @param sigma The one standard deviation, in pixels.
   * @param numStandardDeviations The half-width of function domain, measured in the number of
   *     standard deviations.
   */
  public GaussianFunction(
      @FloatRange(from = 0.0, fromInclusive = false) float sigma,
      @FloatRange(from = 0.0, fromInclusive = false) float numStandardDeviations) {
    checkArgument(sigma > 0 && numStandardDeviations > 0);
    this.sigma = sigma;
    this.numStdDev = numStandardDeviations;
  }

  @Override
  public float domainStart() {
    return -numStdDev * sigma;
  }

  @Override
  public float domainEnd() {
    return numStdDev * sigma;
  }

  @Override
  public float value(float samplePosition) {
    if (Math.abs(samplePosition) > numStdDev * sigma) {
      return 0.0f;
    }
    float samplePositionOverSigma = samplePosition / sigma;
    return (float)
        (exp(-samplePositionOverSigma * samplePositionOverSigma / 2) / sqrt(2 * PI) / sigma);
  }

  @Override
  public boolean equals(@Nullable Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof GaussianFunction)) {
      return false;
    }
    GaussianFunction that = (GaussianFunction) o;
    return Float.compare(that.sigma, sigma) == 0 && Float.compare(that.numStdDev, numStdDev) == 0;
  }

  @Override
  public int hashCode() {
    return Objects.hash(sigma, numStdDev);
  }
}