diff options
author | George Gekov <george.gekov@arm.com> | 2021-08-16 11:32:10 +0100 |
---|---|---|
committer | Jim Flynn <jim.flynn@arm.com> | 2022-02-05 19:49:06 +0000 |
commit | 23c26277086c78704a17f0dae86da947816320c0 (patch) | |
tree | 88b02fd1fae3130256d059251788a7ef68d2831f /samples/KeywordSpotting/src/DsCNNPreprocessor.cpp | |
parent | 922b912fd2d462bac0809bac5669310ad1506310 (diff) | |
download | armnn-23c26277086c78704a17f0dae86da947816320c0.tar.gz |
MLECO-2079 Adding the C++ KWS example
Signed-off-by: Eanna O Cathain <eanna.ocathain@arm.com>
Change-Id: I81899bbfaada32f478c2e2fc6441eabb94d8d0fc
Diffstat (limited to 'samples/KeywordSpotting/src/DsCNNPreprocessor.cpp')
-rw-r--r-- | samples/KeywordSpotting/src/DsCNNPreprocessor.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/samples/KeywordSpotting/src/DsCNNPreprocessor.cpp b/samples/KeywordSpotting/src/DsCNNPreprocessor.cpp new file mode 100644 index 0000000000..8215feeeb5 --- /dev/null +++ b/samples/KeywordSpotting/src/DsCNNPreprocessor.cpp @@ -0,0 +1,40 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// +#include <cmath> +#include <numeric> +#include <algorithm> +#include <memory> +#include "MathUtils.hpp" +#include "SlidingWindow.hpp" +#include "DsCNNPreprocessor.hpp" + +std::vector<int8_t> kws::DsCNNPreprocessor::Invoke(const float* audioData, size_t dataSize, + int quantOffset, float quantScale) +{ + auto window = SlidingWindow<const float>( + audioData, dataSize, + this->m_windowLen, this->m_windowStride); + + uint32_t mfccBufIdx = 0; + std::vector<int8_t> outputBuffer; + // While we can slide over the window + while (window.HasNext()) + { + const float* mfccWindow = window.Next(); + auto mfccAudioData = std::vector<float>(mfccWindow, mfccWindow + this->m_windowLen); + + auto mfcc = this->m_mfcc->MfccComputeQuant<int8_t>(mfccAudioData, quantScale, quantOffset); + + std::copy(mfcc.begin(), mfcc.end(), std::back_inserter(outputBuffer)); + + ++mfccBufIdx; + } + + return outputBuffer; +} + +kws::DsCNNPreprocessor::DsCNNPreprocessor(const uint32_t windowLen, const uint32_t windowStride, + std::unique_ptr<DsCnnMFCC> mfccInst) : + m_windowLen{windowLen}, m_windowStride{windowStride}, m_mfcc{std::move(mfccInst)} {} |