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

Support dims kwarg in rms #500

Merged
merged 1 commit into from
Feb 9, 2024
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
14 changes: 11 additions & 3 deletions src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Base: *
import LinearAlgebra.BLAS
using LinearAlgebra: mul!
using FFTW
using Statistics: mean

export hilbert,
fftintype,
Expand Down Expand Up @@ -162,11 +163,18 @@ Convert an amplitude ratio to dB (decibel), or ``20
amp2db(a::Real) = 20*log10(a)

"""
rms(s)
rms(s; dims)

Return the root mean square of signal `s`.
Return the root mean square (rms) of signal `s`. Optional keyword parameter
`dims` can be used to specify the dimensions along which to compue the rms.
"""
rms(s::AbstractArray{T}) where {T<:Number} = sqrt(sum(abs2, s)/length(s))
function rms(s::AbstractArray{T}; dims=:) where {T<:Number}
if dims === (:)
return sqrt(mean(abs2, s))
else
return sqrt.(mean(abs2, s; dims=dims))
martinholters marked this conversation as resolved.
Show resolved Hide resolved
end
end

"""
rmsfft(f)
Expand Down
3 changes: 3 additions & 0 deletions test/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ end
n = (5,6)
for x in ( randn(n), randn(n)+randn(n)im )
@test rms(x) ≈ sqrt(mean(abs.(x).^2))
@test rms(x; dims=1) ≈ sqrt.(mean(abs.(x).^2; dims=1))
@test rms(x; dims=2) ≈ sqrt.(mean(abs.(x).^2; dims=2))
@test rms(x; dims=1:2) ≈ sqrt.(mean(abs.(x).^2; dims=1:2))
@test rmsfft(fft(x)) ≈ rms(x)
end # for
end
Expand Down
Loading