Autocorrelation function with ranges

Timon Gehr via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 27 05:17:30 PDT 2015


On 06/27/2015 12:29 PM, kerdemdemir wrote:
> 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.
> ...

One obvious problem is this:

fftResult.zip(fftResult.map!(a => a * a.conj())).map!(a=>a[0]*a[1]).

This computes a²·a̅ instead of a·a̅.

What is the source code for residualPowerOf2?

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

Probably you should use http://dlang.org/phobos/std_complex.html#.sqAbs 
instead. You then also don't need the final map to extract the real part.



More information about the Digitalmars-d-learn mailing list