Autocorrelation function with ranges

kerdemdemir via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 27 03:29:49 PDT 2015


Hi

My question is more about Maths than D lang,
I am hoping, maybe somebody worked with AutoCorrelation function 
before.


auto autoCorrelation(R)(R range)
         if (isRandomAccessRange!R)
{
     auto residual = residualPowerOf2(range.length); // Find how 
many zeros to add
     auto fftResult = range.chain(repeat(0, residual)).fft(); // 
Takes FFT

     //First zip each element of fft with conjagute of fft
     auto autoCorrResult = fftResult.zip(fftResult.map!(a => a * 
a.conj())).
				map!( a=> a[0] * a[1] ). // Than multiple them
				inverseFft(). // Than take inverse
				dropBack(residual);//Drop the additional zeros
		
     auto finalResult = autoCorrResult.take(1). // Take DC element
		        chain(autoCorrResult[$/2..$]).//Take last half to 
beginning
		        chain(autoCorrResult[1..$/2]). // First(negative lags) 
to end
			map!(a => a.re); // I just need real part
					
     return finalResult ;
}


My autocorrelation method return some crazy values(finalResult[0] 
= -101652). I am mostly suspicious about calculations to set 
"finalResult"  variable.

Also is there any performance issues? can I make this faster?




More information about the Digitalmars-d-learn mailing list