blob: 5ac10dedf0c779db3fc85beea43bc5eee4bb6895 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
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
alexander3c798932021-03-26 21:42:19 +000020/* See if ARM DSP functions can be used. */
alexander31ae9f02022-02-10 16:15:54 +000021#if defined(ARM_MATH_DSP)
alexander3c798932021-03-26 21:42:19 +000022
alexander31ae9f02022-02-10 16:15:54 +000023 #include "arm_math.h"
24 #define M_PI (PI)
25#else
26 #include <cmath>
27#endif
alexander3c798932021-03-26 21:42:19 +000028
29#include <vector>
alexander31ae9f02022-02-10 16:15:54 +000030#include <cstdint>
31#include <numeric>
alexander3c798932021-03-26 21:42:19 +000032
33namespace arm {
34namespace app {
35namespace math {
36
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +010037 enum class FftType {
38 real = 0,
39 complex = 1
40 };
41
alexander3c798932021-03-26 21:42:19 +000042 struct FftInstance {
alexander31ae9f02022-02-10 16:15:54 +000043#if ARM_MATH_DSP
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +010044 arm_rfft_fast_instance_f32 m_instanceReal;
45 arm_cfft_instance_f32 m_instanceComplex;
alexander3c798932021-03-26 21:42:19 +000046#endif
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +010047 uint16_t m_fftLen{0};
alexander31ae9f02022-02-10 16:15:54 +000048 FftType m_type{FftType::real};
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +010049 bool m_optimisedOptionAvailable{false};
50 bool m_initialised{false};
alexander3c798932021-03-26 21:42:19 +000051 };
52
53 /* Class to provide Math functions like FFT, mean, stddev etc.
54 * This will allow other classes, functions to be independent of
55 * #if definition checks and provide a cleaner API. Also, it will
56 * consolidate all arm math functions used in one place and make
57 * them easier to test. */
58 class MathUtils {
59
60 public:
61 /**
62 * @brief Get the cosine value of the argument in floating point.
63 * @param[in] radians Angle in radians.
64 * @return Cosine value (floating point).
65 */
66 static float CosineF32(float radians);
67
68 /**
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +010069 * @brief Get the sine value of the argument in floating point.
70 * @param[in] radians Angle in radians.
71 * @return Sine value (floating point).
72 */
73 static float SineF32(float radians);
74
75 /**
alexander3c798932021-03-26 21:42:19 +000076 * @brief Get the square root of the argument in floating point.
77 * @param[in] input Value to compute square root of.
78 * @return Square root (floating point) value.
79 */
80 static float SqrtF32(float input);
81
82 /**
83 * @brief Gets the mean of a floating point array of elements.
84 * @param[in] ptrSrc Pointer to the first element.
85 * @param[in] srcLen Number of elements in the array/vector.
86 * @return Average value.
87 */
88 static float MeanF32(float* ptrSrc, uint32_t srcLen);
89
90 /**
91 * @brief Gets the standard deviation of a floating point array
92 * of elements.
93 * @param[in] ptrSrc Pointer to the first element.
94 * @param[in] srcLen Number of elements in the array/vector.
95 * @param[in] mean Pre-computed mean value.
96 * @return Standard deviation value.
97 */
98 static float StdDevF32(float* ptrSrc, uint32_t srcLen,
99 float mean);
100
101 /**
102 * @brief Initialises the internal FFT structures (if available
103 * for the platform). This function should be called
104 * prior to Fft32 function call if built with ARM DSP functions.
105 * @param[in] fftLen Requested length of the FFT.
106 * @param[in] fftInstance FFT instance struct to use.
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +0100107 * @param[in] type FFT type (real or complex)
alexander3c798932021-03-26 21:42:19 +0000108 */
alexander31ae9f02022-02-10 16:15:54 +0000109 static void FftInitF32(uint16_t fftLen,
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +0100110 FftInstance& fftInstance,
alexander31ae9f02022-02-10 16:15:54 +0000111 FftType type = FftType::real);
alexander3c798932021-03-26 21:42:19 +0000112
113 /**
114 * @brief Computes the FFT for the input vector.
115 * @param[in] input Floating point vector of input elements
116 * @param[out] fftOutput Output buffer to be populated by computed FFTs.
117 * @param[in] fftInstance FFT instance struct to use.
118 */
119 static void FftF32(std::vector<float>& input,
120 std::vector<float>& fftOutput,
Kshitij Sisodia14ab8d42021-10-22 17:35:01 +0100121 FftInstance& fftInstance);
alexander3c798932021-03-26 21:42:19 +0000122
123 /**
124 * @brief Computes the natural logarithms of input floating point
125 * vector
126 * @param[in] input Floating point input vector
127 * @param[out] output Pre-allocated buffer to be populated with
128 * natural log values of each input element.
129 */
130 static void VecLogarithmF32(std::vector <float>& input,
131 std::vector <float>& output);
132
133 /**
134 * @brief Computes the dot product of two 1D floating point
135 * vectors.
136 * result = sum(srcA[0]*srcB[0] + srcA[1]*srcB[1] + ..)
137 * @param[in] srcPtrA Pointer to the first element of first
138 * array.
139 * @param[in] srcPtrB Pointer to the first element of second
140 * array.
141 * @param[in] srcLen Number of elements in the array/vector.
142 * @return Dot product.
143 */
144 static float DotProductF32(float* srcPtrA, float* srcPtrB,
alexander31ae9f02022-02-10 16:15:54 +0000145 uint32_t srcLen);
alexander3c798932021-03-26 21:42:19 +0000146
147 /**
148 * @brief Computes the squared magnitude of floating point
149 * complex number array.
150 * @param[in] ptrSrc Pointer to the first element of input
151 * array.
152 * @param[in] srcLen Number of elements in the array/vector.
153 * @param[out] ptrDst Output buffer to be populated.
154 * @param[in] dstLen Output buffer len (for sanity check only).
155 * @return true if successful, false otherwise.
156 */
157 static bool ComplexMagnitudeSquaredF32(float* ptrSrc,
alexander31ae9f02022-02-10 16:15:54 +0000158 uint32_t srcLen,
alexander3c798932021-03-26 21:42:19 +0000159 float* ptrDst,
alexander31ae9f02022-02-10 16:15:54 +0000160 uint32_t dstLen);
alexander3c798932021-03-26 21:42:19 +0000161
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000162 /**
163 * @brief Scales output scores for an arbitrary number of classes so
164 * that they sum to 1, allowing output to be expressed as a probability.
165 * @param[in] vector Vector of floats modified in-place
166 */
167 static void SoftmaxF32(std::vector<float>& vec);
alexander3c798932021-03-26 21:42:19 +0000168 };
Kshitij Sisodia76a15802021-12-24 11:05:11 +0000169
alexander3c798932021-03-26 21:42:19 +0000170} /* namespace math */
171} /* namespace app */
172} /* namespace arm */
173
174#endif /* PLATFORM_MATH_HPP */