Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
kagalenko-m-b authored and kagalenko-m-b committed Dec 5, 2021
1 parent 2efdf68 commit af9e1dc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/signalcorr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ function crosscov!(r::AbstractMatrix, x::AbstractVector, y::AbstractMatrix, lags
return r
end

function crosscov!(r::AbstractArray{U,3} where U, x::AbstractMatrix, y::AbstractMatrix, lags::AbstractVector{<:Integer}; demean::Bool=true)
function crosscov!(r::AbstractArray{<:Any}, x::AbstractMatrix, y::AbstractMatrix, lags::AbstractVector{<:Integer}; demean::Bool=true)
lx = size(x, 1)
nx = size(x, 2)
ny = size(y, 2)
Expand Down Expand Up @@ -458,7 +458,7 @@ function crosscor!(r::AbstractMatrix, x::AbstractVector, y::AbstractMatrix, lags
return r
end

function crosscor!(r::AbstractArray{U,3} where U, x::AbstractMatrix, y::AbstractMatrix, lags::AbstractVector{<:Integer}; demean::Bool=true)
function crosscor!(r::AbstractArray{<:Any}, x::AbstractMatrix, y::AbstractMatrix, lags::AbstractVector{<:Integer}; demean::Bool=true)
lx = size(x, 1)
nx = size(x, 2)
ny = size(y, 2)
Expand Down
19 changes: 9 additions & 10 deletions src/toeplitzsolvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
Solve Yule-Walker equations using Durbin algorithm.
Solution of NxN system is obtained iteratively, by solving 1x1, 2x2, ...
Solution of N×N system is obtained iteratively, by solving 1×1, 2×2, ...
in succession. For use in computing partial autocorrelation matrix,
return the last elements of the successive solutions in vector p[].
return the last elements of the successive solutions in vector `p`.
The section 4.7 of Golub,VanLoan "Matrix Computations," 4th ed.,
discusses the algorithm in detail.
Reference: Golub, G. H., and C. F. Van Loan. "Matrix computations 4th edition the johns hopkins university press." Baltimore, MD (2013), section 4.7
# Arguments
- `r::AbstractVector`: first column of a herimitian positive definite
- `r::AbstractVector`: first column of a Herimitian positive definite
Toeplitz matrix, excluding the diagonal element (equal to one).
- `y::AbstractVector`: work vector for solution, should have the same length
as r[]
as `r`
- `p::AbstractVector`: last elements of the successive solutions, should
have the same length as r[]
have the same length as `r`
# Returns
- `y::AbstractVector`: same as the second argument
- `y::AbstractVector`: return the solution vector (same as the second argument)
"""
function durbin!(r::AbstractVector{T}, y::AbstractVector{T}, p::AbstractVector{T}) where T
function durbin!(r::AbstractVector{T}, y::AbstractVector{T}, p::AbstractVector{T}) where {T}
n = length(r)
n <= length(p) || n <= length(y) || throw(DimensionMismatch("Auxiliary vectors cannot be shorter than data vector"))
y[1] = -r[1]
Expand All @@ -48,7 +47,7 @@ function durbin!(r::AbstractVector{T}, y::AbstractVector{T}, p::AbstractVector{T
end
return y
end
durbin(r::AbstractVector{T}) where T = durbin!(r, zeros(T, length(r)), zeros(T, length(r)))
durbin(r::AbstractVector{T}) where {T} = durbin!(r, zeros(T, length(r)), zeros(T, length(r)))

function levinson!(r::AbstractVector{T}, b::AbstractVector{T}, x::AbstractVector{T}) where T<:BlasReal
n = length(b)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tests = ["ambiguous",
"hist",
"rankcorr",
"signalcorr",
"signalcorr_real",
"misc",
"pairwise",
"reliability",
Expand Down
7 changes: 5 additions & 2 deletions test/signalcorr_complex.jl → test/signalcorr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ cmplx_x2 = convert(AbstractVector{Complex}, x2)

@test autocov([1:5;]) [2.0, 0.8, -0.2, -0.8, -0.8]
@test autocor([1, 2, 3, 4, 5]) [1.0, 0.4, -0.1, -0.4, -0.4]
@test_throws MethodError autocov([1 missing 2 3 4 5])
@test_throws MethodError autocor([1 missing 2 3 4 5])

acovx1 = [0.755284179631112 + 0.0im,
-0.005333112170365584 - 0.18633291805458002im,
Expand Down Expand Up @@ -90,6 +92,7 @@ c22 = crosscov(x2, x2)
@test crosscov(cmplx_x1, cmplx_x) [c11 c12]
@test crosscov(x, x) cat([c11 c21], [c12 c22], dims=3)
@test crosscov(cmplx_x, cmplx_x) cat([c11 c21], [c12 c22], dims=3)
@test_throws MethodError crosscov([1 6; 2 7; missing 8; 3 9; 4 10; 5 11])

rcor0 = [0.230940107675850,
-0.230940107675850,
Expand All @@ -116,7 +119,7 @@ c22 = crosscor(x2, x2)
@test crosscor(cmplx_x1, cmplx_x) [c11 c12]
@test crosscor(x, x) cat([c11 c21], [c12 c22], dims=3)
@test crosscor(cmplx_x, cmplx_x) cat([c11 c21], [c12 c22], dims=3)

@test_throws MethodError crosscor([1 6; 2 7; missing 8; 3 9; 4 10; 5 11])

## pacf least squares
pacf_ls = [-1.598495044296996e-03 - 2.915104118351207e-01im
Expand All @@ -134,7 +137,7 @@ function yulewalker_qr(v::AbstractVector)
x = -A\b
end
function toeplitz(v::AbstractVector{T}) where T
N=length(v)
N = length(v)
A = zeros(T, N - 1, N - 1)
for n in 1:N-1
A[n, n+1:end] = conj(v[2:N-n])
Expand Down

0 comments on commit af9e1dc

Please sign in to comment.