Skip to content

Commit

Permalink
Update show methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Bazdresch committed May 11, 2023
1 parent 065b66e commit dcc24cc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 25 deletions.
19 changes: 12 additions & 7 deletions src/SinusoidalRegressions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ include("liang.jl")
include("plotrecipes.jl")

"""
sinfit(problem::Problem, algorithm::Algorithm)
sinfit(problem::SRProblem, algorithm::SRAlgorithm)
Calculate a sinosoidal regression on `problem` using `algorithm`.
Expand All @@ -53,15 +53,20 @@ Example
```
julia> using SinusoidalRegressions
julia> t = range(0, 1, length = 100) # time instants
julia> t = collect(range(0, 1, length = 100)) # time instants
julia> s = sin.(2*pi*15*t .+ pi/4) .+ 0.1*randn(100) # noisy samples
julia> p = Sin3Problem(t, s, 15) # define regression problem
julia> sinfit(p, IEEE1057()) # calculate fit with IEEE 1057
Sinusoidal parameters SinModel{Float64}:
Frequency (Hz) : 15.0
DC : -0.01067218324878172
Sine amplitude (Q) : 0.7299806464221965
Cosine amplitude (I): 0.6822068658523716
3-Parameter Sinusoidal Problem Sin3Problem:
X : Vector{Float64} with 100 elements
Y : Vector{Float64} with 100 elements
Frequency (Hz) : 15
Parameter estimates:
DC : missing
Sine amplitude (Q) : missing
Cosine amplitude (I) : missing
Lower bounds : missing
Upper bounds : missing
```
See the documentation for more details.
Expand Down
90 changes: 72 additions & 18 deletions src/typedefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ end

Sin3Problem(X, Y, f ; kwargs...) = Sin3Problem(; X, Y, f, kwargs...)

function Base.show(io::IO, problem::Sin3Problem)
println(io, "3-Parameter Sinusoidal Problem Sin3Problem:")
println(io, " X : $(typeof(problem.X)) with $(length(problem.X)) elements")
println(io, " Y : $(typeof(problem.Y)) with $(length(problem.Y)) elements")
println(io, " Frequency (Hz) : $(problem.f)")
println(io, "Parameter estimates:")
println(io, " DC : $(problem.DC)")
println(io, " Sine amplitude (Q) : $(problem.Q)")
println(io, " Cosine amplitude (I) : $(problem.I)")
println(io, " Lower bounds : $(problem.lb)")
println(io, " Upper bounds : $(problem.ub)")
end

"""
Sin4Problem(X, Y ; [f, DC, Q, I, lb, ub]) <: SRProblem
Expand Down Expand Up @@ -134,6 +147,19 @@ end

Sin4Problem(X, Y ; kwargs...) = Sin4Problem(; X, Y, kwargs...)

function Base.show(io::IO, problem::Sin4Problem)
println(io, "4-Parameter Sinusoidal Problem Sin4Problem:")
println(io, " X : $(typeof(problem.X)) with $(length(problem.X)) elements")
println(io, " Y : $(typeof(problem.Y)) with $(length(problem.Y)) elements")
println(io, "Parameter estimates:")
println(io, " Frequency (Hz) : $(problem.f)")
println(io, " DC : $(problem.DC)")
println(io, " Sine amplitude (Q) : $(problem.Q)")
println(io, " Cosine amplitude (I) : $(problem.I)")
println(io, " Lower bounds : $(problem.lb)")
println(io, " Upper bounds : $(problem.ub)")
end

"""
MixedLinSin4Problem(X, Y, f ; [DC, Q, I, m, lb, ub]) <: SRProblem
Expand Down Expand Up @@ -163,6 +189,20 @@ end

MixedLinSin4Problem(X, Y, f ; kwargs...) = MixedLinSin4Problem(; X, Y, f, kwargs...)

function Base.show(io::IO, problem::MixedLinSin4Problem)
println(io, "4-Parameter Mixed Linear-Sinusoidal Problem MixedLinSin4Problem:")
println(io, " X : $(typeof(problem.X)) with $(length(problem.X)) elements")
println(io, " Y : $(typeof(problem.Y)) with $(length(problem.Y)) elements")
println(io, " Frequency (Hz) : $(problem.f)")
println(io, "Parameter estimates:")
println(io, " DC : $(problem.DC)")
println(io, " Sine amplitude (Q) : $(problem.Q)")
println(io, " Cosine amplitude (I) : $(problem.I)")
println(io, " Linear term (m) : $(problem.m)")
println(io, " Lower bounds : $(problem.lb)")
println(io, " Upper bounds : $(problem.ub)")
end

"""
MixedLinSin5Problem(X, Y ; [f, DC, Q, I, m, lb, ub]) <: SRProblem
Expand Down Expand Up @@ -191,6 +231,20 @@ end

MixedLinSin5Problem(X, Y ; kwargs...) = MixedLinSin5Problem(; X, Y, kwargs...)

function Base.show(io::IO, problem::MixedLinSin5Problem)
println(io, "5-Parameter Mixed Linear-Sinusoidal Problem MixedLinSin5Problem:")
println(io, " X : $(typeof(problem.X)) with $(length(problem.X)) elements")
println(io, " Y : $(typeof(problem.Y)) with $(length(problem.Y)) elements")
println(io, "Parameter estimates:")
println(io, " Frequency (Hz) : $(problem.f)")
println(io, " DC : $(problem.DC)")
println(io, " Sine amplitude (Q) : $(problem.Q)")
println(io, " Cosine amplitude (I) : $(problem.I)")
println(io, " Linear term (m) : $(problem.m)")
println(io, " Lower bounds : $(problem.lb)")
println(io, " Upper bounds : $(problem.ub)")
end

"""
SRModel
Expand Down Expand Up @@ -289,12 +343,12 @@ julia> P(t)
(params::SinModel)(t) = @. params.Q*sin(2π*params.f*t) + params.I*cos(2π*params.f*t) + params.DC
#TODO: Remove implicit broadcasting

function Base.show(io::IO, params::SinModel{T}) where {T}
println(io, "Sinusoidal parameters SinModel{$T}:")
println(io, " Frequency (Hz) : $(params.f)")
println(io, " DC : $(params.DC)")
println(io, " Sine amplitude (Q) : $(params.Q)")
println(io, " Cosine amplitude (I): $(params.I)")
function Base.show(io::IO, model::SinModel{T}) where {T}
println(io, "Sinusoidal model SinModel{$T}:")
println(io, " Frequency (Hz) : $(model.f)")
println(io, " DC : $(model.DC)")
println(io, " Sine amplitude (Q) : $(model.Q)")
println(io, " Cosine amplitude (I) : $(model.I)")
end

"""
Expand Down Expand Up @@ -378,34 +432,34 @@ julia> P(t)
(params::MixedLinSinModel)(t) = @. params.Q*sin(2π*params.f*t) + params.I*cos(2π*params.f*t) +
params.m*t + params.DC

function Base.show(io::IO, params::MixedLinSinModel{T}) where {T}
println(io, "Mixed Linear-Sinusoidal parameters MixedLinSinModel{$T}:")
println(io, " Frequency (Hz) : $(params.f)")
println(io, " DC : $(params.DC)")
println(io, " Sine amplitude (Q) : $(params.Q)")
println(io, " Cosine amplitude (I) : $(params.I)")
println(io, " Linear term (m) : $(params.m)")
function Base.show(io::IO, model::MixedLinSinModel{T}) where {T}
println(io, "Mixed Linear-Sinusoidal model MixedLinSinModel{$T}:")
println(io, " Frequency (Hz) : $(model.f)")
println(io, " DC : $(model.DC)")
println(io, " Sine amplitude (Q) : $(model.Q)")
println(io, " Cosine amplitude (I) : $(model.I)")
println(io, " Linear term (m) : $(model.m)")
end

### not yet implemented

"""
GenSinProblem <: SinusoidalFunctionParameters
GenSinModel <: SinModel
Not yet implemented.
See also: [`SinProblem`](@ref), [`MixedLinSinModel`](@ref), [`DampedSinProblem`](@ref)
See also: [`SinModel`](@ref), [`MixedLinSinModel`](@ref), [`DampedSinModel`](@ref)
(not yet implemented).
"""
struct GenSinProblem
struct GenSinModel
end

"""
DampedSinProblem <: SinusoidalFunctionParameters
DampedSinModel <: SinModel
Not yet implemented.
See also: [`SinProblem`](@ref), [`MixedLinSinModel`](@ref), [`GenSinProblem`](@ref)
See also: [`SinModel`](@ref), [`MixedLinSinModel`](@ref), [`GenSinModel`](@ref)
(not yet implemented).
"""
struct DampedSinProblem
Expand Down

0 comments on commit dcc24cc

Please sign in to comment.