Skip to content

Commit

Permalink
improve performance in freqresp for tf
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Feb 21, 2022
1 parent 2f57fc9 commit fcf6dd5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/freqresp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ function freqresp!(R::Array{T,3}, sys::LTISystem, w_vec::AbstractVector{<:Real})
PermutedDimsArray{T,3,(3,1,2),(2,3,1),Array{T,3}}(R)
end

function freqresp!(R::Array{T,3}, sys::TransferFunction, w_vec::AbstractVector{<:Real}) where T
te = sys.timeevol
ny,nu = noutputs(sys), ninputs(sys)
@boundscheck size(R) == (ny,nu,length(w_vec))
@inbounds for wi = eachindex(w_vec), ui = 1:nu, yi = 1:ny
R[yi,ui,wi] = evalfr(sys.matrix[yi,ui], _freq(w_vec[wi], te))
end
PermutedDimsArray{T,3,(3,1,2),(2,3,1),Array{T,3}}(R)
end

@autovec () function freqresp(G::AbstractMatrix, w_vec::AbstractVector{<:Real})
repeat(G, 1, 1, length(w_vec))
end
Expand All @@ -53,12 +63,6 @@ end
_freq(w, ::Continuous) = complex(0, w)
_freq(w, te::Discrete) = cis(w*te.Ts)

function freqresp(sys::AbstractStateSpace, w_vec::AbstractVector{W}) where W <: Real
ny, nu = size(sys)
T = promote_type(Complex{real(eltype(sys.A))}, Complex{W})
R = Array{T, 3}(undef, ny, nu, length(w_vec))
freqresp!(R, sys, w_vec)
end
@autovec () function freqresp!(R::Array{T,3}, sys::AbstractStateSpace, w_vec::AbstractVector{W}) where {T, W <: Real}
ny, nu = size(sys)
@boundscheck size(R) == (ny,nu,length(w_vec))
Expand Down
2 changes: 1 addition & 1 deletion test/test_freqresp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ resp2 = reshape((im*w .+ 2)./(im*w .+ 1), length(w), 1, 1)
@inferred freqresp(G2, w)
@inferred freqresp(H2, w)
@test (@allocated freqresp(sys2, w)) < 1.2*56672 # allow 20% increase due to compiler variations

@test (@allocated freqresp(G2, w)) < 1.2*976 # allow 20% increase due to compiler variations

## Complex-coefficient system
sys3 = ss(-1+im, 1, (1-im), (1-im))
Expand Down

0 comments on commit fcf6dd5

Please sign in to comment.