From a084afd1028f3aba5bfe883d696d0c8954b14f33 Mon Sep 17 00:00:00 2001
From: Guillaume HEUSCH <guillaume.heusch@idiap.ch>
Date: Wed, 4 Jul 2018 11:34:26 +0200
Subject: [PATCH] [extractor] corrected 2-3 (minor ?) bugs on the feature
 computation

---
 bob/pad/face/extractor/LTSS.py | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/bob/pad/face/extractor/LTSS.py b/bob/pad/face/extractor/LTSS.py
index 2177e1ed..a47475d7 100644
--- a/bob/pad/face/extractor/LTSS.py
+++ b/bob/pad/face/extractor/LTSS.py
@@ -56,7 +56,10 @@ class LTSS(Extractor, object):
     """
     super(LTSS, self).__init__(**kwargs)
     self.framerate = framerate
+    
+    # TODO: try to use window size as NFFT - Guillaume HEUSCH, 04-07-2018
     self.nfft = nfft
+    
     self.debug = debug
     self.window_size = window_size
     self.concat = concat
@@ -80,27 +83,40 @@ class LTSS(Extractor, object):
 
     # log-magnitude of DFT coefficients
     log_mags = []
-   
+
     # go through windows
     for w in range(0, (signal.shape[0] - self.window_size), window_stride):
+      
+      # n is even, as a consequence the fft is as follows [y(0), Re(y(1)), Im(y(1)), ..., Re(y(n/2))]
+      # i.e. each coefficient, except first and last, is represented by two numbers (real + imaginary)
       fft = rfft(signal[w:w+self.window_size], n=self.nfft)
-      mags = numpy.zeros(int(self.nfft/2), dtype=numpy.float64)
       
-      # XXX : bug was here (no clipping)
+      # the magnitude is the norm of the complex numbers, so its size is n/2 + 1
+      mags = numpy.zeros((int(self.nfft/2) + 1), dtype=numpy.float64)
+      
+      # first coeff is real
       if abs(fft[0]) < 1:
         mags[0] = 1
       else:
         mags[0] = abs(fft[0])
-      # XXX 
 
+      # go through coeffs 2 to n/2
       index = 1
       for i in range(1, (fft.shape[0]-1), 2):
         mags[index] = numpy.sqrt(fft[i]**2 + fft[i+1]**2)
         if mags[index] < 1:
           mags[index] = 1
         index += 1
+      
+      # last coeff is real too
+      if abs(fft[-1]) < 1:
+        mags[index] = 1
+      else:
+        mags[index] = abs(fft[-1])
+      
       log_mags.append(numpy.log(mags))
 
+    # build final feature
     log_mags = numpy.array(log_mags)
     mean = numpy.mean(log_mags, axis=0)
     std = numpy.std(log_mags, axis=0)
-- 
GitLab