alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. All rights reserved. |
| 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 | |
| 20 | #include "hal.h" |
| 21 | |
| 22 | /* See if ARM DSP functions can be used. */ |
| 23 | #if PLATFORM_HAL == PLATFORM_CORTEX_M_BAREMETAL |
| 24 | #if defined(__DSP_PRESENT) && (__DSP_PRESENT == 1U) |
| 25 | |
| 26 | #define ARM_DSP_AVAILABLE (1U) |
| 27 | #include "arm_math.h" |
| 28 | #define M_PI (PI) |
| 29 | |
| 30 | #endif /* defined(__DSP_PRESENT) && (__DSP_PRESENT == 1U) */ |
| 31 | #endif /* PLATFORM_HAL == PLATFORM_CORTEX_M_BAREMETAL */ |
| 32 | |
| 33 | #include <vector> |
| 34 | |
| 35 | namespace arm { |
| 36 | namespace app { |
| 37 | namespace math { |
| 38 | |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 39 | enum class FftType { |
| 40 | real = 0, |
| 41 | complex = 1 |
| 42 | }; |
| 43 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 44 | struct FftInstance { |
| 45 | #if ARM_DSP_AVAILABLE |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 46 | arm_rfft_fast_instance_f32 m_instanceReal; |
| 47 | arm_cfft_instance_f32 m_instanceComplex; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 48 | #endif |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 49 | uint16_t m_fftLen{0}; |
| 50 | FftType m_type; |
| 51 | bool m_optimisedOptionAvailable{false}; |
| 52 | bool m_initialised{false}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /* Class to provide Math functions like FFT, mean, stddev etc. |
| 56 | * This will allow other classes, functions to be independent of |
| 57 | * #if definition checks and provide a cleaner API. Also, it will |
| 58 | * consolidate all arm math functions used in one place and make |
| 59 | * them easier to test. */ |
| 60 | class MathUtils { |
| 61 | |
| 62 | public: |
| 63 | /** |
| 64 | * @brief Get the cosine value of the argument in floating point. |
| 65 | * @param[in] radians Angle in radians. |
| 66 | * @return Cosine value (floating point). |
| 67 | */ |
| 68 | static float CosineF32(float radians); |
| 69 | |
| 70 | /** |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 71 | * @brief Get the sine value of the argument in floating point. |
| 72 | * @param[in] radians Angle in radians. |
| 73 | * @return Sine value (floating point). |
| 74 | */ |
| 75 | static float SineF32(float radians); |
| 76 | |
| 77 | /** |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 78 | * @brief Get the square root of the argument in floating point. |
| 79 | * @param[in] input Value to compute square root of. |
| 80 | * @return Square root (floating point) value. |
| 81 | */ |
| 82 | static float SqrtF32(float input); |
| 83 | |
| 84 | /** |
| 85 | * @brief Gets the mean of a floating point array of elements. |
| 86 | * @param[in] ptrSrc Pointer to the first element. |
| 87 | * @param[in] srcLen Number of elements in the array/vector. |
| 88 | * @return Average value. |
| 89 | */ |
| 90 | static float MeanF32(float* ptrSrc, uint32_t srcLen); |
| 91 | |
| 92 | /** |
| 93 | * @brief Gets the standard deviation of a floating point array |
| 94 | * of elements. |
| 95 | * @param[in] ptrSrc Pointer to the first element. |
| 96 | * @param[in] srcLen Number of elements in the array/vector. |
| 97 | * @param[in] mean Pre-computed mean value. |
| 98 | * @return Standard deviation value. |
| 99 | */ |
| 100 | static float StdDevF32(float* ptrSrc, uint32_t srcLen, |
| 101 | float mean); |
| 102 | |
| 103 | /** |
| 104 | * @brief Initialises the internal FFT structures (if available |
| 105 | * for the platform). This function should be called |
| 106 | * prior to Fft32 function call if built with ARM DSP functions. |
| 107 | * @param[in] fftLen Requested length of the FFT. |
| 108 | * @param[in] fftInstance FFT instance struct to use. |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 109 | * @param[in] type FFT type (real or complex) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 110 | */ |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 111 | static void FftInitF32(const uint16_t fftLen, |
| 112 | FftInstance& fftInstance, |
| 113 | const FftType type = FftType::real); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * @brief Computes the FFT for the input vector. |
| 117 | * @param[in] input Floating point vector of input elements |
| 118 | * @param[out] fftOutput Output buffer to be populated by computed FFTs. |
| 119 | * @param[in] fftInstance FFT instance struct to use. |
| 120 | */ |
| 121 | static void FftF32(std::vector<float>& input, |
| 122 | std::vector<float>& fftOutput, |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 123 | FftInstance& fftInstance); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * @brief Computes the natural logarithms of input floating point |
| 127 | * vector |
| 128 | * @param[in] input Floating point input vector |
| 129 | * @param[out] output Pre-allocated buffer to be populated with |
| 130 | * natural log values of each input element. |
| 131 | */ |
| 132 | static void VecLogarithmF32(std::vector <float>& input, |
| 133 | std::vector <float>& output); |
| 134 | |
| 135 | /** |
| 136 | * @brief Computes the dot product of two 1D floating point |
| 137 | * vectors. |
| 138 | * result = sum(srcA[0]*srcB[0] + srcA[1]*srcB[1] + ..) |
| 139 | * @param[in] srcPtrA Pointer to the first element of first |
| 140 | * array. |
| 141 | * @param[in] srcPtrB Pointer to the first element of second |
| 142 | * array. |
| 143 | * @param[in] srcLen Number of elements in the array/vector. |
| 144 | * @return Dot product. |
| 145 | */ |
| 146 | static float DotProductF32(float* srcPtrA, float* srcPtrB, |
| 147 | const uint32_t srcLen); |
| 148 | |
| 149 | /** |
| 150 | * @brief Computes the squared magnitude of floating point |
| 151 | * complex number array. |
| 152 | * @param[in] ptrSrc Pointer to the first element of input |
| 153 | * array. |
| 154 | * @param[in] srcLen Number of elements in the array/vector. |
| 155 | * @param[out] ptrDst Output buffer to be populated. |
| 156 | * @param[in] dstLen Output buffer len (for sanity check only). |
| 157 | * @return true if successful, false otherwise. |
| 158 | */ |
| 159 | static bool ComplexMagnitudeSquaredF32(float* ptrSrc, |
| 160 | const uint32_t srcLen, |
| 161 | float* ptrDst, |
| 162 | const uint32_t dstLen); |
| 163 | |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 164 | /** |
| 165 | * @brief Scales output scores for an arbitrary number of classes so |
| 166 | * that they sum to 1, allowing output to be expressed as a probability. |
| 167 | * @param[in] vector Vector of floats modified in-place |
| 168 | */ |
| 169 | static void SoftmaxF32(std::vector<float>& vec); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 170 | }; |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 171 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 172 | } /* namespace math */ |
| 173 | } /* namespace app */ |
| 174 | } /* namespace arm */ |
| 175 | |
| 176 | #endif /* PLATFORM_MATH_HPP */ |