Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize performance of sigma #840

Merged
merged 1 commit into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
optimize performance of sigma
  • Loading branch information
baggepinnen committed May 17, 2023
commit 0bae63a1f164f2f62feba128207d340728384ead
12 changes: 10 additions & 2 deletions lib/ControlSystemsBase/src/freqresp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,18 @@ end
Compute the singular values `sv` of the frequency response of system `sys` at
frequencies `w`. See also [`sigmaplot`](@ref)

`sv` has size `(max(ny, nu), length(w))`"""
`sv` has size `(min(ny, nu), length(w))`"""
@autovec (1) function sigma(sys::LTISystem, w::AbstractVector)
resp = freqresp(sys, w)
sv = dropdims(mapslices(svdvals, resp, dims=(1,2)),dims=2)
ny, nu = size(sys)
if ny == 1 || nu == 1 # Shortcut available
sv = Matrix{real(eltype(resp))}(undef, 1, length(w))
for i = eachindex(w)
@views sv[1, i] = norm(resp[:,:,i])
end
else
sv = dropdims(mapslices(svdvals, resp, dims=(1,2)),dims=2)::Matrix{real(eltype(resp))}
end
return sv, w
end
@autovec (1) sigma(sys::LTISystem) = sigma(sys, _default_freq_vector(sys, Val{:sigma}()))
Expand Down
18 changes: 18 additions & 0 deletions lib/ControlSystemsBase/test/test_freqresp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ end
@test sigma(sys, ws)[1] ≈ sigs


## Test sigma when either ny = 1 or nu = 1
sys = ssrand(1,2,1)
resp = freqresp(sys, ws)
sigs = Array{Float64}(undef, 1, 50)
for i in eachindex(ws)
sigs[:, i] = svdvals(resp[:,:,i])
end
@test sigma(sys, ws)[1] ≈ sigs

sys = ssrand(2,1,1)
resp = freqresp(sys, ws)
sigs = Array{Float64}(undef, 1, 50)
for i in eachindex(ws)
sigs[:, i] = svdvals(resp[:,:,i])
end
@test sigma(sys, ws)[1] ≈ sigs


# test unwrap
P = tf(1, [1, 1, 0]) * tf(1, [1, 1])
w = exp10.(LinRange(-2, 4, 200))
Expand Down
Loading