From bea53834c01c2655c4e5cef68c365dfd2b5ade27 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Leonardo=20Fl=C3=B3rez-Valencia?= <florez-l@javeriana.edu.co>
Date: Tue, 24 Oct 2017 14:00:31 -0500
Subject: [PATCH] ...

---
 lib/ivq/ITK/FourierSeries.cxx | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/lib/ivq/ITK/FourierSeries.cxx b/lib/ivq/ITK/FourierSeries.cxx
index 7fcadf0..c81ba5e 100644
--- a/lib/ivq/ITK/FourierSeries.cxx
+++ b/lib/ivq/ITK/FourierSeries.cxx
@@ -687,19 +687,20 @@ _DFT( const std::vector< TComplex >& p, unsigned int q )
   c /= N;
 
   // Minimize phase
-  long minIdx = 0;
-  TScalar minImag = std::fabs( std::imag( p[ 0 ] - c ) );
-  for( long m = 1; m < p.size( ); ++m )
+  struct _TComplexCmp
   {
-    TScalar y = std::fabs( std::imag( p[ m ] - c ) );
-    if( y < minImag )
-    {
-      minIdx = m;
-      minImag = y;
-
-    } // fi
-
-  } // rof
+    bool operator()( const TComplex& a, const TComplex& b )
+      {
+        return(
+          ( std::real( b ) < std::real( a ) ) &&
+          ( std::fabs( std::imag( a ) ) < std::fabs( std::imag( b ) ) )
+          );
+      }
+  };
+  std::map< TComplex, long, _TComplexCmp > ordered;
+  for( long m = 0; m < p.size( ); ++m )
+    ordered[ p[ m ] - c ] = m;
+  long si = ordered.begin( )->second;
 
   // Real DFT computation
   std::vector< TComplex > dft;
@@ -710,7 +711,7 @@ _DFT( const std::vector< TComplex >& p, unsigned int q )
     TComplex z( _0, _0 );
     for( long i = 0; i < p.size( ); ++i )
       z +=
-        p[ ( i + minIdx ) % p.size( ) ] *
+        p[ ( i + si ) % p.size( ) ] *
         std::polar( _1, _2piN * TScalar( m * i ) );
     z /= N;
     dft.push_back( z );
-- 
2.49.0