From afe88fcf45157037a0283b00f38fef45b0c350f0 Mon Sep 17 00:00:00 2001
From: Manuel Pitz <manuel.pitz@eonerc.rwth-aachen.de>
Date: Wed, 16 Jun 2021 14:00:52 +0000
Subject: [PATCH] first version of prabolic freq estimation

---
 lib/hooks/dft.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/lib/hooks/dft.cpp b/lib/hooks/dft.cpp
index 08dde4586..82a052835 100644
--- a/lib/hooks/dft.cpp
+++ b/lib/hooks/dft.cpp
@@ -779,6 +779,15 @@ public:
 	{
 		assert(state == State::PARSED);
 
+		if (!freqEstimateTypeC) {
+			logger->info("No Frequency estimation type given, assume no none");
+			freqEstType = FreqEstimationType::NONE;
+		}
+		else if (strcmp(freqEstimateTypeC, "quadratic") == 0)
+			freqEstType = FreqEstimationType::QUADRATIC;
+
+
+
 		if (endFreqency < 0 || endFreqency > sampleRate)
 			throw RuntimeError("End frequency must be smaller than sampleRate {}", sampleRate);
 
@@ -1142,6 +1151,48 @@ static HookPlugin<DftHook, n, d, (int) Hook::Flags::NODE_READ | (int) Hook::Flag
 
 		return { y_Fmax, i };
 	}
+
+
+	/**
+	 * This function is calculating the mximum based on a quadratic interpolation
+	 * 
+	 * This function is based on the following paper:
+	 * https://mgasior.web.cern.ch/pap/biw2004.pdf
+	 * 
+	 * In particular equation 10
+	 * 
+	 */
+	Point quadraticEstimation(double x1, double x2, double x3, double y1, double y2, double y3, unsigned maxFBin)
+	{
+
+		//double y_max = x2 - (y3 - y1) / (2 * ( 2 * y2  - y1 - y3));
+		double y_Fmax = 0;
+
+		/*Quadratic Method */
+		double maxBinEst =  ((y3 - y1) / ( 2 * ( 2 * y2 - y1 - y3)));
+		
+
+
+		logger->info("y3 - y1 {} ; y_max : {}", (y3 - y1), maxBinEst);
+
+		maxBinEst = (double)maxFBin + maxBinEst;
+
+		/*Jain’s Method
+		if (y1 > y3)
+		{
+			y_max = (double)maxFBin - 1 + (((y2 / y1) / ( 1 + (y2/y1))));
+		}
+		else
+		{
+			y_max = (double)maxFBin + ((y3 / y2) / ( 1 + (y3 / y2)));
+		}*/
+		
+		y_Fmax = startFreqency + maxBinEst * frequencyResolution;
+
+		Point ret = {y_Fmax, 0};
+
+		return ret;
+	}
 };
 
 /* Register hook */