From 2f67ef5c31caa84e2602de9941669130c03c6020 Mon Sep 17 00:00:00 2001
From: Federico Lolli <federico.lolli@skywarder.eu>
Date: Mon, 16 Oct 2023 16:55:19 +0200
Subject: [PATCH] [SFD] fixed eigen bugs (incorrect operators used)

---
 src/shared/algorithms/SFD/SFDAscent.cpp  | 10 ++++------
 src/shared/algorithms/SFD/SFDAscent.h    |  6 +++---
 src/shared/algorithms/SFD/SFDDescent.cpp |  8 +++-----
 src/shared/algorithms/SFD/SFDDescent.h   |  6 +++---
 src/shared/models/SVM.h                  |  2 +-
 5 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/src/shared/algorithms/SFD/SFDAscent.cpp b/src/shared/algorithms/SFD/SFDAscent.cpp
index fc5d6e54b..35b94568f 100644
--- a/src/shared/algorithms/SFD/SFDAscent.cpp
+++ b/src/shared/algorithms/SFD/SFDAscent.cpp
@@ -22,8 +22,6 @@
 
 // ======= Sensor Fault Detection Model (SFDAscent) =======
 
-#pragma once
-
 #include "SFDAscent.h"
 
 #include <algorithms/FFT.h>
@@ -48,17 +46,17 @@ SFDAscent::FeaturesVec SFDAscent::getFeatures(const VectorIn& input)
     u   = data.mean();
     x0  = data - u * VectorIn::Ones();
     var = x0.squaredNorm() / lenChunk;
-    s2  = x0.pow(2).mean();
-    m4  = x0.pow(4).mean();
+    s2  = x0.array().pow(2).mean();
+    m4  = x0.array().pow(4).mean();
 
-    rfourier = FFT32::fft(data);  // TODO: fix complex -> float
+    rfourier = FFT32::fft(data).real();  // TODO: fix complex -> float
     rfmean   = rfourier.mean();
     rfvar    = (rfourier - rfmean * VectorIn::Ones()).squaredNorm() / lenChunk;
 
     features(0) = delta;
     features(1) = var;
     features(2) = m4 / std::pow(s2, 2);
-    features(3) = data.pow(5).mean();
+    features(3) = data.array().pow(5).mean();
     features(4) = rfvar;
     features(5) = rfourier.cwiseAbs().sum();
 
diff --git a/src/shared/algorithms/SFD/SFDAscent.h b/src/shared/algorithms/SFD/SFDAscent.h
index 970cf31c7..9640d0c8f 100644
--- a/src/shared/algorithms/SFD/SFDAscent.h
+++ b/src/shared/algorithms/SFD/SFDAscent.h
@@ -37,13 +37,13 @@ public:
     static constexpr int numFeatures = 6;
     static constexpr int lenChunk    = 32;
 
-    using SVM         = SVM<numFeatures>;
+    using SVMn        = SVM<numFeatures>;
     using FeaturesVec = Eigen::Vector<float, numFeatures>;
     using VectorIn    = Eigen::Vector<float, lenChunk>;
 
     struct SFDAConfig
     {
-        SVM::SVMConfig modelParameters;
+        SVMn::SVMConfig modelParameters;
     };
 
     SFDAscent(const SFDAConfig& config);
@@ -51,7 +51,7 @@ public:
     bool classify(const VectorIn& input);
 
 private:
-    SVM svm;
+    SVMn svm;
 
     FeaturesVec getFeatures(const VectorIn& input);
 };
diff --git a/src/shared/algorithms/SFD/SFDDescent.cpp b/src/shared/algorithms/SFD/SFDDescent.cpp
index f88bc1682..7ea5283ac 100644
--- a/src/shared/algorithms/SFD/SFDDescent.cpp
+++ b/src/shared/algorithms/SFD/SFDDescent.cpp
@@ -22,8 +22,6 @@
 
 // ======= Sensor Fault Detection Model (SFDAscent) =======
 
-#pragma once
-
 #include "SFDDescent.h"
 
 namespace Boardcore
@@ -47,9 +45,9 @@ SFDDescent::FeaturesVec SFDDescent::getFeatures(const VectorIn& input)
         data(i) = (input(i) - min) / (std::max(delta, 1e-25f) * 2) - 1;
     u   = data.mean();
     x0  = data - u * VectorIn::Ones();
-    s2  = x0.pow(2).mean();
-    m3  = x0.pow(3).mean();
-    m4  = x0.pow(4).mean();
+    s2  = x0.array().pow(2).mean();
+    m3  = x0.array().pow(3).mean();
+    m4  = x0.array().pow(4).mean();
     rms = std::sqrt(s2);
 
     features(0) = data.cwiseAbs().maxCoeff() / rms;
diff --git a/src/shared/algorithms/SFD/SFDDescent.h b/src/shared/algorithms/SFD/SFDDescent.h
index 45c043a38..a0c332f4b 100644
--- a/src/shared/algorithms/SFD/SFDDescent.h
+++ b/src/shared/algorithms/SFD/SFDDescent.h
@@ -37,13 +37,13 @@ public:
     static constexpr int numFeatures = 5;
     static constexpr int lenChunk    = 32;
 
-    using SVM         = SVM<numFeatures>;
+    using SVMn        = SVM<numFeatures>;
     using FeaturesVec = Eigen::Vector<float, numFeatures>;
     using VectorIn    = Eigen::Vector<float, lenChunk>;
 
     struct SFDDConfig
     {
-        SVM::SVMConfig modelParameters;
+        SVMn::SVMConfig modelParameters;
     };
 
     SFDDescent(const SFDDConfig& config);
@@ -51,7 +51,7 @@ public:
     bool classify(const VectorIn& input);
 
 private:
-    SVM svm;
+    SVMn svm;
 
     FeaturesVec getFeatures(const VectorIn& input);
 };
diff --git a/src/shared/models/SVM.h b/src/shared/models/SVM.h
index fe019d659..27594b3d6 100644
--- a/src/shared/models/SVM.h
+++ b/src/shared/models/SVM.h
@@ -51,7 +51,7 @@ public:
     float score(VectorD input)
     {
         VectorD x = input - mu;
-        x /= sigma;
+        x         = x.array() / sigma.array();
         return -((x / scale).dot(beta) + bias);
     }
 
-- 
GitLab