1 /* 2 * Copyright (c) 2024, Alliance for Open Media. All rights reserved 3 * 4 * This source code is subject to the terms of the BSD 3-Clause Clear License 5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear 6 * License was not distributed with this source code in the LICENSE file, you 7 * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the 8 * Alliance for Open Media Patent License 1.0 was not distributed with this 9 * source code in the PATENTS file, you can obtain it at 10 * www.aomedia.org/license/patent. 11 */ 12 // TODO(b/400635711): Use the one in the obr library once it is open-sourced. 13 #ifndef CLI_AMBISONIC_ENCODER_ASSOCIATED_LEGENDRE_POLYNOMIALS_GENERATOR_H_ 14 #define CLI_AMBISONIC_ENCODER_ASSOCIATED_LEGENDRE_POLYNOMIALS_GENERATOR_H_ 15 16 #include <cstddef> 17 #include <vector> 18 19 // This code is forked from Resonance Audio's 20 // `associated_legendre_polynomials_generator.h`. 21 namespace iamf_tools { 22 23 // Generates associated Legendre polynomials. 24 class AssociatedLegendrePolynomialsGenerator { 25 public: 26 /*!\brief Constructs a generator for associated Legendre polynomials (ALP). 27 * 28 * \param max_degree Maximum ALP degree supported by this generator. 29 * \param condon_shortley_phase Whether the Condon-Shortley phase, (-1)^order, 30 * should be included in the polynomials generated. 31 * \param compute_negative_order Whether this generator should compute 32 * negative-ordered polynomials. 33 */ 34 AssociatedLegendrePolynomialsGenerator(int max_degree, 35 bool condon_shortley_phase, 36 bool compute_negative_order); 37 38 /*!\brief Generates the associated Legendre polynomials for the given |x|. 39 * 40 * \param x Abscissa (the polynomials' variable). 41 * \return Output vector of computed sequence values. 42 */ 43 std::vector<float> Generate(float x) const; 44 45 /*!\brief Gets the produced number of associated Legendre polynomials. 46 * 47 * \return Number of associated Legendre polynomials this generator 48 * produces. 49 */ 50 size_t GetNumValues() const; 51 52 /*!\brief Gets the index into the output vector, given |degree| and |order|. 53 * 54 * \param degree Polynomial's degree. 55 * \param order Polynomial's order. 56 * \return Index into the vector of computed values corresponding to the 57 * specified ALP. 58 */ 59 size_t GetIndex(int degree, int order) const; 60 61 private: 62 /*!\brief Computes the ALP for (degree, order) the given |x|. 63 * 64 * ALP is computed using recurrence relations. It is assumed that the ALPs 65 * necessary for each computation are already computed and stored in `values`. 66 * 67 * \param degree Degree of the polynomial being computed. 68 * \param degree Order of the polynomial being computed. 69 * \param values Previously computed values. 70 * \return Computed polynomial. 71 */ 72 inline float ComputeValue(int degree, int order, float x, 73 const std::vector<float>& values) const; 74 75 /*!\brief Checks the validity of the given index. 76 * 77 * \param degree The polynomial's degree. 78 * \param order The polynomial's order. 79 */ 80 inline void CheckIndexValidity(int degree, int order) const; 81 82 // The maximum polynomial degree that can be computed; must be >= 0. 83 const int max_degree_; 84 85 // Whether the Condon-Shortley phase, (-1)^order, should be included in the 86 // polynomials generated. 87 const bool condon_shortley_phase_; 88 89 // Whether this generator should compute negative-ordered polynomials. 90 const bool compute_negative_order_; 91 }; 92 93 } // namespace iamf_tools 94 95 #endif // CLI_AMBISONIC_ENCODER_ASSOCIATED_LEGENDRE_POLYNOMIALS_GENERATOR_H_ 96