Index: src/pitch/pitchyinfft.c
===================================================================
--- src/pitch/pitchyinfft.c	(revision ac20c856d41f89c5c9c4e2f197ff79d4fc4e0c2d)
+++ src/pitch/pitchyinfft.c	(revision b209eda52148caba027b22dea1630f019c6b143d)
@@ -1,4 +1,4 @@
 /*
-  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
+  Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
 
   This file is part of aubio.
@@ -31,8 +31,7 @@
   fvec_t *win;        /**< temporal weighting window */
   fvec_t *winput;     /**< windowed spectrum */
-  cvec_t *res;        /**< complex vector to compute square difference function */
   fvec_t *sqrmag;     /**< square difference function */
   fvec_t *weight;     /**< spectral weighting window (psychoacoustic model) */
-  cvec_t *fftout;     /**< Fourier transform output */
+  fvec_t *fftout;     /**< Fourier transform output */
   aubio_fft_t *fft;   /**< fft object to compute square difference function */
   fvec_t *yinfft;     /**< Yin function */
@@ -59,7 +58,6 @@
   p->winput = new_fvec (bufsize);
   p->fft = new_aubio_fft (bufsize);
-  p->fftout = new_cvec (bufsize);
+  p->fftout = new_fvec (bufsize);
   p->sqrmag = new_fvec (bufsize);
-  p->res = new_cvec (bufsize);
   p->yinfft = new_fvec (bufsize / 2 + 1);
   p->tol = 0.85;
@@ -99,41 +97,48 @@
 {
   uint_t tau, l;
+  uint_t length = p->fftout->length;
   uint_t halfperiod;
-  smpl_t tmp, sum;
-  cvec_t *res = (cvec_t *) p->res;
-  fvec_t *yin = (fvec_t *) p->yinfft;
-  l = 0;
-  tmp = 0.;
-  sum = 0.;
+  fvec_t *fftout = p->fftout;
+  fvec_t *yin = p->yinfft;
+  smpl_t tmp = 0., sum = 0.;
+  // window the input
   for (l = 0; l < input->length; l++) {
     p->winput->data[l] = p->win->data[l] * input->data[l];
   }
-  aubio_fft_do (p->fft, p->winput, p->fftout);
-  for (l = 0; l < p->fftout->length; l++) {
-    p->sqrmag->data[l] = SQR (p->fftout->norm[l]);
+  // get the real / imag parts of its fft
+  aubio_fft_do_complex (p->fft, p->winput, fftout);
+  // get the squared magnitude spectrum, applying some weight
+  p->sqrmag->data[0] = SQR(fftout->data[0]);
+  p->sqrmag->data[0] *= p->weight->data[0];
+  for (l = 1; l < length / 2; l++) {
+    p->sqrmag->data[l] = SQR(fftout->data[l]) + SQR(fftout->data[length - l]);
     p->sqrmag->data[l] *= p->weight->data[l];
+    p->sqrmag->data[length - l] = p->sqrmag->data[l];
   }
-  for (l = 1; l < p->fftout->length; l++) {
-    p->sqrmag->data[(p->fftout->length - 1) * 2 - l] =
-        SQR (p->fftout->norm[l]);
-    p->sqrmag->data[(p->fftout->length - 1) * 2 - l] *=
-        p->weight->data[l];
-  }
-  for (l = 0; l < p->sqrmag->length / 2 + 1; l++) {
+  p->sqrmag->data[length / 2] = SQR(fftout->data[length / 2]);
+  p->sqrmag->data[length / 2] *= p->weight->data[length / 2];
+  // get sum of weighted squared mags
+  for (l = 0; l < length / 2 + 1; l++) {
     sum += p->sqrmag->data[l];
   }
   sum *= 2.;
-  aubio_fft_do (p->fft, p->sqrmag, res);
+  // get the real / imag parts of the fft of the squared magnitude
+  aubio_fft_do_complex (p->fft, p->sqrmag, fftout);
   yin->data[0] = 1.;
   for (tau = 1; tau < yin->length; tau++) {
-    yin->data[tau] = sum - res->norm[tau] * COS (res->phas[tau]);
+    // compute the square differences
+    yin->data[tau] = sum - fftout->data[tau];
+    // and the cumulative mean normalized difference function
     tmp += yin->data[tau];
     yin->data[tau] *= tau / tmp;
   }
+  // find best candidates
   tau = fvec_min_elem (yin);
   if (yin->data[tau] < p->tol) {
-    /* no interpolation */
-    //return tau;
-    /* 3 point quadratic interpolation */
+    // no interpolation, directly return the period as an integer
+    //output->data[0] = tau;
+    //return;
+
+    // 3 point quadratic interpolation
     //return fvec_quadint_min(yin,tau,1);
     /* additional check for (unlikely) octave doubling in higher frequencies */
@@ -160,6 +165,5 @@
   del_fvec (p->yinfft);
   del_fvec (p->sqrmag);
-  del_cvec (p->res);
-  del_cvec (p->fftout);
+  del_fvec (p->fftout);
   del_fvec (p->winput);
   del_fvec (p->weight);
