From 8354abbf73de0509fd5909186f0cf46a4734209f Mon Sep 17 00:00:00 2001
From: prog <youssef@airspy.com>
Date: Fri, 15 Jun 2018 00:00:42 +0200
Subject: [PATCH 3/7] Improved the image rejection near DC.

---
 libairspyhf/src/airspyhf.h   | 4 ++--
 libairspyhf/src/iqbalancer.c | 8 +++++---
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/libairspyhf/src/airspyhf.h b/libairspyhf/src/airspyhf.h
index b68cbec..06fe5b6 100644
--- a/libairspyhf/src/airspyhf.h
+++ b/libairspyhf/src/airspyhf.h
@@ -28,10 +28,10 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSI
 
 #include <stdint.h>
 
-#define AIRSPYHF_VERSION "1.1.5"
+#define AIRSPYHF_VERSION "1.1.6"
 #define AIRSPYHF_VER_MAJOR 1
 #define AIRSPYHF_VER_MINOR 1
-#define AIRSPYHF_VER_REVISION 5
+#define AIRSPYHF_VER_REVISION 6
 
 #define AIRSPYHF_ENDPOINT_IN (1)
 
diff --git a/libairspyhf/src/iqbalancer.c b/libairspyhf/src/iqbalancer.c
index fc988ff..9ccecd9 100644
--- a/libairspyhf/src/iqbalancer.c
+++ b/libairspyhf/src/iqbalancer.c
@@ -204,19 +204,21 @@ static float utility(iq_balancer_t *iq_balancer, airspyhf_complex_float_t* iq, f
 	float acc2 = 0.0f;
 	float max1 = 0.0f;
 	float max2 = 0.0f;
+	float invskip = 1.0f / BinsToSkip;
 	int count1 = 0;
 	int count2 = 0;
 
-	for (int i = 1 + BinsToSkip, j = FFTBins - 1 - BinsToSkip; i < FFTBins / 2 - BinsToSkip; i++, j--)
+	for (int i = 1, j = FFTBins - 1; i < FFTBins / 2 - BinsToSkip; i++, j--)
 	{
 		airspyhf_complex_float_t prod = multiply_complex_complex(fftPtr + i, fftPtr + j);
 		float corr = prod.re * prod.re + prod.im * prod.im;
 		float m1 = fftPtr[i].re * fftPtr[i].re + fftPtr[i].im * fftPtr[i].im;
 		float m2 = fftPtr[j].re * fftPtr[j].re + fftPtr[j].im * fftPtr[j].im;
+		float weight = (i > BinsToSkip) ? 1.0f : (i * invskip);
 
 		if (i >= iq_balancer->optimal_bin - BinsToOptimize / 2 && i <= iq_balancer->optimal_bin + BinsToOptimize / 2)
 		{
-			acc1 += corr;
+			acc1 += corr * weight;
 			if (max1 < m1)
 			{
 				max1 = m1;
@@ -229,7 +231,7 @@ static float utility(iq_balancer_t *iq_balancer, airspyhf_complex_float_t* iq, f
 		}
 		else
 		{
-			acc2 += corr;
+			acc2 += corr * weight;
 			if (max2 < m1)
 			{
 				max2 = m1;
-- 
2.11.0

