From 84692a4212e5349f92c6a08c07ca600f31a4e25d Mon Sep 17 00:00:00 2001
From: Erik Kooistra <kooistra@astron.nl>
Date: Wed, 6 Apr 2016 09:34:52 +0000
Subject: [PATCH] Added normalize output and quantize.

---
 applications/apertif/matlab/pfft.m | 18 ++++++++++++------
 applications/apertif/matlab/pfir.m | 10 +++++++++-
 2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/applications/apertif/matlab/pfft.m b/applications/apertif/matlab/pfft.m
index 9d30aab843..de868e147d 100644
--- a/applications/apertif/matlab/pfft.m
+++ b/applications/apertif/matlab/pfft.m
@@ -23,13 +23,19 @@
 % Purpose : FFT per block of data
 % Description :
 
-function [data] = pfft(ctrl, data)
+function [out_data] = pfft(ctrl, in_data)
     % Data processing
-    dat = fft(data, ctrl.fft_size);
+    out_data = fft(in_data, ctrl.fft_size);
     
-    if ctrl.complex
-        data = dat;
-    else
+    if ~ctrl.complex
         % because we have real input, we can omit half of the fft result
-        data = dat(1:ctrl.fft_size/2);
+        out_data = out_data(1:ctrl.fft_size/2);
+    end
+
+    % Normalize output to [-1 1]
+    out_data = out_data / ctrl.gain;
+    
+    % Quantization
+    if ctrl.data_w>0
+        out_data = quantize(out_data, 1, ctrl.data_w, 'half_away');
     end
diff --git a/applications/apertif/matlab/pfir.m b/applications/apertif/matlab/pfir.m
index 73d7ce43f8..1f2f70d28e 100644
--- a/applications/apertif/matlab/pfir.m
+++ b/applications/apertif/matlab/pfir.m
@@ -26,7 +26,15 @@
 function [state, out_data] = pfir(ctrl, in_data)
     % Data processing
     for t = 1:ctrl.downsample_factor
-        [out_data(t), ctrl.Zdelays(t,:)] = filter(ctrl.coeff(t,:), 1, in_data(t), ctrl.Zdelays(t,:));   % ctrl.nof_taps coefficients and ctrl.nof_taps-1 Zdelays
+        [out_data(t), ctrl.Zdelays(t,:)] = filter(ctrl.coeff(t,:), 1, in_data(t), ctrl.Zdelays(t,:));   % range ctrl.nof_taps coefficients and range ctrl.nof_taps-1 Zdelays
+    end
+    
+    % Normalize output to [-1 1]
+    out_data = out_data / ctrl.gain;
+
+    % Quantization
+    if ctrl.data_w>0
+        out_data = quantize(out_data, 2^ctrl.scale_w, ctrl.data_w, 'half_away');
     end
     
     % Keep state for next call
-- 
GitLab