From c350cdced0a8a2ca17376f58813e6d48d796ac7c Mon Sep 17 00:00:00 2001 From: alexander Date: Thu, 29 Apr 2021 20:36:09 +0100 Subject: MLECO-1868: Code static analyzer warnings fixes Signed-off-by: alexander Change-Id: Ie423e9cad3fabec6ab077ded7236813fe4933dea --- source/use_case/ad/include/AdMelSpectrogram.hpp | 4 +- source/use_case/ad/include/AdModel.hpp | 4 +- source/use_case/ad/include/MelSpectrogram.hpp | 22 ++--- source/use_case/ad/src/AdMelSpectrogram.cc | 17 ++-- source/use_case/ad/src/MelSpectrogram.cc | 53 +++++------ source/use_case/ad/src/UseCaseHandler.cc | 113 +++++++++++++----------- 6 files changed, 112 insertions(+), 101 deletions(-) (limited to 'source/use_case/ad') diff --git a/source/use_case/ad/include/AdMelSpectrogram.hpp b/source/use_case/ad/include/AdMelSpectrogram.hpp index cf8a1d4..30a77c1 100644 --- a/source/use_case/ad/include/AdMelSpectrogram.hpp +++ b/source/use_case/ad/include/AdMelSpectrogram.hpp @@ -60,8 +60,8 @@ namespace audio { virtual bool ApplyMelFilterBank( std::vector& fftVec, std::vector>& melFilterBank, - std::vector& filterBankFilterFirst, - std::vector& filterBankFilterLast, + std::vector& filterBankFilterFirst, + std::vector& filterBankFilterLast, std::vector& melEnergies) override; /** diff --git a/source/use_case/ad/include/AdModel.hpp b/source/use_case/ad/include/AdModel.hpp index 2d83455..bbdf91c 100644 --- a/source/use_case/ad/include/AdModel.hpp +++ b/source/use_case/ad/include/AdModel.hpp @@ -41,10 +41,10 @@ namespace app { private: /* Maximum number of individual operations that can be enlisted */ - static constexpr int _ms_maxOpCnt = 6; + static constexpr int ms_maxOpCnt = 6; /* A mutable op resolver instance */ - tflite::MicroMutableOpResolver<_ms_maxOpCnt> _m_opResolver; + tflite::MicroMutableOpResolver _m_opResolver; }; } /* namespace app */ diff --git a/source/use_case/ad/include/MelSpectrogram.hpp b/source/use_case/ad/include/MelSpectrogram.hpp index c1dd61e..22b5d29 100644 --- a/source/use_case/ad/include/MelSpectrogram.hpp +++ b/source/use_case/ad/include/MelSpectrogram.hpp @@ -49,7 +49,7 @@ namespace audio { ~MelSpecParams() = default; /** @brief String representation of parameters */ - std::string Str(); + std::string Str() const; }; /** @@ -76,7 +76,7 @@ namespace audio { * @brief Constructor * @param[in] params - Mel Spectrogram parameters */ - MelSpectrogram(const MelSpecParams& params); + explicit MelSpectrogram(const MelSpecParams& params); MelSpectrogram() = delete; ~MelSpectrogram() = default; @@ -148,7 +148,7 @@ namespace audio { * bank weights and adding them up to be placed into * bins, according to the filter bank's first and last * indices (pre-computed for each filter bank element - * by _CreateMelFilterBank function). + * by CreateMelFilterBank function). * @param[in] fftVec Vector populated with FFT magnitudes * @param[in] melFilterBank 2D Vector with filter bank weights * @param[in] filterBankFilterFirst Vector containing the first indices of filter bank @@ -162,8 +162,8 @@ namespace audio { virtual bool ApplyMelFilterBank( std::vector& fftVec, std::vector>& melFilterBank, - std::vector& filterBankFilterFirst, - std::vector& filterBankFilterLast, + std::vector& filterBankFilterFirst, + std::vector& filterBankFilterLast, std::vector& melEnergies); /** @@ -195,33 +195,33 @@ namespace audio { std::vector _m_melEnergies; std::vector _m_windowFunc; std::vector> _m_melFilterBank; - std::vector _m_filterBankFilterFirst; - std::vector _m_filterBankFilterLast; + std::vector _m_filterBankFilterFirst; + std::vector _m_filterBankFilterLast; bool _m_filterBankInitialised; arm::app::math::FftInstance _m_fftInstance; /** * @brief Initialises the filter banks. **/ - void _InitMelFilterBank(); + void InitMelFilterBank(); /** * @brief Signals whether the instance of MelSpectrogram has had its * required buffers initialised * @return True if initialised, false otherwise **/ - bool _IsMelFilterBankInited(); + bool IsMelFilterBankInited() const; /** * @brief Create mel filter banks for Mel Spectrogram calculation. * @return 2D vector of floats **/ - std::vector> _CreateMelFilterBank(); + std::vector> CreateMelFilterBank(); /** * @brief Computes the magnitude from an interleaved complex array **/ - void _ConvertToPowerSpectrum(); + void ConvertToPowerSpectrum(); }; diff --git a/source/use_case/ad/src/AdMelSpectrogram.cc b/source/use_case/ad/src/AdMelSpectrogram.cc index 183c05c..e070eb8 100644 --- a/source/use_case/ad/src/AdMelSpectrogram.cc +++ b/source/use_case/ad/src/AdMelSpectrogram.cc @@ -18,6 +18,8 @@ #include "PlatformMath.hpp" +#include + namespace arm { namespace app { namespace audio { @@ -25,8 +27,8 @@ namespace audio { bool AdMelSpectrogram::ApplyMelFilterBank( std::vector& fftVec, std::vector>& melFilterBank, - std::vector& filterBankFilterFirst, - std::vector& filterBankFilterLast, + std::vector& filterBankFilterFirst, + std::vector& filterBankFilterLast, std::vector& melEnergies) { const size_t numBanks = melEnergies.size(); @@ -39,11 +41,12 @@ namespace audio { for (size_t bin = 0; bin < numBanks; ++bin) { auto filterBankIter = melFilterBank[bin].begin(); - float melEnergy = 1e-10; /* Avoid log of zero at later stages. */ - const int32_t firstIndex = filterBankFilterFirst[bin]; - const int32_t lastIndex = filterBankFilterLast[bin]; + auto end = melFilterBank[bin].end(); + float melEnergy = FLT_MIN; /* Avoid log of zero at later stages. */ + const uint32_t firstIndex = filterBankFilterFirst[bin]; + const uint32_t lastIndex = std::min(filterBankFilterLast[bin], fftVec.size() - 1); - for (int32_t i = firstIndex; i <= lastIndex; ++i) { + for (uint32_t i = firstIndex; i <= lastIndex && filterBankIter != end; ++i) { melEnergy += (*filterBankIter++ * fftVec[i]); } @@ -69,7 +72,7 @@ namespace audio { /* Scale the log values. */ for (auto iterM = melEnergies.begin(), iterL = vecLogEnergies.begin(); - iterM != melEnergies.end(); ++iterM, ++iterL) { + iterM != melEnergies.end() && iterL != vecLogEnergies.end(); ++iterM, ++iterL) { *iterM = *iterL * multiplier; } diff --git a/source/use_case/ad/src/MelSpectrogram.cc b/source/use_case/ad/src/MelSpectrogram.cc index 86d57e6..372ebd8 100644 --- a/source/use_case/ad/src/MelSpectrogram.cc +++ b/source/use_case/ad/src/MelSpectrogram.cc @@ -42,7 +42,7 @@ namespace audio { m_useHtkMethod(useHtkMethod) {} - std::string MelSpecParams::Str() + std::string MelSpecParams::Str() const { char strC[1024]; snprintf(strC, sizeof(strC) - 1, "\n \ @@ -71,7 +71,7 @@ namespace audio { this->_m_params.m_numFbankBins, 0.0); this->_m_windowFunc = std::vector(this->_m_params.m_frameLen); - const float multiplier = 2 * M_PI / this->_m_params.m_frameLen; + const auto multiplier = static_cast(2 * M_PI / this->_m_params.m_frameLen); /* Create window function. */ for (size_t i = 0; i < this->_m_params.m_frameLen; ++i) { @@ -85,7 +85,7 @@ namespace audio { void MelSpectrogram::Init() { - this->_InitMelFilterBank(); + this->InitMelFilterBank(); } float MelSpectrogram::MelScale(const float freq, const bool useHTKMethod) @@ -121,8 +121,8 @@ namespace audio { bool MelSpectrogram::ApplyMelFilterBank( std::vector& fftVec, std::vector>& melFilterBank, - std::vector& filterBankFilterFirst, - std::vector& filterBankFilterLast, + std::vector& filterBankFilterFirst, + std::vector& filterBankFilterLast, std::vector& melEnergies) { const size_t numBanks = melEnergies.size(); @@ -135,11 +135,12 @@ namespace audio { for (size_t bin = 0; bin < numBanks; ++bin) { auto filterBankIter = melFilterBank[bin].begin(); + auto end = melFilterBank[bin].end(); float melEnergy = FLT_MIN; /* Avoid log of zero at later stages */ - int32_t firstIndex = filterBankFilterFirst[bin]; - int32_t lastIndex = filterBankFilterLast[bin]; + const uint32_t firstIndex = filterBankFilterFirst[bin]; + const uint32_t lastIndex = std::min(filterBankFilterLast[bin], fftVec.size() - 1); - for (int i = firstIndex; i <= lastIndex; ++i) { + for (uint32_t i = firstIndex; i <= lastIndex && filterBankIter != end; ++i) { float energyRep = math::MathUtils::SqrtF32(fftVec[i]); melEnergy += (*filterBankIter++ * energyRep); } @@ -152,14 +153,14 @@ namespace audio { void MelSpectrogram::ConvertToLogarithmicScale(std::vector& melEnergies) { - for (size_t bin = 0; bin < melEnergies.size(); ++bin) { - melEnergies[bin] = logf(melEnergies[bin]); + for (float& melEnergy : melEnergies) { + melEnergy = logf(melEnergy); } } - void MelSpectrogram::_ConvertToPowerSpectrum() + void MelSpectrogram::ConvertToPowerSpectrum() { - const uint32_t halfDim = this->_m_params.m_frameLenPadded / 2; + const uint32_t halfDim = this->_m_buffer.size() / 2; /* Handle this special case. */ float firstEnergy = this->_m_buffer[0] * this->_m_buffer[0]; @@ -188,22 +189,22 @@ namespace audio { return 1.f; } - void MelSpectrogram::_InitMelFilterBank() + void MelSpectrogram::InitMelFilterBank() { - if (!this->_IsMelFilterBankInited()) { - this->_m_melFilterBank = this->_CreateMelFilterBank(); + if (!this->IsMelFilterBankInited()) { + this->_m_melFilterBank = this->CreateMelFilterBank(); this->_m_filterBankInitialised = true; } } - bool MelSpectrogram::_IsMelFilterBankInited() + bool MelSpectrogram::IsMelFilterBankInited() const { return this->_m_filterBankInitialised; } std::vector MelSpectrogram::ComputeMelSpec(const std::vector& audioData, float trainingMean) { - this->_InitMelFilterBank(); + this->InitMelFilterBank(); /* TensorFlow way of normalizing .wav data to (-1, 1). */ constexpr float normaliser = 1.0/(1<<15); @@ -223,7 +224,7 @@ namespace audio { math::MathUtils::FftF32(this->_m_frame, this->_m_buffer, this->_m_fftInstance); /* Convert to power spectrum. */ - this->_ConvertToPowerSpectrum(); + this->ConvertToPowerSpectrum(); /* Apply mel filterbanks. */ if (!this->ApplyMelFilterBank(this->_m_buffer, @@ -245,7 +246,7 @@ namespace audio { return this->_m_melEnergies; } - std::vector> MelSpectrogram::_CreateMelFilterBank() + std::vector> MelSpectrogram::CreateMelFilterBank() { size_t numFftBins = this->_m_params.m_frameLenPadded / 2; float fftBinWidth = static_cast(this->_m_params.m_samplingFreq) / this->_m_params.m_frameLenPadded; @@ -260,17 +261,18 @@ namespace audio { std::vector> melFilterBank( this->_m_params.m_numFbankBins); this->_m_filterBankFilterFirst = - std::vector(this->_m_params.m_numFbankBins); + std::vector(this->_m_params.m_numFbankBins); this->_m_filterBankFilterLast = - std::vector(this->_m_params.m_numFbankBins); + std::vector(this->_m_params.m_numFbankBins); for (size_t bin = 0; bin < this->_m_params.m_numFbankBins; bin++) { float leftMel = melLowFreq + bin * melFreqDelta; float centerMel = melLowFreq + (bin + 1) * melFreqDelta; float rightMel = melLowFreq + (bin + 2) * melFreqDelta; - int32_t firstIndex = -1; - int32_t lastIndex = -1; + uint32_t firstIndex = 0; + uint32_t lastIndex = 0; + bool firstIndexFound = false; const float normaliser = this->GetMelFilterBankNormaliser(leftMel, rightMel, this->_m_params.m_useHtkMethod); for (size_t i = 0; i < numFftBins; ++i) { @@ -287,8 +289,9 @@ namespace audio { } thisBin[i] = weight * normaliser; - if (firstIndex == -1) { + if (!firstIndexFound) { firstIndex = i; + firstIndexFound = true; } lastIndex = i; } @@ -298,7 +301,7 @@ namespace audio { this->_m_filterBankFilterLast[bin] = lastIndex; /* Copy the part we care about. */ - for (int32_t i = firstIndex; i <= lastIndex; ++i) { + for (uint32_t i = firstIndex; i <= lastIndex; ++i) { melFilterBank[bin].push_back(thisBin[i]); } } diff --git a/source/use_case/ad/src/UseCaseHandler.cc b/source/use_case/ad/src/UseCaseHandler.cc index 1c15595..8f86966 100644 --- a/source/use_case/ad/src/UseCaseHandler.cc +++ b/source/use_case/ad/src/UseCaseHandler.cc @@ -32,7 +32,7 @@ namespace app { * @brief Helper function to increment current audio clip index * @param[in/out] ctx pointer to the application context object **/ - static void _IncrementAppCtxClipIdx(ApplicationContext& ctx); + static void IncrementAppCtxClipIdx(ApplicationContext& ctx); /** * @brief Helper function to set the audio clip index @@ -40,7 +40,7 @@ namespace app { * @param[in] idx value to be set * @return true if index is set, false otherwise **/ - static bool _SetAppCtxClipIdx(ApplicationContext& ctx, uint32_t idx); + static bool SetAppCtxClipIdx(ApplicationContext& ctx, uint32_t idx); /** * @brief Presents inference results using the data presentation @@ -50,7 +50,7 @@ namespace app { * @param[in] threhsold if larger than this value we have an anomaly * @return true if successful, false otherwise **/ - static bool _PresentInferenceResult(hal_platform& platform, float result, float threshold); + static bool PresentInferenceResult(hal_platform& platform, float result, float threshold); /** * @brief Returns a function to perform feature calculation and populates input tensor data with @@ -87,7 +87,7 @@ namespace app { /* If the request has a valid size, set the audio index */ if (clipIndex < NUMBER_OF_FILES) { - if (!_SetAppCtxClipIdx(ctx, clipIndex)) { + if (!SetAppCtxClipIdx(ctx, clipIndex)) { return false; } } @@ -216,20 +216,20 @@ namespace app { dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); ctx.Set("result", result); - if (!_PresentInferenceResult(platform, result, scoreThreshold)) { + if (!PresentInferenceResult(platform, result, scoreThreshold)) { return false; } profiler.PrintProfilingResult(); - _IncrementAppCtxClipIdx(ctx); + IncrementAppCtxClipIdx(ctx); } while (runAll && ctx.Get("clipIndex") != startClipIdx); return true; } - static void _IncrementAppCtxClipIdx(ApplicationContext& ctx) + static void IncrementAppCtxClipIdx(ApplicationContext& ctx) { auto curAudioIdx = ctx.Get("clipIndex"); @@ -241,7 +241,7 @@ namespace app { ctx.Set("clipIndex", curAudioIdx); } - static bool _SetAppCtxClipIdx(ApplicationContext& ctx, const uint32_t idx) + static bool SetAppCtxClipIdx(ApplicationContext& ctx, uint32_t idx) { if (idx >= NUMBER_OF_FILES) { printf_err("Invalid idx %u (expected less than %u)\n", @@ -252,7 +252,7 @@ namespace app { return true; } - static bool _PresentInferenceResult(hal_platform& platform, float result, float threshold) + static bool PresentInferenceResult(hal_platform& platform, float result, float threshold) { constexpr uint32_t dataPsnTxtStartX1 = 20; constexpr uint32_t dataPsnTxtStartY1 = 30; @@ -275,7 +275,7 @@ namespace app { platform.data_psn->present_data_text( resultStr.c_str(), resultStr.size(), - dataPsnTxtStartX1, rowIdx1, 0); + dataPsnTxtStartX1, rowIdx1, false); info("%s\n", resultStr.c_str()); @@ -297,8 +297,8 @@ namespace app { */ template std::function&, size_t, bool, size_t, size_t)> - _FeatureCalc(TfLiteTensor* inputTensor, size_t cacheSize, - std::function (std::vector& )> compute) + FeatureCalc(TfLiteTensor* inputTensor, size_t cacheSize, + std::function (std::vector& )> compute) { /* Feature cache to be captured by lambda function*/ static std::vector> featureCache = std::vector>(cacheSize); @@ -335,24 +335,24 @@ namespace app { } template std::function&, size_t , bool, size_t, size_t)> - _FeatureCalc(TfLiteTensor* inputTensor, - size_t cacheSize, - std::function (std::vector&)> compute); + FeatureCalc(TfLiteTensor* inputTensor, + size_t cacheSize, + std::function (std::vector&)> compute); template std::function&, size_t , bool, size_t, size_t)> - _FeatureCalc(TfLiteTensor* inputTensor, - size_t cacheSize, - std::function (std::vector&)> compute); + FeatureCalc(TfLiteTensor* inputTensor, + size_t cacheSize, + std::function (std::vector&)> compute); template std::function&, size_t , bool, size_t, size_t)> - _FeatureCalc(TfLiteTensor* inputTensor, - size_t cacheSize, - std::function (std::vector&)> compute); + FeatureCalc(TfLiteTensor* inputTensor, + size_t cacheSize, + std::function (std::vector&)> compute); template std::function&, size_t, bool, size_t, size_t)> - _FeatureCalc(TfLiteTensor *inputTensor, - size_t cacheSize, - std::function(std::vector&)> compute); + FeatureCalc(TfLiteTensor *inputTensor, + size_t cacheSize, + std::function(std::vector&)> compute); static std::function&, int, bool, size_t, size_t)> @@ -370,38 +370,41 @@ namespace app { switch (inputTensor->type) { case kTfLiteInt8: { - melSpecFeatureCalc = _FeatureCalc(inputTensor, - cacheSize, - [=, &melSpec](std::vector& audioDataWindow) { - return melSpec.MelSpecComputeQuant(audioDataWindow, - quantScale, - quantOffset, - trainingMean); - } + melSpecFeatureCalc = FeatureCalc(inputTensor, + cacheSize, + [=, &melSpec](std::vector& audioDataWindow) { + return melSpec.MelSpecComputeQuant( + audioDataWindow, + quantScale, + quantOffset, + trainingMean); + } ); break; } case kTfLiteUInt8: { - melSpecFeatureCalc = _FeatureCalc(inputTensor, - cacheSize, - [=, &melSpec](std::vector& audioDataWindow) { - return melSpec.MelSpecComputeQuant(audioDataWindow, - quantScale, - quantOffset, - trainingMean); - } + melSpecFeatureCalc = FeatureCalc(inputTensor, + cacheSize, + [=, &melSpec](std::vector& audioDataWindow) { + return melSpec.MelSpecComputeQuant( + audioDataWindow, + quantScale, + quantOffset, + trainingMean); + } ); break; } case kTfLiteInt16: { - melSpecFeatureCalc = _FeatureCalc(inputTensor, - cacheSize, - [=, &melSpec](std::vector& audioDataWindow) { - return melSpec.MelSpecComputeQuant(audioDataWindow, - quantScale, - quantOffset, - trainingMean); - } + melSpecFeatureCalc = FeatureCalc(inputTensor, + cacheSize, + [=, &melSpec](std::vector& audioDataWindow) { + return melSpec.MelSpecComputeQuant( + audioDataWindow, + quantScale, + quantOffset, + trainingMean); + } ); break; } @@ -411,12 +414,14 @@ namespace app { } else { - melSpecFeatureCalc = melSpecFeatureCalc = _FeatureCalc(inputTensor, - cacheSize, - [=, &melSpec](std::vector& audioDataWindow) { - return melSpec.ComputeMelSpec(audioDataWindow, - trainingMean); - }); + melSpecFeatureCalc = melSpecFeatureCalc = FeatureCalc(inputTensor, + cacheSize, + [=, &melSpec]( + std::vector& audioDataWindow) { + return melSpec.ComputeMelSpec( + audioDataWindow, + trainingMean); + }); } return melSpecFeatureCalc; } -- cgit v1.2.1