From f687a393411d0743213c5d4f56a2860796c8ec6f Mon Sep 17 00:00:00 2001 From: Federico Lolli <federico.lolli@skywarder.eu> Date: Wed, 25 Oct 2023 10:40:42 +0200 Subject: [PATCH] [SFD] respect naming guidelines and small const extraction refactor --- src/shared/algorithms/Filters/LowPass.h | 5 +---- src/shared/algorithms/SFD/SFDAscent.cpp | 8 ++++---- src/shared/algorithms/SFD/SFDAscent.h | 15 +++++++------- src/shared/algorithms/SFD/SFDCommon.h | 26 ++++++++++++++++++++++++ src/shared/algorithms/SFD/SFDDescent.cpp | 4 ++-- src/shared/algorithms/SFD/SFDDescent.h | 15 +++++++------- 6 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 src/shared/algorithms/SFD/SFDCommon.h diff --git a/src/shared/algorithms/Filters/LowPass.h b/src/shared/algorithms/Filters/LowPass.h index 725c9bd13..49f5c2b7e 100644 --- a/src/shared/algorithms/Filters/LowPass.h +++ b/src/shared/algorithms/Filters/LowPass.h @@ -48,10 +48,7 @@ public: float filter(float input); private: - float cutoffFreq; - float lambda; - float gain; - float output; + float gain, cutoffFreq, lambda, output; }; } // namespace Boardcore diff --git a/src/shared/algorithms/SFD/SFDAscent.cpp b/src/shared/algorithms/SFD/SFDAscent.cpp index dfa644379..0ce072078 100644 --- a/src/shared/algorithms/SFD/SFDAscent.cpp +++ b/src/shared/algorithms/SFD/SFDAscent.cpp @@ -29,7 +29,7 @@ namespace Boardcore { -SFDAscent::SFDAscent(const SFDAConfig& config) : svm(config.modelParameters) {} +SFDAscent::SFDAscent(const SFDAscentConfig& config) : svm(config.modelParameters) {} SFDAscent::FeaturesVec SFDAscent::getFeatures(const VectorIn& input) { @@ -41,17 +41,17 @@ SFDAscent::FeaturesVec SFDAscent::getFeatures(const VectorIn& input) min = input.minCoeff(); max = input.maxCoeff(); delta = max - min; - for (int i = 0; i < lenChunk; i++) + for (int i = 0; i < LEN_CHUNK; i++) data(i) = (input(i) - min) / (std::max(delta, 1e-25f) * 2) - 1; u = data.mean(); x0 = data - u * VectorIn::Ones(); - var = x0.squaredNorm() / lenChunk; + var = x0.squaredNorm() / LEN_CHUNK; s2 = x0.array().pow(2).mean(); m4 = x0.array().pow(4).mean(); rfourier = FFT32::fft(data).real(); // TODO: fix complex -> float rfmean = rfourier.mean(); - rfvar = (rfourier - rfmean * VectorIn::Ones()).squaredNorm() / lenChunk; + rfvar = (rfourier - rfmean * VectorIn::Ones()).squaredNorm() / LEN_CHUNK; features(0) = delta; features(1) = var; diff --git a/src/shared/algorithms/SFD/SFDAscent.h b/src/shared/algorithms/SFD/SFDAscent.h index c714d5bae..0e3d99ce9 100644 --- a/src/shared/algorithms/SFD/SFDAscent.h +++ b/src/shared/algorithms/SFD/SFDAscent.h @@ -28,25 +28,26 @@ #include <Eigen/Core> +#include "SFDCommon.h" + namespace Boardcore { class SFDAscent { public: - static constexpr int numFeatures = 6; - static constexpr int lenChunk = 32; + static constexpr int NUM_FEATURES = 6; - using SVMn = SVM<numFeatures>; - using FeaturesVec = Eigen::Vector<float, numFeatures>; - using VectorIn = Eigen::Vector<float, lenChunk>; + using SVMn = SVM<NUM_FEATURES>; + using FeaturesVec = Eigen::Vector<float, NUM_FEATURES>; + using VectorIn = Eigen::Vector<float, LEN_CHUNK>; - struct SFDAConfig + struct SFDAscentConfig { SVMn::SVMConfig modelParameters; }; - explicit SFDAscent(const SFDAConfig& config); + explicit SFDAscent(const SFDAscentConfig& config); bool classify(const VectorIn& input); diff --git a/src/shared/algorithms/SFD/SFDCommon.h b/src/shared/algorithms/SFD/SFDCommon.h new file mode 100644 index 000000000..0ee8e7c72 --- /dev/null +++ b/src/shared/algorithms/SFD/SFDCommon.h @@ -0,0 +1,26 @@ + +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Author: Federico Lolli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +static constexpr int LEN_CHUNK = 32; diff --git a/src/shared/algorithms/SFD/SFDDescent.cpp b/src/shared/algorithms/SFD/SFDDescent.cpp index b58e59960..aad11791e 100644 --- a/src/shared/algorithms/SFD/SFDDescent.cpp +++ b/src/shared/algorithms/SFD/SFDDescent.cpp @@ -27,7 +27,7 @@ namespace Boardcore { -SFDDescent::SFDDescent(const SFDDConfig& config) : svm(config.modelParameters) +SFDDescent::SFDDescent(const SFDDescentConfig& config) : svm(config.modelParameters) { } @@ -41,7 +41,7 @@ SFDDescent::FeaturesVec SFDDescent::getFeatures(const VectorIn& input) min = input.minCoeff(); max = input.maxCoeff(); delta = max - min; - for (int i = 0; i < lenChunk; i++) + for (int i = 0; i < LEN_CHUNK; i++) data(i) = (input(i) - min) / (std::max(delta, 1e-25f) * 2) - 1; u = data.mean(); x0 = data - u * VectorIn::Ones(); diff --git a/src/shared/algorithms/SFD/SFDDescent.h b/src/shared/algorithms/SFD/SFDDescent.h index 4df2ac03c..90a717bce 100644 --- a/src/shared/algorithms/SFD/SFDDescent.h +++ b/src/shared/algorithms/SFD/SFDDescent.h @@ -28,25 +28,26 @@ #include <Eigen/Core> +#include "SFDCommon.h" + namespace Boardcore { class SFDDescent { public: - static constexpr int numFeatures = 5; - static constexpr int lenChunk = 32; + static constexpr int NUM_FEATURES = 5; - using SVMn = SVM<numFeatures>; - using FeaturesVec = Eigen::Vector<float, numFeatures>; - using VectorIn = Eigen::Vector<float, lenChunk>; + using SVMn = SVM<NUM_FEATURES>; + using FeaturesVec = Eigen::Vector<float, NUM_FEATURES>; + using VectorIn = Eigen::Vector<float, LEN_CHUNK>; - struct SFDDConfig + struct SFDDescentConfig { SVMn::SVMConfig modelParameters; }; - explicit SFDDescent(const SFDDConfig& config); + explicit SFDDescent(const SFDDescentConfig& config); bool classify(const VectorIn& input); -- GitLab