Skip to content

Commit e73d83c

Browse files
committed
Limit changes to more general argument types
1 parent 6b9d2cc commit e73d83c

File tree

1 file changed

+56
-35
lines changed

1 file changed

+56
-35
lines changed

src/signalcorr.jl

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ end
4343

4444
default_autolags(lx::Int) = 0 : default_laglen(lx)
4545

46+
_autodot(x::AbstractVector{<:RealFP}, lx::Int, l::Int) = dot(x, 1:(lx-l), x, (1+l):lx)
4647
_autodot(x::AbstractVector, lx::Int, l::Int) = dot(view(x, 1:(lx-l)), view(x, (1+l):lx))
4748

49+
4850
## autocov
4951
"""
5052
autocov!(r, x, lags; demean=true)
@@ -59,31 +61,34 @@ where each column in the result will correspond to a column in `x`.
5961
6062
The output is not normalized. See [`autocor!`](@ref) for a method with normalization.
6163
"""
62-
function autocov!(
63-
r::AbstractVector,
64-
x::AbstractVector,
65-
lags::AbstractVector{<:Integer},
66-
z::Vector=Vector{typeof(zero(eltype(x)) / 1)}(undef, length(x));
67-
demean::Bool=true
68-
)
64+
function autocov!(r::AbstractVector, x::AbstractVector, lags::AbstractVector{<:Integer} demean::Bool=true)
6965
lx = length(x)
7066
m = length(lags)
71-
length(r) == length(z) == m || throw(DimensionMismatch())
67+
length(r) == m || throw(DimensionMismatch())
7268
check_lags(lx, lags)
73-
demean ? z .= x .- mean(x) : copyto!(z, x)
74-
@inbounds for (k, lags_k) in zip(eachindex(r), lags) # foreach lag value
75-
r[k] = _autodot(z, lx, lags_k) / lx
69+
70+
T = typeof(zero(eltype(x)) / 1)
71+
z::Vector{T} = demean ? x .- mean(x) : x
72+
for k = 1 : m # foreach lag value
73+
r[k] = _autodot(z, lx, lags[k]) / lx
7674
end
7775
return r
7876
end
7977

80-
function autocov!(
81-
r::AbstractMatrix, x::AbstractMatrix, lags::AbstractVector{<:Integer}; demean::Bool=true
82-
)
78+
function autocov!(r::AbstractMatrix, x::RealMatrix, AbstractVector{<:Integer}; demean::Bool=true)
79+
lx = size(x, 1)
80+
ns = size(x, 2)
81+
m = length(lags)
82+
size(r) == (m, ns) || throw(DimensionMismatch())
83+
check_lags(lx, lags)
84+
8385
T = typeof(zero(eltype(x)) / 1)
84-
z = Vector{T}(undef, size(x, 1))
85-
for n in 1:size(x, 2)
86-
autocov!(view(r, :, n), view(x, :, n), lags, z; demean)
86+
z = Vector{T}(undef, lx)
87+
for j = 1 : ns
88+
demean_col!(z, x, j, demean)
89+
for k = 1 : m
90+
r[k,j] = _autodot(z, lx, lags[k]) / lx
91+
end
8792
end
8893
return r
8994
end
@@ -134,27 +139,36 @@ where each column in the result will correspond to a column in `x`.
134139
The output is normalized by the variance of `x`, i.e. so that the lag 0
135140
autocorrelation is 1. See [`autocov!`](@ref) for the unnormalized form.
136141
"""
137-
function autocor!(
138-
r::AbstractVector,
139-
x::AbstractVector,
140-
lags::AbstractVector{<:Integer},
141-
z=zeros(typeof(zero(eltype(x)) / 1), length(x));
142-
demean::Bool=true
143-
)
144-
autocov!(view(r, 1:1), x, 0:0, z; demean)
145-
zz = r[1]
146-
autocov!(r, x, lags, z; demean)
147-
ldiv!(zz, r)
142+
function autocor!(r::AbstractVector, x::AbstractVector, lags::AbstractVector{<:Integer}; demean::Bool=true)
143+
lx = length(x)
144+
m = length(lags)
145+
length(r) == m || throw(DimensionMismatch())
146+
check_lags(lx, lags)
147+
148+
T = typeof(zero(eltype(x)) / 1)
149+
z::Vector{T} = demean ? x .- mean(x) : x
150+
zz = dot(z, z)
151+
for k = 1 : m # foreach lag value
152+
r[k] = _autodot(z, lx, lags[k]) / zz
153+
end
148154
return r
149155
end
150156

151-
function autocor!(
152-
r::AbstractMatrix, x::AbstractMatrix, lags::AbstractVector{<:Integer}; demean::Bool=true
153-
)
154-
T = typeof(zero(eltype(x))/1)
155-
z = Vector{T}(undef, size(x, 1))
156-
for n in 1:size(x, 2)
157-
autocor!(view(r, :, n), view(x, :, n), lags, z; demean=demean)
157+
function autocor!(r::AbstractMatrix, x::AbstractMatrix, lags::AbstractVector{<:Integer}; demean::Bool=true)
158+
lx = size(x, 1)
159+
ns = size(x, 2)
160+
m = length(lags)
161+
size(r) == (m, ns) || throw(DimensionMismatch())
162+
check_lags(lx, lags)
163+
164+
T = typeof(zero(eltype(x)) / 1)
165+
z = Vector{T}(undef, lx)
166+
for j = 1 : ns
167+
demean_col!(z, x, j, demean)
168+
zz = dot(z, z)
169+
for k = 1 : m
170+
r[k,j] = _autodot(z, lx, lags[k]) / zz
171+
end
158172
end
159173
return r
160174
end
@@ -199,6 +213,13 @@ autocor(x::AbstractVecOrMat; demean::Bool=true) =
199213

200214
default_crosslags(lx::Int) = (l=default_laglen(lx); -l:l)
201215

216+
function _crossdot(x::AbstractVector{T}, y::AbstractVector{T}, lx::Int, l::Int) where {T<:RealFP}
217+
if l >= 0
218+
dot(x, 1:(lx-l), y, (1+l):lx)
219+
else
220+
dot(x, (1-l):lx, y, 1:(lx+l))
221+
end
222+
end
202223
function _crossdot(x::AbstractVector, y::AbstractVector, lx::Int, l::Int)
203224
if l >= 0
204225
dot(view(x, 1:(lx-l)), view(y, (1+l):lx))

0 commit comments

Comments
 (0)