diff --git a/src/shared/algorithms/Filters/LowPass.h b/src/shared/algorithms/Filters/LowPass.h index 725c9bd1389f14d18748b907832abeecd0c4e81b..49f5c2b7e5fa5c5218b012ebd755ad9db72ced0a 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 dfa644379bece21ca24cd86dba05ddef41470f9b..0ce072078faa528dc987efcd807d009e168a68a4 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 c714d5bae0ef7d064e4cdd79974436a8f74c58c4..0e3d99ce90d952a509f1753673504d0bda48132b 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 0000000000000000000000000000000000000000..0ee8e7c7260340f78a37d4ca07a7d25e6580ca31 --- /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 b58e59960572d75a30b3b4f5ac3d43f3ff8fc568..aad11791ecba9136e5d0e4bae284cb2830301083 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 4df2ac03c59be22d3ec04a4d945ed379746935a5..90a717bce35233fd72d29d2d3dc3d3fa1837a3eb 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);