title: The complex conjugate transpose of an array in c++
body: <p>I am trying to write a code in c++ equivalents to the following Matlab code:</p>
  
  <pre class="lang-matlab prettyprint-override"><code>n = 32;
  s1 = dlmread('ssa.txt');
  s2 = dlmread('ssb.txt');
  fd1 = fft(s1);
  fd2 = fft(s2);
  nf = sqrt((fd1 * fd1') * (fd2 * fd2')) / n
  </code></pre>
  
  <p>Where</p>
  
  <pre><code>ssa.txt=  0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
  </code></pre>
  
  <p>and</p>
  
  <pre><code>ssb.txt=  0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1
  </code></pre>
  
  <p>Unfortunately the result I am getting in c++ is bit different from Matlab</p>
  
  <p>result from Matlab:</p>
  
  <pre><code>nf = 12
  </code></pre>
  
  <p>result in c++:</p>
  
  <pre><code>nf(4.88621,0)
  </code></pre>
  
  <p>My c++ code :</p>
  
  <pre><code>//iterDelayEst.cpp
  #include &lt;complex&gt;
  #include "binFreq.cpp"
  #include &lt;valarray&gt;
  
  typedef std::complex&lt;double&gt; Complex;
  typedef std::valarray&lt;Complex&gt; CArray;
  
  double iterDelayEst(int n, CArray&amp; x, CArray&amp; y)
  {
  /**********************************************constants************************************************
  *******************************************************************************************************/
      //exit if uncertainty below threshold
      fft(x);
      fft(y);
      auto fd2Tau = y;
      //frequency domain representation of signals
      std::vector&lt;double&gt; tau;
  
      auto f = binFreq(n);
  
      std::vector&lt;double&gt; e;
      Complex nf3(0.0,0.0);
  
      int j;
      for ( j = 0 ; j &lt; n ; j++ )
      {
          auto op = [](CArray::value_type v) {
              return std::conj(v);
          };
          auto nf1 = ((x * x.apply(op)) * (y * y.apply(op)));
          nf3 += nf1[j];
      }
      auto nf2 =std::sqrt(nf3);
      auto nf =nf2/(double)n;
      cout &lt;&lt; "nf" &lt;&lt; nf &lt;&lt;endl;
  }
  </code></pre>
  
  <p>my main.cpp:</p>
  
  <pre><code>#include &lt;complex&gt;
  #include &lt;iostream&gt;
  #include &lt;valarray&gt;
  #include &lt;malloc.h&gt;
  #include &lt;string&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fstream&gt;
  #include &lt;cstdio&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cmath&gt;
  #include &lt;iomanip&gt;
  #include &lt;cmath&gt;
  #include &lt;vector&gt;
  #include "fft.cpp"
  #include "cs_delay.cpp"
  #include "iterDelayEst.cpp"
  using namespace std;
  char filename[]  = "myfile.txt";
  char filename2[] = "myfile2.txt";
  
  typedef std::complex&lt;double&gt; Complex;
  typedef std::valarray &lt;Complex&gt; CArray;
  /***********************************************************************************************
  *                                 function declarations
  ************************** ******************************************************** ***********/
  void fft(CArray&amp; x);
  void ifft(CArray&amp; x);
  std::vector&lt;double&gt; binFreq(int n);
  void cs_delay(CArray&amp; x, int rate_hz, int delay_s, int n);
  double iterDelayEst(int n, CArray&amp; x, CArray&amp; x2);
  
  int main()
  {
      int dTest_samples;
      int cTest;
      int n=299;
      int i;
      int j;
      double x [n];
      double y [n];
      int rate_hz=1;
      int delay_s=30;
  
      /*****************************getting x*******************************/
  
      string line;
      double Result;
      ifstream myfile (filename);
      if (myfile.is_open())
      {
          for ( i = 0 ; (i &lt; n) &amp;&amp; (myfile &gt;&gt; x[i]) ; ++i)
              cout &lt;&lt; line &lt;&lt; '\n';
          stringstream convert(line);
  
          if ( !(convert &gt;&gt; Result) )
              Result = 0;
          x[i]=Result;
      }
      else cout &lt;&lt; "Unable to open file";
      /*****************************getting y******************************/
      string line2;
      double Result2;
      ifstream myfile2 (filename2);
      if (myfile2.is_open())
      {
          for ( i = 0 ; (i &lt; n) &amp;&amp; (myfile2 &gt;&gt; y[i]) ; ++i)
              cout &lt;&lt; line2 &lt;&lt; '\n';
          stringstream convert(line2);
  
          if ( !(convert &gt;&gt; Result2) )
              Result2 = 0;
          y[i]=Result2;
      }
      else cout &lt;&lt; "Unable to open file2";
      /***********************************************************************/
      /*********************for x******************/
      Complex test[n];
  
      for ( i = 0 ; i &lt; n ; ++i )
          test[i] = x[i];
  
      CArray data(test,n);
      /*********************for y******************/
      Complex test2[n];
  
      for ( j = 0 ; j &lt; n ; ++j )
          test2[j] = y[j];
  
      CArray data2(test2,n);
  
      // forward fft
      fft(data);
  
      std::cout &lt;&lt; "fft" &lt;&lt; std::endl;
      for (int i = 0; i &lt;n; ++i)
      {
          cout &lt;&lt; data[i] &lt;&lt; endl;
      }
  
      // inverse fft
      ifft(data);
  
      std::cout &lt;&lt; std::endl &lt;&lt; "ifft" &lt;&lt; std::endl;
      for (int i = 0; i &lt;n; ++i)
      {
          std::cout &lt;&lt; data[i] &lt;&lt; std::endl;
      }
  
      cs_delay(data, 1, 6, n);
      for (int i = 0; i &lt;n; ++i)
      {
          std::cout &lt;&lt; data[i] &lt;&lt; std::endl;
      }
  
      iterDelayEst(n, data, data2 );
  
      return 0;
  }
  </code></pre>
  
  <p>the fft function:</p>
  
  <pre><code>//fft.cpp
  using namespace std;
  const double PI = 3.141592653589793238460;
  
  /************************************************************************************************
  *                      Cooley–Tukey FFT (in-place, divide-and-conquer)                          *
  *              Higher memory requirements and redundancy although more intuitive                *
  ************************************************************************************************/
  
  typedef std::complex&lt;double&gt; Complex;
  typedef std::valarray&lt;Complex&gt; CArray;
  // Cooley–Tukey FFT (in-place, divide-and-conquer)
  // Higher memory requirements and redundancy although more intuitive
  void fft(CArray&amp; x)
  {
      const size_t N = x.size();
      if (N &lt;= 1) return;
  
      // divide
      CArray even = x[std::slice(0, N/2, 2)];
      CArray  odd = x[std::slice(1, N/2, 2)];
  
      // conquer
      fft(even);
      fft(odd);
  
      // combine
      for (size_t k = 0; k &lt; N/2; ++k)
      {
          Complex t = std::polar(1.0, -2 * PI * k / N) * odd[k];
          x[k    ] = even[k] + t;
          x[k+N/2] = even[k] - t;
      }
  }
  </code></pre>
  
  <p>result of my fft in matlab:</p>
  
  <pre><code>&gt;&gt; fft([0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0]')
  
  9.0000 + 0.0000i
    -3.3099 - 1.8957i
     0.0000 - 5.0273i
    -3.7722 + 5.1864i
     1.0000 + 0.0000i
     2.4654 + 1.0512i
    -0.0000 - 1.4966i
    -0.9547 - 0.4595i
     1.0000 + 0.0000i
    -1.3771 + 0.0371i
     0.0000 - 0.6682i
    -0.4381 + 1.8523i
     1.0000 + 0.0000i
     0.5733 - 0.8409i
     0.0000 - 0.1989i
    -1.1867 - 0.2275i
     1.0000 + 0.0000i
    -1.1867 + 0.2275i
     0.0000 + 0.1989i
     0.5733 + 0.8409i
     1.0000 + 0.0000i
    -0.4381 - 1.8523i
     0.0000 + 0.6682i
    -1.3771 - 0.0371i
     1.0000 + 0.0000i
    -0.9547 + 0.4595i
    -0.0000 + 1.4966i
     2.4654 - 1.0512i
     1.0000 + 0.0000i
    -3.7722 - 5.1864i
     0.0000 + 5.0273i
    -3.3099 + 1.8957i
  </code></pre>
  
  <p>result of my fft in c++:</p>
  
  <pre><code>(9,0)
  (-3.3099,-1.89568)
  (1.72253e-16,-5.02734)
  (-3.77219,5.1864)
  (1,0)
  (2.46544,1.05123)
  (2.94822e-16,-1.49661)
  (-0.954746,-0.459467)
  (1,0)
  (-1.37707,0.0371386)
  (-6.28295e-17,-0.668179)
  (-0.438105,1.85232)
  (1,0)
  (0.573279,-0.840935)
  (2.78992e-16,-0.198912)
  (-1.18671,-0.227505)
  (1,0)
  (-1.18671,0.227505)
  (1.72253e-16,0.198912)
  (0.573279,0.840935)
  (1,0)
  (-0.438105,-1.85232)
  (-3.82452e-17,0.668179)
  (-1.37707,-0.0371386)
  (1,0)
  (-0.954746,0.459467)
  (-3.67545e-17,1.49661)
  (2.46544,-1.05123)
  (1,0)
  (-3.77219,-5.1864)
  (-7.8049e-16,5.02734)
  (-3.3099,1.89568)
  </code></pre>
  
  <p>Let assume the result of nf is correct, I added new lines 
  new added lines :</p>
  
  <pre><code>for ( j = 0 ; j &lt; n ; j++ ){
      auto xcorr2=(fd2Tau * x.apply(std::conj));
      auto xcorr3=(std::abs(xcorr2[i]))/nf;
      cout &lt;&lt; "xcorr3" &lt;&lt; xcorr3[i] &lt;&lt; endl;
  }
  </code></pre>
  
  <p>unfortunately now I am receiving following errors messages:</p>
  
  <pre><code>In file included from myfft.cpp:16:0:
  iterDelayEst.cpp: In function ‘double iterDelayEst(int, CArray&amp;, CArray&amp;)’:
  iterDelayEst.cpp:61:30: error: no match for ‘operator[]’ (operand types are ‘std::complex&lt;double&gt;’ and ‘int’)
      cout &lt;&lt; "xcorr3" &lt;&lt; xcorr3[i] &lt;&lt; endl;
  
  
                                ^
  </code></pre>
  
  <p>Can someone help me to sort out what I am doing wrong?</p>
  
  <p>I correct the code and I am not getting the error any more. However, I added new lines, I would like to transform this Matlab code  ( ix = find(xcorr == max(xcorr)); ) to c++ 
  The code find  indices of values corresponding to the maximum of xcorr elements. I have written following code for the purpose :</p>
  
  <pre><code>find.cpp:
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;cstdio&gt;
  #include &lt;algorithm&gt;
  #include &lt;iterator&gt;
  #include &lt;cstdlib&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  int main() {
      std::vector&lt;double&gt; v;
      v.push_back(1);
      v.push_back(3);
      v.push_back(5);
      v.push_back(0);
      v.push_back(7);
      v.push_back(1);
      v.push_back(7);
  
      std::vector&lt;double&gt;::iterator result ;
      const double maxim = *max_element(v.begin(), v.end());
      for(result = std::find(v.begin(), v.end(), maxim); result != v.end(); result = std::find(result + 1, v.end(), maxim))
      {
             cout &lt;&lt; "maximum found at index " &lt;&lt; result -v.begin() +1 &lt;&lt; endl;
      }
      return 0;
  }
  </code></pre>
  
  <p>I need to incorporate this code into my iterDelayEst.cpp function, I don't see how I can send the result (array) from xcorr as argument to find function so that it use as vector </p>