blob: 45e6a9e1a1c6ea9303167d8e7fd91625bb1a7d5c [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
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
35namespace arm {
36namespace app {
37namespace math {
38
39 struct FftInstance {
40#if ARM_DSP_AVAILABLE
41 arm_rfft_fast_instance_f32 instance;
42#endif
43 bool initialised = false;
44 };
45
46 /* Class to provide Math functions like FFT, mean, stddev etc.
47 * This will allow other classes, functions to be independent of
48 * #if definition checks and provide a cleaner API. Also, it will
49 * consolidate all arm math functions used in one place and make
50 * them easier to test. */
51 class MathUtils {
52
53 public:
54 /**
55 * @brief Get the cosine value of the argument in floating point.
56 * @param[in] radians Angle in radians.
57 * @return Cosine value (floating point).
58 */
59 static float CosineF32(float radians);
60
61 /**
62 * @brief Get the square root of the argument in floating point.
63 * @param[in] input Value to compute square root of.
64 * @return Square root (floating point) value.
65 */
66 static float SqrtF32(float input);
67
68 /**
69 * @brief Gets the mean of a floating point array of elements.
70 * @param[in] ptrSrc Pointer to the first element.
71 * @param[in] srcLen Number of elements in the array/vector.
72 * @return Average value.
73 */
74 static float MeanF32(float* ptrSrc, uint32_t srcLen);
75
76 /**
77 * @brief Gets the standard deviation of a floating point array
78 * of elements.
79 * @param[in] ptrSrc Pointer to the first element.
80 * @param[in] srcLen Number of elements in the array/vector.
81 * @param[in] mean Pre-computed mean value.
82 * @return Standard deviation value.
83 */
84 static float StdDevF32(float* ptrSrc, uint32_t srcLen,
85 float mean);
86
87 /**
88 * @brief Initialises the internal FFT structures (if available
89 * for the platform). This function should be called
90 * prior to Fft32 function call if built with ARM DSP functions.
91 * @param[in] fftLen Requested length of the FFT.
92 * @param[in] fftInstance FFT instance struct to use.
93 * @return true if successful, false otherwise.
94 */
95 static bool FftInitF32(const uint16_t fftLen, arm::app::math::FftInstance& fftInstance);
96
97 /**
98 * @brief Computes the FFT for the input vector.
99 * @param[in] input Floating point vector of input elements
100 * @param[out] fftOutput Output buffer to be populated by computed FFTs.
101 * @param[in] fftInstance FFT instance struct to use.
102 */
103 static void FftF32(std::vector<float>& input,
104 std::vector<float>& fftOutput,
105 arm::app::math::FftInstance& fftInstance);
106
107 /**
108 * @brief Computes the natural logarithms of input floating point
109 * vector
110 * @param[in] input Floating point input vector
111 * @param[out] output Pre-allocated buffer to be populated with
112 * natural log values of each input element.
113 */
114 static void VecLogarithmF32(std::vector <float>& input,
115 std::vector <float>& output);
116
117 /**
118 * @brief Computes the dot product of two 1D floating point
119 * vectors.
120 * result = sum(srcA[0]*srcB[0] + srcA[1]*srcB[1] + ..)
121 * @param[in] srcPtrA Pointer to the first element of first
122 * array.
123 * @param[in] srcPtrB Pointer to the first element of second
124 * array.
125 * @param[in] srcLen Number of elements in the array/vector.
126 * @return Dot product.
127 */
128 static float DotProductF32(float* srcPtrA, float* srcPtrB,
129 const uint32_t srcLen);
130
131 /**
132 * @brief Computes the squared magnitude of floating point
133 * complex number array.
134 * @param[in] ptrSrc Pointer to the first element of input
135 * array.
136 * @param[in] srcLen Number of elements in the array/vector.
137 * @param[out] ptrDst Output buffer to be populated.
138 * @param[in] dstLen Output buffer len (for sanity check only).
139 * @return true if successful, false otherwise.
140 */
141 static bool ComplexMagnitudeSquaredF32(float* ptrSrc,
142 const uint32_t srcLen,
143 float* ptrDst,
144 const uint32_t dstLen);
145
146 };
147} /* namespace math */
148} /* namespace app */
149} /* namespace arm */
150
151#endif /* PLATFORM_MATH_HPP */