alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2021-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #ifndef PLATFORM_MATH_HPP |
| 18 | #define PLATFORM_MATH_HPP |
| 19 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | /* See if ARM DSP functions can be used. */ |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 21 | #if (defined(__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1)) |
| 22 | #include "arm_math.h" |
| 23 | #define M_PI (PI) |
| 24 | #else /* (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1)) */ |
| 25 | #include <cmath> |
| 26 | #endif /* (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1)) */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 27 | |
| 28 | #include <vector> |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 29 | #include <cstdint> |
| 30 | #include <numeric> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 31 | |
| 32 | namespace arm { |
| 33 | namespace app { |
| 34 | namespace math { |
| 35 | |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 36 | enum class FftType { |
| 37 | real = 0, |
| 38 | complex = 1 |
| 39 | }; |
| 40 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 41 | struct FftInstance { |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 42 | #if (defined(__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1)) |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 43 | arm_rfft_fast_instance_f32 m_instanceReal; |
| 44 | arm_cfft_instance_f32 m_instanceComplex; |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 45 | #endif /* (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1)) */ |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 46 | uint16_t m_fftLen{0}; |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 47 | FftType m_type{FftType::real}; |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 48 | bool m_optimisedOptionAvailable{false}; |
| 49 | bool m_initialised{false}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | /* Class to provide Math functions like FFT, mean, stddev etc. |
| 53 | * This will allow other classes, functions to be independent of |
| 54 | * #if definition checks and provide a cleaner API. Also, it will |
| 55 | * consolidate all arm math functions used in one place and make |
| 56 | * them easier to test. */ |
| 57 | class MathUtils { |
| 58 | |
| 59 | public: |
| 60 | /** |
| 61 | * @brief Get the cosine value of the argument in floating point. |
| 62 | * @param[in] radians Angle in radians. |
| 63 | * @return Cosine value (floating point). |
| 64 | */ |
| 65 | static float CosineF32(float radians); |
| 66 | |
| 67 | /** |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 68 | * @brief Get the sine value of the argument in floating point. |
| 69 | * @param[in] radians Angle in radians. |
| 70 | * @return Sine value (floating point). |
| 71 | */ |
| 72 | static float SineF32(float radians); |
| 73 | |
| 74 | /** |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 75 | * @brief Get the square root of the argument in floating point. |
| 76 | * @param[in] input Value to compute square root of. |
| 77 | * @return Square root (floating point) value. |
| 78 | */ |
| 79 | static float SqrtF32(float input); |
| 80 | |
| 81 | /** |
| 82 | * @brief Gets the mean of a floating point array of elements. |
| 83 | * @param[in] ptrSrc Pointer to the first element. |
| 84 | * @param[in] srcLen Number of elements in the array/vector. |
| 85 | * @return Average value. |
| 86 | */ |
| 87 | static float MeanF32(float* ptrSrc, uint32_t srcLen); |
| 88 | |
| 89 | /** |
| 90 | * @brief Gets the standard deviation of a floating point array |
| 91 | * of elements. |
| 92 | * @param[in] ptrSrc Pointer to the first element. |
| 93 | * @param[in] srcLen Number of elements in the array/vector. |
| 94 | * @param[in] mean Pre-computed mean value. |
| 95 | * @return Standard deviation value. |
| 96 | */ |
| 97 | static float StdDevF32(float* ptrSrc, uint32_t srcLen, |
| 98 | float mean); |
| 99 | |
| 100 | /** |
| 101 | * @brief Initialises the internal FFT structures (if available |
| 102 | * for the platform). This function should be called |
| 103 | * prior to Fft32 function call if built with ARM DSP functions. |
| 104 | * @param[in] fftLen Requested length of the FFT. |
| 105 | * @param[in] fftInstance FFT instance struct to use. |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 106 | * @param[in] type FFT type (real or complex) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 107 | */ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 108 | static void FftInitF32(uint16_t fftLen, |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 109 | FftInstance& fftInstance, |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 110 | FftType type = FftType::real); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * @brief Computes the FFT for the input vector. |
| 114 | * @param[in] input Floating point vector of input elements |
| 115 | * @param[out] fftOutput Output buffer to be populated by computed FFTs. |
| 116 | * @param[in] fftInstance FFT instance struct to use. |
| 117 | */ |
| 118 | static void FftF32(std::vector<float>& input, |
| 119 | std::vector<float>& fftOutput, |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 120 | FftInstance& fftInstance); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * @brief Computes the natural logarithms of input floating point |
| 124 | * vector |
| 125 | * @param[in] input Floating point input vector |
| 126 | * @param[out] output Pre-allocated buffer to be populated with |
| 127 | * natural log values of each input element. |
| 128 | */ |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 129 | static void VecLogarithmF32(std::vector<float>& input, |
| 130 | std::vector<float>& output); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * @brief Computes the dot product of two 1D floating point |
| 134 | * vectors. |
| 135 | * result = sum(srcA[0]*srcB[0] + srcA[1]*srcB[1] + ..) |
| 136 | * @param[in] srcPtrA Pointer to the first element of first |
| 137 | * array. |
| 138 | * @param[in] srcPtrB Pointer to the first element of second |
| 139 | * array. |
| 140 | * @param[in] srcLen Number of elements in the array/vector. |
| 141 | * @return Dot product. |
| 142 | */ |
| 143 | static float DotProductF32(float* srcPtrA, float* srcPtrB, |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 144 | uint32_t srcLen); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * @brief Computes the squared magnitude of floating point |
| 148 | * complex number array. |
| 149 | * @param[in] ptrSrc Pointer to the first element of input |
| 150 | * array. |
| 151 | * @param[in] srcLen Number of elements in the array/vector. |
| 152 | * @param[out] ptrDst Output buffer to be populated. |
Conor Kennedy | 5cf8e74 | 2023-02-13 10:50:40 +0000 | [diff] [blame] | 153 | * @param[in] dstLen Output buffer len (for health check only). |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 154 | * @return true if successful, false otherwise. |
| 155 | */ |
| 156 | static bool ComplexMagnitudeSquaredF32(float* ptrSrc, |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 157 | uint32_t srcLen, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 158 | float* ptrDst, |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 159 | uint32_t dstLen); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 160 | |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 161 | /** |
| 162 | * @brief Scales output scores for an arbitrary number of classes so |
| 163 | * that they sum to 1, allowing output to be expressed as a probability. |
| 164 | * @param[in] vector Vector of floats modified in-place |
| 165 | */ |
| 166 | static void SoftmaxF32(std::vector<float>& vec); |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * @brief Calculate the Sigmoid function of the given value. |
| 170 | * @param[in] x Value to apply Sigmoid to. |
| 171 | * @return Sigmoid value of the input. |
| 172 | */ |
| 173 | static float SigmoidF32(float x); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 174 | }; |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 175 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 176 | } /* namespace math */ |
| 177 | } /* namespace app */ |
| 178 | } /* namespace arm */ |
| 179 | |
Kshitij Sisodia | 9c6f9f8 | 2022-05-20 14:30:02 +0100 | [diff] [blame] | 180 | #endif /* PLATFORM_MATH_HPP */ |