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/common | |
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/common')
-rw-r--r-- | samples/common/include/ArmnnUtils/ArmnnNetworkExecutor.hpp | 18 | ||||
-rw-r--r-- | samples/common/include/Audio/AudioCapture.hpp | 57 | ||||
-rw-r--r-- | samples/common/include/Audio/DataStructures.hpp | 102 | ||||
-rw-r--r-- | samples/common/include/Audio/MFCC.hpp | 234 | ||||
-rw-r--r-- | samples/common/include/Audio/MathUtils.hpp | 85 | ||||
-rw-r--r-- | samples/common/include/Audio/SlidingWindow.hpp | 161 | ||||
-rw-r--r-- | samples/common/src/Audio/AudioCapture.cpp | 96 | ||||
-rw-r--r-- | samples/common/src/Audio/MFCC.cpp | 354 | ||||
-rw-r--r-- | samples/common/src/Audio/MathUtils.cpp | 111 | ||||
-rw-r--r-- | samples/common/test/Audio/AudioCaptureTest.cpp | 61 | ||||
-rw-r--r-- | samples/common/test/Audio/MathUtilsTest.cpp | 112 |
11 files changed, 1391 insertions, 0 deletions
diff --git a/samples/common/include/ArmnnUtils/ArmnnNetworkExecutor.hpp b/samples/common/include/ArmnnUtils/ArmnnNetworkExecutor.hpp index 96cc1d0184..9f1ef5475c 100644 --- a/samples/common/include/ArmnnUtils/ArmnnNetworkExecutor.hpp +++ b/samples/common/include/ArmnnUtils/ArmnnNetworkExecutor.hpp @@ -72,6 +72,10 @@ public: int GetQuantizationOffset(); + float GetOutputQuantizationScale(int tensorIndex); + + int GetOutputQuantizationOffset(int tensorIndex); + /** * @brief Runs inference on the provided input data, and stores the results in the provided InferenceResults object. * @@ -203,6 +207,20 @@ int ArmnnNetworkExecutor<Tout>::GetQuantizationOffset() } template <class Tout> +float ArmnnNetworkExecutor<Tout>::GetOutputQuantizationScale(int tensorIndex) +{ + assert(this->m_outputLayerNamesList.size() > tensorIndex); + return this->m_outputBindingInfo[tensorIndex].second.GetQuantizationScale(); +} + +template <class Tout> +int ArmnnNetworkExecutor<Tout>::GetOutputQuantizationOffset(int tensorIndex) +{ + assert(this->m_outputLayerNamesList.size() > tensorIndex); + return this->m_outputBindingInfo[tensorIndex].second.GetQuantizationOffset(); +} + +template <class Tout> Size ArmnnNetworkExecutor<Tout>::GetImageAspectRatio() { const auto shape = m_inputBindingInfo.second.GetShape(); diff --git a/samples/common/include/Audio/AudioCapture.hpp b/samples/common/include/Audio/AudioCapture.hpp new file mode 100644 index 0000000000..898bf911f4 --- /dev/null +++ b/samples/common/include/Audio/AudioCapture.hpp @@ -0,0 +1,57 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include <string> +#include <iostream> +#include <cmath> +#include <vector> +#include <exception> + +#include "SlidingWindow.hpp" + +namespace audio +{ + +/** +* @brief Class used to capture the audio data loaded from file, and to provide a method of + * extracting correctly positioned and appropriately sized audio windows +* +*/ + class AudioCapture + { + public: + + SlidingWindow<const float> m_window; + + /** + * @brief Default constructor + */ + AudioCapture() = default; + + /** + * @brief Function to load the audio data captured from the + * input file to memory. + */ + static std::vector<float> LoadAudioFile(std::string filePath); + + /** + * @brief Function to initialize the sliding window. This will set its position in memory, its + * window size and its stride. + */ + void InitSlidingWindow(float* data, size_t dataSize, int minSamples, size_t stride); + + /** + * Checks whether there is another block of audio in memory to read + */ + bool HasNext(); + + /** + * Retrieves the next block of audio if its available + */ + std::vector<float> Next(); + }; +} // namespace audio
\ No newline at end of file diff --git a/samples/common/include/Audio/DataStructures.hpp b/samples/common/include/Audio/DataStructures.hpp new file mode 100644 index 0000000000..9922265299 --- /dev/null +++ b/samples/common/include/Audio/DataStructures.hpp @@ -0,0 +1,102 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// +#pragma once + +#include <stdio.h> +#include <iterator> + +/** + * Class Array2d is a data structure that represents a two dimensional array. + * The data is allocated in contiguous memory, arranged row-wise + * and individual elements can be accessed with the () operator. + * For example a two dimensional array D of size (M, N) can be accessed: + * + * _|<------------- col size = N -------->| + * | D(r=0, c=0) D(r=0, c=1)... D(r=0, c=N) + * | D(r=1, c=0) D(r=1, c=1)... D(r=1, c=N) + * | ... + * row size = M ... + * | ... + * _ D(r=M, c=0) D(r=M, c=1)... D(r=M, c=N) + * + */ +template<typename T> +class Array2d +{ +private: + size_t m_rows; + size_t m_cols; + T* m_data; + +public: + /** + * Creates the array2d with the given sizes. + * + * @param rows number of rows. + * @param cols number of columns. + */ + Array2d(unsigned rows, unsigned cols) + { + if (rows == 0 || cols == 0) { + printf("Array2d constructor has 0 size.\n"); + m_data = nullptr; + return; + } + m_rows = rows; + m_cols = cols; + m_data = new T[rows * cols]; + } + + ~Array2d() + { + delete[] m_data; + } + + T& operator() (unsigned int row, unsigned int col) + { + return m_data[m_cols * row + col]; + } + + T operator() (unsigned int row, unsigned int col) const + { + return m_data[m_cols * row + col]; + } + + /** + * Gets rows number of the current array2d. + * @return number of rows. + */ + size_t size(size_t dim) + { + switch (dim) + { + case 0: + return m_rows; + case 1: + return m_cols; + default: + return 0; + } + } + + /** + * Gets the array2d total size. + */ + size_t totalSize() + { + return m_rows * m_cols; + } + + /** + * array2d iterator. + */ + using iterator=T*; + using const_iterator=T const*; + + iterator begin() { return m_data; } + iterator end() { return m_data + totalSize(); } + const_iterator begin() const { return m_data; } + const_iterator end() const { return m_data + totalSize(); }; +}; diff --git a/samples/common/include/Audio/MFCC.hpp b/samples/common/include/Audio/MFCC.hpp new file mode 100644 index 0000000000..468bf92fae --- /dev/null +++ b/samples/common/include/Audio/MFCC.hpp @@ -0,0 +1,234 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// +#pragma once + + +#include <vector> +#include <cstdint> +#include <cmath> +#include <limits> +#include <string> + +/* MFCC's consolidated parameters */ +class MfccParams +{ +public: + float m_samplingFreq; + int m_numFbankBins; + float m_melLoFreq; + float m_melHiFreq; + int m_numMfccFeatures; + int m_frameLen; + int m_frameLenPadded; + bool m_useHtkMethod; + int m_numMfccVectors; + /** @brief Constructor */ + MfccParams(const float samplingFreq, const int numFbankBins, + const float melLoFreq, const float melHiFreq, + const int numMfccFeats, const int frameLen, + const bool useHtkMethod, const int numMfccVectors); + /* Delete the default constructor */ + MfccParams() = delete; + /* Default destructor */ + ~MfccParams() = default; + /** @brief String representation of parameters */ + std::string Str(); +}; + +/** + * @brief Class for MFCC feature extraction. + * Based on https://github.com/ARM-software/ML-KWS-for-MCU/blob/master/Deployment/Source/MFCC/mfcc.cpp + * This class is designed to be generic and self-sufficient but + * certain calculation routines can be overridden to accommodate + * use-case specific requirements. + */ +class MFCC { +public: + /** + * @brief Constructor + * @param[in] params MFCC parameters + */ + explicit MFCC(const MfccParams& params); + + MFCC() = delete; + + ~MFCC() = default; + + /** + * @brief Extract MFCC features for one single small frame of + * audio data e.g. 640 samples. + * @param[in] audioData Vector of audio samples to calculate + * features for. + * @return Vector of extracted MFCC features. + **/ + std::vector<float> MfccCompute(const std::vector<float>& audioData); + + /** @brief Initialise. */ + void Init(); + + /** + * @brief Extract MFCC features and quantise for one single small + * frame of audio data e.g. 640 samples. + * @param[in] audioData Vector of audio samples to calculate + * features for. + * @param[in] quantScale Quantisation scale. + * @param[in] quantOffset Quantisation offset. + * @return Vector of extracted quantised MFCC features. + **/ + template<typename T> + std::vector<T> MfccComputeQuant(const std::vector<float>& audioData, + const float quantScale, + const int quantOffset) + { + this->MfccComputePreFeature(audioData); + float minVal = std::numeric_limits<T>::min(); + float maxVal = std::numeric_limits<T>::max(); + + std::vector<T> mfccOut(this->m_params.m_numMfccFeatures); + const size_t numFbankBins = this->m_params.m_numFbankBins; + + /* Take DCT. Uses matrix mul. */ + for (size_t i = 0, j = 0; i < mfccOut.size(); ++i, j += numFbankBins) + { + float sum = 0; + for (size_t k = 0; k < numFbankBins; ++k) + { + sum += this->m_dctMatrix[j + k] * this->m_melEnergies[k]; + } + /* Quantize to T. */ + sum = std::round((sum / quantScale) + quantOffset); + mfccOut[i] = static_cast<T>(std::min<float>(std::max<float>(sum, minVal), maxVal)); + } + + return mfccOut; + } + + MfccParams m_params; + + /* Constants */ + static constexpr float ms_logStep = /*logf(6.4)*/ 1.8562979903656 / 27.0; + static constexpr float ms_freqStep = 200.0 / 3; + static constexpr float ms_minLogHz = 1000.0; + static constexpr float ms_minLogMel = ms_minLogHz / ms_freqStep; + +protected: + /** + * @brief Project input frequency to Mel Scale. + * @param[in] freq Input frequency in floating point. + * @param[in] useHTKMethod bool to signal if HTK method is to be + * used for calculation. + * @return Mel transformed frequency in floating point. + **/ + static float MelScale(float freq, + bool useHTKMethod = true); + + /** + * @brief Inverse Mel transform - convert MEL warped frequency + * back to normal frequency. + * @param[in] melFreq Mel frequency in floating point. + * @param[in] useHTKMethod bool to signal if HTK method is to be + * used for calculation. + * @return Real world frequency in floating point. + **/ + static float InverseMelScale(float melFreq, + bool useHTKMethod = true); + + /** + * @brief Populates MEL energies after applying the MEL filter + * 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). + * @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 + * to be used for each bin. + * @param[in] filterBankFilterLast Vector containing the last indices of filter bank + * to be used for each bin. + * @param[out] melEnergies Pre-allocated vector of MEL energies to be + * populated. + * @return true if successful, false otherwise. + */ + virtual bool ApplyMelFilterBank( + std::vector<float>& fftVec, + std::vector<std::vector<float>>& melFilterBank, + std::vector<uint32_t>& filterBankFilterFirst, + std::vector<uint32_t>& filterBankFilterLast, + std::vector<float>& melEnergies); + + /** + * @brief Converts the Mel energies for logarithmic scale. + * @param[in,out] melEnergies 1D vector of Mel energies. + **/ + virtual void ConvertToLogarithmicScale(std::vector<float>& melEnergies); + + /** + * @brief Create a matrix used to calculate Discrete Cosine + * Transform. + * @param[in] inputLength Input length of the buffer on which + * DCT will be performed. + * @param[in] coefficientCount Total coefficients per input length. + * @return 1D vector with inputLength x coefficientCount elements + * populated with DCT coefficients. + */ + virtual std::vector<float> CreateDCTMatrix( + int32_t inputLength, + int32_t coefficientCount); + + /** + * @brief Given the low and high Mel values, get the normaliser + * for weights to be applied when populating the filter + * bank. + * @param[in] leftMel Low Mel frequency value. + * @param[in] rightMel High Mel frequency value. + * @param[in] useHTKMethod bool to signal if HTK method is to be + * used for calculation. + * @return Value to use for normalizing. + */ + virtual float GetMelFilterBankNormaliser( + const float& leftMel, + const float& rightMel, + bool useHTKMethod); + +private: + + std::vector<float> m_frame; + std::vector<float> m_buffer; + std::vector<float> m_melEnergies; + std::vector<float> m_windowFunc; + std::vector<std::vector<float>> m_melFilterBank; + std::vector<float> m_dctMatrix; + std::vector<uint32_t> m_filterBankFilterFirst; + std::vector<uint32_t> m_filterBankFilterLast; + bool m_filterBankInitialised; + + /** + * @brief Initialises the filter banks and the DCT matrix. **/ + void InitMelFilterBank(); + + /** + * @brief Signals whether the instance of MFCC has had its + * required buffers initialised. + * @return true if initialised, false otherwise. + **/ + bool IsMelFilterBankInited() const; + + /** + * @brief Create mel filter banks for MFCC calculation. + * @return 2D vector of floats. + **/ + std::vector<std::vector<float>> CreateMelFilterBank(); + + /** + * @brief Computes and populates internal memeber buffers used + * in MFCC feature calculation + * @param[in] audioData 1D vector of 16-bit audio data. + */ + void MfccComputePreFeature(const std::vector<float>& audioData); + + /** @brief Computes the magnitude from an interleaved complex array. */ + void ConvertToPowerSpectrum(); + +}; diff --git a/samples/common/include/Audio/MathUtils.hpp b/samples/common/include/Audio/MathUtils.hpp new file mode 100644 index 0000000000..1d8b0d31cc --- /dev/null +++ b/samples/common/include/Audio/MathUtils.hpp @@ -0,0 +1,85 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include <vector> +#include <cmath> +#include <cstdint> +#include <numeric> + +class MathUtils +{ + +public: + + /** + * @brief Computes the FFT for the input vector + * @param[in] input Floating point vector of input elements + * @param[out] fftOutput Output buffer to be populated by computed + * FFTs + * @return none + */ + static void FftF32(std::vector<float>& input, + std::vector<float>& fftOutput); + + + /** + * @brief Computes the dot product of two 1D floating point + * vectors. + * result = sum(srcA[0]*srcB[0] + srcA[1]*srcB[1] + ..) + * @param[in] srcPtrA pointer to the first element of first + * array + * @param[in] srcPtrB pointer to the first element of second + * array + * @param[in] srcLen Number of elements in the array/vector + * @return dot product + */ + static float DotProductF32(const float* srcPtrA, float* srcPtrB, + int srcLen); + + /** + * @brief Computes the squared magnitude of floating point + * complex number array. + * @param[in] ptrSrc pointer to the first element of input + * array + * @param[in] srcLen Number of elements in the array/vector + * @param[out] ptrDst Output buffer to be populated + * @param[in] dstLen output buffer len (for sanity check only) + * @return true if successful, false otherwise + */ + static bool ComplexMagnitudeSquaredF32(const float* ptrSrc, + int srcLen, + float* ptrDst, + int dstLen); + + /** + * @brief Computes the natural logarithms of input floating point + * vector + * @param[in] input Floating point input vector + * @param[out] output Pre-allocated buffer to be populated with + * natural log values of each input element + * @return none + */ + static void VecLogarithmF32(std::vector <float>& input, + std::vector <float>& output); + + /** + * @brief Gets the mean of a floating point array of elements + * @param[in] ptrSrc pointer to the first element + * @param[in] srcLen Number of elements in the array/vector + * @return average value + */ + static float MeanF32(const float* ptrSrc, uint32_t srcLen); + + /** + * @brief Gets the standard deviation of a floating point array + * of elements + * @param[in] ptrSrc pointer to the first element + * @param[in] srcLen Number of elements in the array/vector + * @param[in] mean pre-computed mean value + * @return standard deviation value + */ + static float StdDevF32(const float* ptrSrc, uint32_t srcLen, + float mean); +}; diff --git a/samples/common/include/Audio/SlidingWindow.hpp b/samples/common/include/Audio/SlidingWindow.hpp new file mode 100644 index 0000000000..77498c6338 --- /dev/null +++ b/samples/common/include/Audio/SlidingWindow.hpp @@ -0,0 +1,161 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +template<class T> +class SlidingWindow +{ +protected: + T* m_start = nullptr; + size_t m_dataSize = 0; + size_t m_size = 0; + size_t m_stride = 0; + size_t m_count = 0; +public: + + /** + * Creates the window slider through the given data. + * + * @param data pointer to the data to slide through. + * @param dataSize size in T type elements wise. + * @param windowSize sliding window size in T type wise elements. + * @param stride stride size in T type wise elements. + */ + SlidingWindow(T* data, size_t dataSize, + size_t windowSize, size_t stride) + { + m_start = data; + m_dataSize = dataSize; + m_size = windowSize; + m_stride = stride; + } + + SlidingWindow() = default; + + ~SlidingWindow() = default; + + /** + * Get the next data window. + * @return pointer to the next window, if next window is not available nullptr is returned. + */ + virtual T* Next() + { + if (HasNext()) + { + m_count++; + return m_start + Index() * m_stride; + } + else + { + return nullptr; + } + } + + /** + * Checks if the next data portion is available. + * @return true if next data portion is available + */ + bool HasNext() + { + return this->m_count < 1 + this->FractionalTotalStrides() && (this->NextWindowStartIndex() < this->m_dataSize); + } + + /** + * Resest the slider to the initial position. + */ + virtual void Reset() + { + m_count = 0; + } + + /** + * Resest the slider to the initial position. + */ + virtual size_t GetWindowSize() + { + return m_size; + } + + /** + * Resets the slider to the start of the new data. + * New data size MUST be the same as the old one. + * @param newStart pointer to the new data to slide through. + */ + virtual void Reset(T* newStart) + { + m_start = newStart; + Reset(); + } + + /** + * Gets current index of the sliding window. + * @return current position of the sliding window in number of strides + */ + size_t Index() + { + return m_count == 0? 0: m_count - 1; + } + + /** + * Gets the index from the start of the data where the next window will begin. + * While Index() returns the index of sliding window itself this function returns the index of the data + * element itself. + * @return Index from the start of the data where the next sliding window will begin. + */ + virtual size_t NextWindowStartIndex() + { + return m_count == 0? 0: ((m_count) * m_stride); + } + + /** + * Go to given sliding window index. + * @param index new position of the sliding window. if index is invalid (greater than possible range of strides) + * then next call to Next() will return nullptr. + */ + void FastForward(size_t index) + { + m_count = index; + } + + /** + * Calculates whole number of times the window can stride through the given data. + * @return maximum number of strides. + */ + size_t TotalStrides() + { + if (m_size > m_dataSize) + { + return 0; + } + return ((m_dataSize - m_size)/m_stride); + } + + /** + * Calculates number of times the window can stride through the given data. May not be a whole number. + * @return Number of strides to cover all data. + */ + float FractionalTotalStrides() + { + if(this->m_size > this->m_dataSize) + { + return this->m_dataSize / this->m_size; + } + else + { + return ((this->m_dataSize - this->m_size)/ static_cast<float>(this->m_stride)); + } + + } + + /** + * Calculates the remaining data left to be processed + * @return The remaining unprocessed data + */ + int RemainingData() + { + return this->m_dataSize - this->NextWindowStartIndex(); + } +};
\ No newline at end of file diff --git a/samples/common/src/Audio/AudioCapture.cpp b/samples/common/src/Audio/AudioCapture.cpp new file mode 100644 index 0000000000..920d7a5233 --- /dev/null +++ b/samples/common/src/Audio/AudioCapture.cpp @@ -0,0 +1,96 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "AudioCapture.hpp" +#include <alsa/asoundlib.h> +#include <sndfile.h> +#include <samplerate.h> + +namespace audio +{ + std::vector<float> AudioCapture::LoadAudioFile(std::string filePath) + { + SF_INFO inputSoundFileInfo; + SNDFILE* infile = nullptr; + infile = sf_open(filePath.c_str(), SFM_READ, &inputSoundFileInfo); + + float audioIn[inputSoundFileInfo.channels * inputSoundFileInfo.frames]; + sf_read_float(infile, audioIn, inputSoundFileInfo.channels * inputSoundFileInfo.frames); + + float sampleRate = 16000.0f; + float srcRatio = sampleRate / (float)inputSoundFileInfo.samplerate; + int outputFrames = ceilf(inputSoundFileInfo.frames * srcRatio); + + // Convert to mono + std::vector<float> monoData(inputSoundFileInfo.frames); + for(int i = 0; i < inputSoundFileInfo.frames; i++) + { + for(int j = 0; j < inputSoundFileInfo.channels; j++) + monoData[i] += audioIn[i * inputSoundFileInfo.channels + j]; + monoData[i] /= inputSoundFileInfo.channels; + } + + // Resample + SRC_DATA srcData; + srcData.data_in = monoData.data(); + srcData.input_frames = inputSoundFileInfo.frames; + + std::vector<float> dataOut(outputFrames); + srcData.data_out = dataOut.data(); + + srcData.output_frames = outputFrames; + srcData.src_ratio = srcRatio; + + src_simple(&srcData, SRC_SINC_BEST_QUALITY, 1); + + sf_close(infile); + + return dataOut; + } + + void AudioCapture::InitSlidingWindow(float* data, size_t dataSize, int minSamples, size_t stride) + { + this->m_window = SlidingWindow<const float>(data, dataSize, minSamples, stride); + } + + bool AudioCapture::HasNext() + { + return m_window.HasNext(); + } + + std::vector<float> AudioCapture::Next() + { + if (this->m_window.HasNext()) + { + int remainingData = this->m_window.RemainingData(); + const float* windowData = this->m_window.Next(); + + size_t windowSize = this->m_window.GetWindowSize(); + + if(remainingData < windowSize) + { + std::vector<float> audioData(windowSize, 0.0f); + for(int i = 0; i < remainingData; ++i) + { + audioData[i] = *windowData; + if(i < remainingData - 1) + { + ++windowData; + } + } + return audioData; + } + else + { + std::vector<float> audioData(windowData, windowData + windowSize); + return audioData; + } + } + else + { + throw std::out_of_range("Error, end of audio data reached."); + } + } +} //namespace asr
\ No newline at end of file diff --git a/samples/common/src/Audio/MFCC.cpp b/samples/common/src/Audio/MFCC.cpp new file mode 100644 index 0000000000..911c32b26e --- /dev/null +++ b/samples/common/src/Audio/MFCC.cpp @@ -0,0 +1,354 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// +#include "MFCC.hpp" +#include "MathUtils.hpp" + +#include <cfloat> +#include <cinttypes> +#include <cstring> + +MfccParams::MfccParams( + const float samplingFreq, + const int numFbankBins, + const float melLoFreq, + const float melHiFreq, + const int numMfccFeats, + const int frameLen, + const bool useHtkMethod, + const int numMfccVectors): + m_samplingFreq(samplingFreq), + m_numFbankBins(numFbankBins), + m_melLoFreq(melLoFreq), + m_melHiFreq(melHiFreq), + m_numMfccFeatures(numMfccFeats), + m_frameLen(frameLen), + m_numMfccVectors(numMfccVectors), + /* Smallest power of 2 >= frame length. */ + m_frameLenPadded(pow(2, ceil((log(frameLen)/log(2))))), + m_useHtkMethod(useHtkMethod) +{} + +std::string MfccParams::Str() +{ + char strC[1024]; + snprintf(strC, sizeof(strC) - 1, "\n \ + \n\t Sampling frequency: %f\ + \n\t Number of filter banks: %u\ + \n\t Mel frequency limit (low): %f\ + \n\t Mel frequency limit (high): %f\ + \n\t Number of MFCC features: %u\ + \n\t Frame length: %u\ + \n\t Padded frame length: %u\ + \n\t Using HTK for Mel scale: %s\n", + this->m_samplingFreq, this->m_numFbankBins, this->m_melLoFreq, + this->m_melHiFreq, this->m_numMfccFeatures, this->m_frameLen, + this->m_frameLenPadded, this->m_useHtkMethod ? "yes" : "no"); + return std::string{strC}; +} + +MFCC::MFCC(const MfccParams& params): + m_params(params), + m_filterBankInitialised(false) +{ + this->m_buffer = std::vector<float>( + this->m_params.m_frameLenPadded, 0.0); + this->m_frame = std::vector<float>( + this->m_params.m_frameLenPadded, 0.0); + this->m_melEnergies = std::vector<float>( + this->m_params.m_numFbankBins, 0.0); + + this->m_windowFunc = std::vector<float>(this->m_params.m_frameLen); + const auto multiplier = static_cast<float>(2 * M_PI / this->m_params.m_frameLen); + + /* Create window function. */ + for (size_t i = 0; i < this->m_params.m_frameLen; i++) + { + this->m_windowFunc[i] = (0.5 - (0.5 * cosf(static_cast<float>(i) * multiplier))); + } + +} + +void MFCC::Init() +{ + this->InitMelFilterBank(); +} + +float MFCC::MelScale(const float freq, const bool useHTKMethod) +{ + if (useHTKMethod) + { + return 1127.0f * logf (1.0f + freq / 700.0f); + } + else + { + /* Slaney formula for mel scale. */ + float mel = freq / ms_freqStep; + + if (freq >= ms_minLogHz) + { + mel = ms_minLogMel + logf(freq / ms_minLogHz) / ms_logStep; + } + return mel; + } +} + +float MFCC::InverseMelScale(const float melFreq, const bool useHTKMethod) +{ + if (useHTKMethod) { + return 700.0f * (expf (melFreq / 1127.0f) - 1.0f); + } + else + { + /* Slaney formula for mel scale. */ + float freq = ms_freqStep * melFreq; + + if (melFreq >= ms_minLogMel) + { + freq = ms_minLogHz * expf(ms_logStep * (melFreq - ms_minLogMel)); + } + return freq; + } +} + + +bool MFCC::ApplyMelFilterBank( + std::vector<float>& fftVec, + std::vector<std::vector<float>>& melFilterBank, + std::vector<uint32_t>& filterBankFilterFirst, + std::vector<uint32_t>& filterBankFilterLast, + std::vector<float>& melEnergies) +{ + const size_t numBanks = melEnergies.size(); + + if (numBanks != filterBankFilterFirst.size() || + numBanks != filterBankFilterLast.size()) + { + printf("unexpected filter bank lengths\n"); + return false; + } + + 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 */ + const uint32_t firstIndex = filterBankFilterFirst[bin]; + const uint32_t lastIndex = std::min<uint32_t>(filterBankFilterLast[bin], fftVec.size() - 1); + + for (uint32_t i = firstIndex; i <= lastIndex && filterBankIter != end; i++) + { + float energyRep = sqrt(fftVec[i]); + melEnergy += (*filterBankIter++ * energyRep); + } + + melEnergies[bin] = melEnergy; + } + + return true; +} + +void MFCC::ConvertToLogarithmicScale(std::vector<float>& melEnergies) +{ + for (float& melEnergy : melEnergies) + { + melEnergy = logf(melEnergy); + } +} + +void MFCC::ConvertToPowerSpectrum() +{ + const uint32_t halfDim = this->m_buffer.size() / 2; + + /* Handle this special case. */ + float firstEnergy = this->m_buffer[0] * this->m_buffer[0]; + float lastEnergy = this->m_buffer[1] * this->m_buffer[1]; + + MathUtils::ComplexMagnitudeSquaredF32( + this->m_buffer.data(), + this->m_buffer.size(), + this->m_buffer.data(), + this->m_buffer.size()/2); + + this->m_buffer[0] = firstEnergy; + this->m_buffer[halfDim] = lastEnergy; +} + +std::vector<float> MFCC::CreateDCTMatrix( + const int32_t inputLength, + const int32_t coefficientCount) +{ + std::vector<float> dctMatrix(inputLength * coefficientCount); + + const float normalizer = sqrtf(2.0f/inputLength); + const float angleIncr = M_PI/inputLength; + float angle = 0; + + for (int32_t k = 0, m = 0; k < coefficientCount; k++, m += inputLength) + { + for (int32_t n = 0; n < inputLength; n++) + { + dctMatrix[m + n] = normalizer * cosf((n + 0.5f) * angle); + } + angle += angleIncr; + } + + return dctMatrix; +} + +float MFCC::GetMelFilterBankNormaliser( + const float& leftMel, + const float& rightMel, + const bool useHTKMethod) +{ + /* By default, no normalisation => return 1 */ + return 1.f; +} + +void MFCC::InitMelFilterBank() +{ + if (!this->IsMelFilterBankInited()) + { + this->m_melFilterBank = this->CreateMelFilterBank(); + this->m_dctMatrix = this->CreateDCTMatrix( + this->m_params.m_numFbankBins, + this->m_params.m_numMfccFeatures); + this->m_filterBankInitialised = true; + } +} + +bool MFCC::IsMelFilterBankInited() const +{ + return this->m_filterBankInitialised; +} + +void MFCC::MfccComputePreFeature(const std::vector<float>& audioData) +{ + this->InitMelFilterBank(); + + auto size = std::min(std::min(this->m_frame.size(), audioData.size()), + static_cast<size_t>(this->m_params.m_frameLen)) * sizeof(float); + std::memcpy(this->m_frame.data(), audioData.data(), size); + + /* Apply window function to input frame. */ + for(size_t i = 0; i < this->m_params.m_frameLen; i++) + { + this->m_frame[i] *= this->m_windowFunc[i]; + } + + /* Set remaining frame values to 0. */ + std::fill(this->m_frame.begin() + this->m_params.m_frameLen,this->m_frame.end(), 0); + + /* Compute FFT. */ + MathUtils::FftF32(this->m_frame, this->m_buffer); + + /* Convert to power spectrum. */ + this->ConvertToPowerSpectrum(); + + /* Apply mel filterbanks. */ + if (!this->ApplyMelFilterBank(this->m_buffer, + this->m_melFilterBank, + this->m_filterBankFilterFirst, + this->m_filterBankFilterLast, + this->m_melEnergies)) + { + printf("Failed to apply MEL filter banks\n"); + } + + /* Convert to logarithmic scale. */ + this->ConvertToLogarithmicScale(this->m_melEnergies); +} + +std::vector<float> MFCC::MfccCompute(const std::vector<float>& audioData) +{ + this->MfccComputePreFeature(audioData); + + std::vector<float> mfccOut(this->m_params.m_numMfccFeatures); + + float * ptrMel = this->m_melEnergies.data(); + float * ptrDct = this->m_dctMatrix.data(); + float * ptrMfcc = mfccOut.data(); + + /* Take DCT. Uses matrix mul. */ + for (size_t i = 0, j = 0; i < mfccOut.size(); + ++i, j += this->m_params.m_numFbankBins) + { + *ptrMfcc++ = MathUtils::DotProductF32( + ptrDct + j, + ptrMel, + this->m_params.m_numFbankBins); + } + return mfccOut; +} + +std::vector<std::vector<float>> MFCC::CreateMelFilterBank() +{ + size_t numFftBins = this->m_params.m_frameLenPadded / 2; + float fftBinWidth = static_cast<float>(this->m_params.m_samplingFreq) / this->m_params.m_frameLenPadded; + + float melLowFreq = MFCC::MelScale(this->m_params.m_melLoFreq, + this->m_params.m_useHtkMethod); + float melHighFreq = MFCC::MelScale(this->m_params.m_melHiFreq, + this->m_params.m_useHtkMethod); + float melFreqDelta = (melHighFreq - melLowFreq) / (this->m_params.m_numFbankBins + 1); + + std::vector<float> thisBin = std::vector<float>(numFftBins); + std::vector<std::vector<float>> melFilterBank( + this->m_params.m_numFbankBins); + this->m_filterBankFilterFirst = + std::vector<uint32_t>(this->m_params.m_numFbankBins); + this->m_filterBankFilterLast = + std::vector<uint32_t>(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; + + 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++) + { + float freq = (fftBinWidth * i); /* Center freq of this fft bin. */ + float mel = MFCC::MelScale(freq, this->m_params.m_useHtkMethod); + thisBin[i] = 0.0; + + if (mel > leftMel && mel < rightMel) + { + float weight; + if (mel <= centerMel) + { + weight = (mel - leftMel) / (centerMel - leftMel); + } + else + { + weight = (rightMel - mel) / (rightMel - centerMel); + } + + thisBin[i] = weight * normaliser; + if (!firstIndexFound) + { + firstIndex = i; + firstIndexFound = true; + } + lastIndex = i; + } + } + + this->m_filterBankFilterFirst[bin] = firstIndex; + this->m_filterBankFilterLast[bin] = lastIndex; + + /* Copy the part we care about. */ + for (uint32_t i = firstIndex; i <= lastIndex; i++) + { + melFilterBank[bin].push_back(thisBin[i]); + } + } + + return melFilterBank; +} diff --git a/samples/common/src/Audio/MathUtils.cpp b/samples/common/src/Audio/MathUtils.cpp new file mode 100644 index 0000000000..d91b5098e1 --- /dev/null +++ b/samples/common/src/Audio/MathUtils.cpp @@ -0,0 +1,111 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "MathUtils.hpp" +#include <vector> +#include <cmath> +#include <cstdio> + +void MathUtils::FftF32(std::vector<float>& input, + std::vector<float>& fftOutput) +{ + const int inputLength = input.size(); + + for (int k = 0; k <= inputLength / 2; k++) + { + float sumReal = 0, sumImag = 0; + + for (int t = 0; t < inputLength; t++) + { + float angle = 2 * M_PI * t * k / inputLength; + sumReal += input[t] * cosf(angle); + sumImag += -input[t] * sinf(angle); + } + + /* Arrange output to [real0, realN/2, real1, im1, real2, im2, ...] */ + if (k == 0) + { + fftOutput[0] = sumReal; + } + else if (k == inputLength / 2) + { + fftOutput[1] = sumReal; + } + else + { + fftOutput[k*2] = sumReal; + fftOutput[k*2 + 1] = sumImag; + }; + } +} + +float MathUtils::DotProductF32(const float* srcPtrA, float* srcPtrB, + const int srcLen) +{ + float output = 0.f; + + for (int i = 0; i < srcLen; ++i) + { + output += *srcPtrA++ * *srcPtrB++; + } + return output; +} + +bool MathUtils::ComplexMagnitudeSquaredF32(const float* ptrSrc, + int srcLen, + float* ptrDst, + int dstLen) +{ + if (dstLen < srcLen/2) + { + printf("dstLen must be greater than srcLen/2"); + return false; + } + + for (int j = 0; j < dstLen; ++j) + { + const float real = *ptrSrc++; + const float im = *ptrSrc++; + *ptrDst++ = real*real + im*im; + } + return true; +} + +void MathUtils::VecLogarithmF32(std::vector <float>& input, + std::vector <float>& output) +{ + for (auto in = input.begin(), out = output.begin(); + in != input.end(); ++in, ++out) + { + *out = logf(*in); + } +} + +float MathUtils::MeanF32(const float* ptrSrc, const uint32_t srcLen) +{ + if (!srcLen) + { + return 0.f; + } + + float acc = std::accumulate(ptrSrc, ptrSrc + srcLen, 0.0); + return acc/srcLen; +} + +float MathUtils::StdDevF32(const float* ptrSrc, uint32_t srcLen, float mean) +{ + if (!srcLen) + { + return 0.f; + } + auto VarianceFunction = [mean, srcLen](float acc, const float value) { + return acc + (((value - mean) * (value - mean))/ srcLen); + }; + + float acc = std::accumulate(ptrSrc, ptrSrc + srcLen, 0.0, + VarianceFunction); + return sqrtf(acc); +} + diff --git a/samples/common/test/Audio/AudioCaptureTest.cpp b/samples/common/test/Audio/AudioCaptureTest.cpp new file mode 100644 index 0000000000..b8ea7b285c --- /dev/null +++ b/samples/common/test/Audio/AudioCaptureTest.cpp @@ -0,0 +1,61 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#define CATCH_CONFIG_MAIN +#include <catch.hpp> +#include <limits> + +#include "AudioCapture.hpp" + +TEST_CASE("Test capture of audio file") +{ + std::string testResources = TEST_RESOURCE_DIR; + REQUIRE(testResources != ""); + std::string file = testResources + "/" + "myVoiceIsMyPassportVerifyMe04.wav"; + audio::AudioCapture capture; + std::vector<float> audioData = capture.LoadAudioFile(file); + capture.InitSlidingWindow(audioData.data(), audioData.size(), 47712, 16000); + + std::vector<float> firstAudioBlock = capture.Next(); + float actual1 = firstAudioBlock.at(0); + float actual2 = firstAudioBlock.at(47000); + CHECK(std::to_string(actual1) == "0.000352"); + CHECK(std::to_string(actual2) == "-0.056441"); + CHECK(firstAudioBlock.size() == 47712); + + CHECK(capture.HasNext() == true); + + std::vector<float> secondAudioBlock = capture.Next(); + float actual3 = secondAudioBlock.at(0); + float actual4 = secondAudioBlock.at(47000); + CHECK(std::to_string(actual3) == "0.102077"); + CHECK(std::to_string(actual4) == "0.000194"); + CHECK(capture.HasNext() == true); + + std::vector<float> thirdAudioBlock = capture.Next(); + float actual5 = thirdAudioBlock.at(0); + float actual6 = thirdAudioBlock.at(33500); + float actual7 = thirdAudioBlock.at(33600); + CHECK(std::to_string(actual5) == "-0.076416"); + CHECK(std::to_string(actual6) == "-0.000275"); + CHECK(std::to_string(actual7) == "0.000000"); + CHECK(capture.HasNext() == false); +} + +TEST_CASE("Test sliding window of audio capture") +{ + std::string testResources = TEST_RESOURCE_DIR; + REQUIRE(testResources != ""); + std::string file = testResources + "/" + "myVoiceIsMyPassportVerifyMe04.wav"; + audio::AudioCapture capture; + std::vector<float> audioData = capture.LoadAudioFile(file); + capture.InitSlidingWindow(audioData.data(), audioData.size(), 47712, 16000); + capture.Next(); + capture.Next(); + + CHECK(capture.HasNext() == true); + capture.Next(); + CHECK(capture.HasNext() == false); +} diff --git a/samples/common/test/Audio/MathUtilsTest.cpp b/samples/common/test/Audio/MathUtilsTest.cpp new file mode 100644 index 0000000000..d7a435db56 --- /dev/null +++ b/samples/common/test/Audio/MathUtilsTest.cpp @@ -0,0 +1,112 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include <catch.hpp> +#include <limits> + +#include "MathUtils.hpp" +#include <iostream> +#include <numeric> + +TEST_CASE("Test DotProductF32") +{ + // Test Constants: + const int length = 6; + + float inputA[] = { 1, 1, 1, 0, 0, 0 }; + float inputB[] = { 0, 0, 0, 1, 1, 1 }; + + float dot_prod = MathUtils::DotProductF32(inputA, inputB, length); + float expectedResult = 0; + CHECK(dot_prod == expectedResult); +} + +TEST_CASE("Test FFT32") +{ + // Test Constants: + std::vector<float> input(32, 0); + std::vector<float> output(32); + std::vector<float> expectedResult(32, 0); + + MathUtils::FftF32(input, output); + + // To avoid common failed assertions due to rounding of near-zero values a small offset is added + transform(output.begin(), output.end(), output.begin(), + bind2nd(std::plus<double>(), 0.1)); + + transform(expectedResult.begin(), expectedResult.end(), expectedResult.begin(), + bind2nd(std::plus<double>(), 0.1)); + + for (int i = 0; i < output.size(); i++) + { + CHECK (expectedResult[i] == Approx(output[i])); + } +} + +TEST_CASE("Test ComplexMagnitudeSquaredF32") +{ + // Test Constants: + float input[] = { 0.0, 0.0, 0.5, 0.5,1,1 }; + int inputLen = (sizeof(input)/sizeof(*input)); + float expectedResult[] = { 0.0, 0.5, 2 }; + int outputLen = inputLen/2; + float output[outputLen]; + + MathUtils::ComplexMagnitudeSquaredF32(input, inputLen, output, outputLen); + + for (int i = 0; i < outputLen; i++) + { + CHECK (expectedResult[i] == Approx(output[i])); + } +} + +TEST_CASE("Test VecLogarithmF32") +{ + // Test Constants: + + std::vector<float> input = { 1, 0.1e-10 }; + std::vector<float> expectedResult = { 0, -25.328436 }; + std::vector<float> output(input.size()); + MathUtils::VecLogarithmF32(input,output); + + for (int i = 0; i < input.size(); i++) + { + CHECK (expectedResult[i] == Approx(output[i])); + } +} + +TEST_CASE("Test MeanF32") +{ + // Test Constants: + float input[] = { 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 1.000 }; + uint32_t inputLen = (sizeof(input)/sizeof(*input)); + float output; + + // Manually calculated mean of above array + float expectedResult = 0.100; + output = MathUtils::MeanF32(input, inputLen); + + CHECK (expectedResult == Approx(output)); +} + +TEST_CASE("Test StdDevF32") +{ + // Test Constants: + + float input[] = { 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 1.000 }; + + uint32_t inputLen = (sizeof(input)/sizeof(*input)); + + // Calculate mean using std library to avoid dependency on MathUtils::MeanF32 + float mean = (std::accumulate(input, input + inputLen, 0.0f))/float(inputLen); + + float output = MathUtils::StdDevF32(input, inputLen, mean); + + // Manually calculated standard deviation of above array + float expectedResult = 0.300; + + CHECK (expectedResult == Approx(output)); +} + |