blob: 035ce67447cd1fa8d744080307fdb32949ccc02f [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00003 * 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 ADMELSPECTROGRAM_HPP
18#define ADMELSPECTROGRAM_HPP
19
20#include "MelSpectrogram.hpp"
21
22namespace arm {
23namespace app {
24namespace audio {
25
26 /* Class to provide anomaly detection specific Mel Spectrogram calculation requirements */
27 class AdMelSpectrogram : public MelSpectrogram {
28
29 public:
30 static constexpr uint32_t ms_defaultSamplingFreq = 16000;
31 static constexpr uint32_t ms_defaultNumFbankBins = 64;
32 static constexpr uint32_t ms_defaultMelLoFreq = 0;
33 static constexpr uint32_t ms_defaultMelHiFreq = 8000;
34 static constexpr bool ms_defaultUseHtkMethod = false;
35
36 explicit AdMelSpectrogram(const size_t frameLen)
37 : MelSpectrogram(MelSpecParams(
38 ms_defaultSamplingFreq, ms_defaultNumFbankBins,
39 ms_defaultMelLoFreq, ms_defaultMelHiFreq,
40 frameLen, ms_defaultUseHtkMethod))
41 {}
42
43 AdMelSpectrogram() = delete;
Richard Burtonec5e99b2022-10-05 11:00:37 +010044 virtual ~AdMelSpectrogram() = default;
alexander3c798932021-03-26 21:42:19 +000045
46 protected:
47
48 /**
49 * @brief Overrides base class implementation of this function.
50 * @param[in] fftVec Vector populated with FFT magnitudes
51 * @param[in] melFilterBank 2D Vector with filter bank weights
52 * @param[in] filterBankFilterFirst Vector containing the first indices of filter bank
53 * to be used for each bin.
54 * @param[in] filterBankFilterLast Vector containing the last indices of filter bank
55 * to be used for each bin.
56 * @param[out] melEnergies Pre-allocated vector of MEL energies to be
57 * populated.
58 * @return true if successful, false otherwise
59 */
60 virtual bool ApplyMelFilterBank(
61 std::vector<float>& fftVec,
62 std::vector<std::vector<float>>& melFilterBank,
alexanderc350cdc2021-04-29 20:36:09 +010063 std::vector<uint32_t>& filterBankFilterFirst,
64 std::vector<uint32_t>& filterBankFilterLast,
alexander3c798932021-03-26 21:42:19 +000065 std::vector<float>& melEnergies) override;
66
67 /**
68 * @brief Override for the base class implementation convert mel
69 * energies to logarithmic scale. The difference from
70 * default behaviour is that the power is converted to dB
71 * and subsequently clamped.
Isabella Gottardi56ee6202021-05-12 08:27:15 +010072 * @param[in,out] melEnergies - 1D vector of Mel energies
alexander3c798932021-03-26 21:42:19 +000073 **/
74 virtual void ConvertToLogarithmicScale(std::vector<float>& melEnergies) override;
75
76 /**
77 * @brief Given the low and high Mel values, get the normaliser
78 * for weights to be applied when populating the filter
79 * bank. Override for the base class implementation.
80 * @param[in] leftMel - low Mel frequency value
81 * @param[in] rightMel - high Mel frequency value
82 * @param[in] useHTKMethod - bool to signal if HTK method is to be
83 * used for calculation
84 * @return Return float value to be applied
85 * when populating the filter bank.
86 */
87 virtual float GetMelFilterBankNormaliser(
88 const float& leftMel,
89 const float& rightMel,
90 const bool useHTKMethod) override;
91 };
92
93} /* namespace audio */
94} /* namespace app */
95} /* namespace arm */
96
97#endif /* ADMELSPECTROGRAM_HPP */