public void calcPhaseDeviation() {
		// saves the result of the FFT in an object that contains the spectrum,
		// real and imaginary part.
		FFTComponents components = this.nextPhase();
		double[] phase = new double[components.real.length];
		double[] previousPhase = new double[components.real.length];
		double[] antePreviousPhase = new double[components.real.length];

		do {

			/*
			 * get the phase from the components object
			 */
			phase = calcPhaseFromComponents(components);

			double phaseDeviation = 0;

			/**
			 * used to normalize
			 */
			float totalSpectrum = 0;

			/**
			 * prepare the data for the next iteration
			 */

			/*
			 * iterate though the bins and sum the modulus of the phase
			 * deviation. deltaPhi = Phi(n)-2Phi(n-1)+Phi(n-2) If using
			 * weighting, each deltaphi has to be multiplied by the
			 * correspondent spectrum magnitude. Note: One uses the formulas (8)
			 * and (9) of Bello "A tutorial on onset detection in music signals"
			 * and, for the weighting, the formula 2.4 in Simon Dixon
			 * "Onset Detection Revisited"
			 */
			for (int i = 0; i < components.spectrum.length; i++) {
				if (!useWeighting) {
					phaseDeviation += Math.abs(phase[i] - 2 * previousPhase[i]
							+ antePreviousPhase[i]);
				} else {
					phaseDeviation += Math
							.abs(components.spectrum[i]
									* (phase[i] - 2 * previousPhase[i] + antePreviousPhase[i]));
				}
				if (useNormalization) {
					totalSpectrum += Math.abs(components.spectrum[i]);
				}
			}

			/*
			 * Adds the phase deviation to the list, dividing the result
			 * obtained with the number of bins or doing the normalization
			 */
			if (!useNormalization) {
				PD.add((float) phaseDeviation / components.spectrum.length);
			} else {
				//to avoid division by zero
				if(totalSpectrum == 0) {
					totalSpectrum = 1;
				}
				PD.add((float) phaseDeviation / totalSpectrum);
			}

			/**
			 * prepare the data for the next iteration
			 */

			// the antepreviousphase in the next iteration is the previous phase
			// in this iteration
			if (previousPhase != null) {
				System.arraycopy(previousPhase, 0, antePreviousPhase, 0,
						phase.length);
			}

			// the previous phase in the following iteration is the
			// current phase of this iteration
			System.arraycopy(phase, 0, previousPhase, 0, phase.length);

		} while ((components = this.nextPhase()) != null);
	}