blob: 3ffabb4fc8e9fbf69fc9c1cae2bc98a5f2951797 [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 KWS_ASR_WAV2LET_PREPROC_HPP
18#define KWS_ASR_WAV2LET_PREPROC_HPP
19
20#include "Wav2LetterModel.hpp"
21#include "Wav2LetterMfcc.hpp"
22#include "AudioUtils.hpp"
23#include "DataStructures.hpp"
24
25namespace arm {
26namespace app {
27namespace audio {
28namespace asr {
29
30 /* Class to facilitate pre-processing calculation for Wav2Letter model
31 * for ASR. */
32 using AudioWindow = SlidingWindow <const int16_t>;
33
34 class Preprocess {
35 public:
36 /**
37 * @brief Constructor
38 * @param[in] numMfccFeatures Number of MFCC features per window.
39 * @param[in] windowLen Number of elements in a window.
40 * @param[in] windowStride Stride (in number of elements) for
41 * moving the window.
42 * @param[in] numMfccVectors Number of MFCC vectors per window.
43 */
44 Preprocess(
45 uint32_t numMfccFeatures,
46 uint32_t windowLen,
47 uint32_t windowStride,
48 uint32_t numMfccVectors);
49 Preprocess() = delete;
50 ~Preprocess() = default;
51
52 /**
53 * @brief Calculates the features required from audio data. This
54 * includes MFCC, first and second order deltas,
55 * normalisation and finally, quantisation. The tensor is
56 * populated with feature from a given window placed along
57 * in a single row.
58 * @param[in] audioData Pointer to the first element of audio data.
59 * @param[in] audioDataLen Number of elements in the audio data.
60 * @param[in] tensor Tensor to be populated.
61 * @return true if successful, false in case of error.
62 */
63 bool Invoke(const int16_t * audioData,
64 uint32_t audioDataLen,
65 TfLiteTensor * tensor);
66
67 protected:
68 /**
69 * @brief Computes the first and second order deltas for the
70 * MFCC buffers - they are assumed to be populated.
71 *
72 * @param[in] mfcc MFCC buffers.
73 * @param[out] delta1 Result of the first diff computation.
74 * @param[out] delta2 Result of the second diff computation.
75 *
76 * @return true if successful, false otherwise.
77 */
78 static bool _ComputeDeltas(Array2d<float>& mfcc,
79 Array2d<float>& delta1,
80 Array2d<float>& delta2);
81
82 /**
83 * @brief Given a 2D vector of floats, computes the mean.
84 * @param[in] vec Vector of vector of floats.
85 * @return Mean value.
86 */
87 static float _GetMean(Array2d<float>& vec);
88
89 /**
90 * @brief Given a 2D vector of floats, computes the stddev.
91 * @param[in] vec Vector of vector of floats.
92 * @param[in] mean Mean value of the vector passed in.
93 * @return stddev value.
94 */
95 static float _GetStdDev(Array2d<float>& vec,
96 float mean);
97
98 /**
99 * @brief Given a 2D vector of floats, normalises it using
100 * the mean and the stddev
101 * @param[in,out] vec Vector of vector of floats.
102 */
103 static void _NormaliseVec(Array2d<float>& vec);
104
105 /**
106 * @brief Normalises the MFCC and delta buffers.
107 */
108 void _Normalise();
109
110 /**
111 * @brief Given the quantisation and data type limits, computes
112 * the quantised values of a floating point input data.
113 * @param[in] elem Element to be quantised.
114 * @param[in] quantScale Scale.
115 * @param[in] quantOffset Offset.
116 * @param[in] minVal Numerical limit - minimum.
117 * @param[in] maxVal Numerical limit - maximum.
118 * @return Floating point quantised value.
119 */
120 static float _GetQuantElem(
121 float elem,
122 float quantScale,
123 int quantOffset,
124 float minVal,
125 float maxVal);
126
127 /**
128 * @brief Quantises the MFCC and delta buffers, and places them
129 * in the output buffer. While doing so, it transposes
130 * the data. Reason: Buffers in this class are arranged
131 * for "time" axis to be row major. Primary reason for
132 * this being the convolution speed up (as we can use
133 * contiguous memory). The output, however, requires the
134 * time axis to be in column major arrangement.
135 * @param[in] outputBuf Pointer to the output buffer.
136 * @param[in] outputBufSz Output buffer's size.
137 * @param[in] quantScale Quantisation scale.
138 * @param[in] quantOffset Quantisation offset.
139 */
140 template <typename T>
141 bool _Quantise(
142 T * outputBuf,
143 const uint32_t outputBufSz,
144 const float quantScale,
145 const int quantOffset)
146 {
147 /* Check the output size will for everything. */
148 if (outputBufSz < (this->_m_mfccBuf.size(0) * 3 * sizeof(T))) {
149 printf_err("Tensor size too small for features\n");
150 return false;
151 }
152
153 /* Populate. */
154 T * outputBufMfcc = outputBuf;
155 T * outputBufD1 = outputBuf + this->_m_numMfccFeats;
156 T * outputBufD2 = outputBufD1 + this->_m_numMfccFeats;
157 const uint32_t ptrIncr = this->_m_numMfccFeats * 2; /* (3 vectors - 1 vector) */
158
159 const float minVal = std::numeric_limits<T>::min();
160 const float maxVal = std::numeric_limits<T>::max();
161
162 /* We need to do a transpose while copying and concatenating
163 * the tensor. */
164 for (uint32_t j = 0; j < this->_m_numFeatVectors; ++j) {
165 for (uint32_t i = 0; i < this->_m_numMfccFeats; ++i) {
166 *outputBufMfcc++ = static_cast<T>(this->_GetQuantElem(
167 this->_m_mfccBuf(i, j), quantScale,
168 quantOffset, minVal, maxVal));
169 *outputBufD1++ = static_cast<T>(this->_GetQuantElem(
170 this->_m_delta1Buf(i, j), quantScale,
171 quantOffset, minVal, maxVal));
172 *outputBufD2++ = static_cast<T>(this->_GetQuantElem(
173 this->_m_delta2Buf(i, j), quantScale,
174 quantOffset, minVal, maxVal));
175 }
176 outputBufMfcc += ptrIncr;
177 outputBufD1 += ptrIncr;
178 outputBufD2 += ptrIncr;
179 }
180
181 return true;
182 }
183
184 private:
185 Wav2LetterMFCC _m_mfcc; /* MFCC instance. */
186
187 /* Actual buffers to be populated. */
188 Array2d<float> _m_mfccBuf; /* Contiguous buffer 1D: MFCC */
189 Array2d<float> _m_delta1Buf; /* Contiguous buffer 1D: Delta 1 */
190 Array2d<float> _m_delta2Buf; /* Contiguous buffer 1D: Delta 2 */
191
192 uint32_t _m_windowLen; /* Window length for MFCC. */
193 uint32_t _m_windowStride; /* Window stride len for MFCC. */
194 uint32_t _m_numMfccFeats; /* Number of MFCC features per window. */
195 uint32_t _m_numFeatVectors; /* Number of _m_numMfccFeats. */
196 AudioWindow _m_window; /* Sliding window. */
197
198 };
199
200} /* namespace asr */
201} /* namespace audio */
202} /* namespace app */
203} /* namespace arm */
204
205#endif /* KWS_ASR_WAV2LET_PREPROC_HPP */