|
| 1 | +export ccorr |
| 2 | + |
| 3 | + |
| 4 | +""" |
| 5 | + ccorr(u, v[, dims]; centered=false) |
| 6 | +
|
| 7 | +Calculates the cross-correlation between `u` and `v` along `dims`. |
| 8 | +`centered=true` moves the output of the cross-correlation to the Fourier center. |
| 9 | +
|
| 10 | +If `u` and `v` are both a real valued array we use `rfft` and hence |
| 11 | +the output is real as well. |
| 12 | +If either `u` or `v` is complex we use `fft` and output is hence complex. |
| 13 | +
|
| 14 | +Per default the correlation is performed along `min(ndims(u), ndims(v))`. |
| 15 | +
|
| 16 | +
|
| 17 | +```jldoctest |
| 18 | +julia> ccorr([1,1,0,0], [1,1,0,0], centered=true) |
| 19 | +4-element Vector{Float64}: |
| 20 | + 0.0 |
| 21 | + 1.0 |
| 22 | + 2.0 |
| 23 | + 1.0 |
| 24 | +
|
| 25 | +julia> ccorr([1,1,0,0], [1,1,0,0]) |
| 26 | +4-element Vector{Float64}: |
| 27 | + 2.0 |
| 28 | + 1.0 |
| 29 | + 0.0 |
| 30 | + 1.0 |
| 31 | +
|
| 32 | +julia> ccorr([1im,0,0,0], [0,1im,0,0]) |
| 33 | +4-element Vector{ComplexF64}: |
| 34 | + 0.0 + 0.0im |
| 35 | + 0.0 + 0.0im |
| 36 | + 0.0 + 0.0im |
| 37 | + 1.0 + 0.0im |
| 38 | +
|
| 39 | +julia> ccorr([1im,0,0,0], [0,1im,0,0], centered=true) |
| 40 | +4-element Vector{ComplexF64}: |
| 41 | + 0.0 + 0.0im |
| 42 | + 1.0 + 0.0im |
| 43 | + 0.0 + 0.0im |
| 44 | + 0.0 + 0.0im |
| 45 | +``` |
| 46 | +""" |
| 47 | +function ccorr(u::AbstractArray{T, N}, v::AbstractArray{D, M}, |
| 48 | + dims=ntuple(+, min(N, M)); |
| 49 | + centered=false) where {T, D, N, M} |
| 50 | + out = ifft(fft(u, dims) .* conj.(fft(v, dims)), dims) |
| 51 | + |
| 52 | + if centered |
| 53 | + return fftshift(out) |
| 54 | + else |
| 55 | + return out |
| 56 | + end |
| 57 | +end |
| 58 | + |
| 59 | +function ccorr(u::AbstractArray{<:Real, N}, v::AbstractArray{<:Real, M}, |
| 60 | + dims=ntuple(+, min(N, M)); |
| 61 | + centered=false) where {N, M} |
| 62 | + out = irfft(rfft(u, dims) .* conj.(rfft(v, dims)), size(u, dims[1]), dims) |
| 63 | + |
| 64 | + if centered |
| 65 | + return fftshift(out) |
| 66 | + else |
| 67 | + return out |
| 68 | + end |
| 69 | +end |
0 commit comments