From 9cd8b28e116a4b5445dacb6b919da68b49a5cc2e Mon Sep 17 00:00:00 2001
From: Carl Philipp Klemm <philipp@uvos.xyz>
Date: Fri, 6 Jan 2023 14:44:27 +0100
Subject: [PATCH] eistype: add operator== to DataPoint, add sweep distance
 calculation function, add ability to add custom header to saved spectra

---
 eisgenerator/eistype.h | 12 +++++++++---
 eistype.cpp            | 18 +++++++++++++++++-
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/eisgenerator/eistype.h b/eisgenerator/eistype.h
index 595ecb8..6224047 100644
--- a/eisgenerator/eistype.h
+++ b/eisgenerator/eistype.h
@@ -7,10 +7,15 @@ typedef double fvalue;
 
 namespace eis
 {
-struct DataPoint
+class DataPoint
 {
+public:
 	std::complex<fvalue> im;
 	fvalue omega;
+	bool operator==(const DataPoint& in)
+	{
+		return im == in.im;
+	}
 };
 
 class Range
@@ -32,7 +37,6 @@ public:
 	{
 		return at(step);
 	}
-
 	fvalue at(size_t index) const
 	{
 		assert(index < count);
@@ -69,6 +73,8 @@ public:
 	static std::vector<Range> rangesFromParamString(const std::string& paramStr, size_t count);
 };
 
-bool saveToDisk(const std::vector<DataPoint>& data, std::string fileName);
+bool saveToDisk(const std::vector<DataPoint>& data, const std::string& fileName, std::string headStr = "");
+
+double eisDistance(const std::vector<eis::DataPoint>& a, const std::vector<eis::DataPoint>& b);
 
 }
diff --git a/eistype.cpp b/eistype.cpp
index 148487b..6276fdd 100644
--- a/eistype.cpp
+++ b/eistype.cpp
@@ -7,7 +7,7 @@
 
 using namespace eis;
 
-bool eis::saveToDisk(const std::vector<DataPoint>& data, std::string fileName)
+bool eis::saveToDisk(const std::vector<DataPoint>& data, const std::string& fileName, std::string headStr)
 {
 	std::fstream file;
 	file.open(fileName, std::ios_base::out | std::ios_base::trunc);
@@ -17,6 +17,8 @@ bool eis::saveToDisk(const std::vector<DataPoint>& data, std::string fileName)
 		return false;
 	}
 
+	if(!headStr.empty())
+		file<<headStr<<'\n';
 	file<<"omega,real,im\n";
 
 	for(const eis::DataPoint& point : data)
@@ -92,3 +94,17 @@ bool eis::Range::isSane() const
 		return false;
 	return true;
 }
+
+double eis::eisDistance(const std::vector<eis::DataPoint>& a, const std::vector<eis::DataPoint>& b)
+{
+	assert(a.size() == b.size());
+
+	double accum = 0;
+	for(size_t i = 0; i < a.size(); ++i)
+	{
+		double diffRe = std::pow(b[i].im.real() - a[i].im.real(), 2);
+		double diffIm = std::pow(b[i].im.imag() - a[i].im.imag(), 2);
+		accum += std::sqrt(diffRe+diffIm);
+	}
+	return accum/a.size();
+}
-- 
GitLab