From 70dd8d3c02424348de64ea5cec7ee1dc4c37148e Mon Sep 17 00:00:00 2001
From: Giulio Gaio <giulio.gaio@elettra.eu>
Date: Mon, 31 Jan 2022 21:20:44 +0100
Subject: [PATCH] Clean lp correlation history from zeros

---
 src/CalcStats.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/CalcStats.py b/src/CalcStats.py
index cb513e2..05c4289 100755
--- a/src/CalcStats.py
+++ b/src/CalcStats.py
@@ -811,8 +811,12 @@ class DeviceObj:
 						# calculate the correlation, numpy indexes start from 1 
 						self.corr[t_idx] = np.corrcoef(sensor_array,target_array)[0,1]
 						# low pass filter correlation
-						self.old_lp_filter_corr[t_idx] = self.lp_filter_corr[t_idx];
-						self.lp_filter_corr[t_idx] = lp_filter * np.absolute(self.corr[t_idx]) + (1 - lp_filter) * self.old_lp_filter_corr[t_idx]
+						# if data is 0/inf/NaN keep the previous value in order to avoid corrupting the filtered value
+						if (self.corr[t_idx] == 0) or np.isnan(self.corr[t_idx]) or np.isinf(self.corr[t_idx])
+							self.lp_filter_corr[t_idx] = self.old_lp_filter_corr[t_idx]
+						else	
+							self.old_lp_filter_corr[t_idx] = self.lp_filter_corr[t_idx];
+							self.lp_filter_corr[t_idx] = lp_filter * np.absolute(self.corr[t_idx]) + (1 - lp_filter) * self.old_lp_filter_corr[t_idx]
 
 						# correlation calculated with the bottom half of sensor values
 						self.corr_low[t_idx] = np.corrcoef(sensor_array_sort[1:int(self.valid_samples/2)],target_array_sort[1:int(self.valid_samples/2)])[0,1]
-- 
GitLab