blob: 859716fcdfb559063b4d788716f9ba9080099881 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtoned35a6f2022-02-14 11:55:35 +00002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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
alexander3c798932021-03-26 21:42:19 +000020/* See if ARM DSP functions can be used. */
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010021#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)) */
alexander3c798932021-03-26 21:42:19 +000027
28#include <vector>
alexander31ae9f02022-02-10 16:15:54 +000029#include <cstdint>
30#include <numeric>
alexander3c798932021-03-26 21:42:19 +000031
32namespace arm {
33namespace app {
34namespace math {
35
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +010036 enum class FftType {
37 real = 0,
38 complex = 1
39 };
40
alexander3c798932021-03-26 21:42:19 +000041 struct FftInstance {
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010042#if (defined(__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +010043 arm_rfft_fast_instance_f32 m_instanceReal;
44 arm_cfft_instance_f32 m_instanceComplex;
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +010045#endif /* (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1)) */
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +010046 uint16_t m_fftLen{0};
alexander31ae9f02022-02-10 16:15:54 +000047 FftType m_type{FftType::real};
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +010048 bool m_optimisedOptionAvailable{false};
49 bool m_initialised{false};
alexander3c798932021-03-26 21:42:19 +000050 };
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 Sisodia14ab8d42021-10-22 17:35:01 +010068 * @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 /**
alexander3c798932021-03-26 21:42:19 +000075 * @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 Sisodia14ab8d42021-10-22 17:35:01 +0100106 * @param[in] type FFT type (real or complex)
alexander3c798932021-03-26 21:42:19 +0000107 */
alexander31ae9f02022-02-10 16:15:54 +0000108 static void FftInitF32(uint16_t fftLen,
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +0100109 FftInstance& fftInstance,
alexander31ae9f02022-02-10 16:15:54 +0000110 FftType type = FftType::real);
alexander3c798932021-03-26 21:42:19 +0000111
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 Sisodia14ab8d42021-10-22 17:35:01 +0100120 FftInstance& fftInstance);
alexander3c798932021-03-26 21:42:19 +0000121
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 Burtoned35a6f2022-02-14 11:55:35 +0000129 static void VecLogarithmF32(std::vector<float>& input,
130 std::vector<float>& output);
alexander3c798932021-03-26 21:42:19 +0000131
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,
alexander31ae9f02022-02-10 16:15:54 +0000144 uint32_t srcLen);
alexander3c798932021-03-26 21:42:19 +0000145
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.
153 * @param[in] dstLen Output buffer len (for sanity check only).
154 * @return true if successful, false otherwise.
155 */
156 static bool ComplexMagnitudeSquaredF32(float* ptrSrc,
alexander31ae9f02022-02-10 16:15:54 +0000157 uint32_t srcLen,
alexander3c798932021-03-26 21:42:19 +0000158 float* ptrDst,
alexander31ae9f02022-02-10 16:15:54 +0000159 uint32_t dstLen);
alexander3c798932021-03-26 21:42:19 +0000160
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000161 /**
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 Burtoned35a6f2022-02-14 11:55:35 +0000167
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);
alexander3c798932021-03-26 21:42:19 +0000174 };
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000175
alexander3c798932021-03-26 21:42:19 +0000176} /* namespace math */
177} /* namespace app */
178} /* namespace arm */
179
Kshitij Sisodia9c6f9f82022-05-20 14:30:02 +0100180#endif /* PLATFORM_MATH_HPP */