Skip to content

Commit

Permalink
Add polar notation for SinusoidP
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Bazdresch committed Apr 5, 2023
1 parent a6507ec commit 13bcd78
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/typedefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ struct SinusoidP{T <: Real} <: SinusoidalFunctionParameters
end

"""
SinusoidP{T}(f, DC, Q, I)
SinusoidP{T}(f, DC, Q, I ; polar = false)
Construct a `SinusoidP{T}` with the given parameters, promoting to a common type `T` if
necessary.
Example
=======
If the keyword argument `polar` is `true`, then the arguments are interpreted as frequency,
DC, amplitude `A` and phase `ϕ` in the model ``s(x) = DC + A\\cos(2πfx + ϕ)``.
Examples
========
```
julia> SinusoidP(10, 1, -0.5, 1.2)
Expand All @@ -40,9 +43,24 @@ Sinusoidal parameters SinusoidP{Float64}:
DC : 1.0
Sine amplitude (Q) : -0.5
Cosine amplitude (I): 1.2
julia> SinusoidP(1, 0, 1, 0, polar = true) # A pure cosine
Sinusoidal parameters SinusoidP{Float64}:
Frequency (Hz) : 1.0
DC : 0.0
Sine amplitude (Q) : -0.0
Cosine amplitude (I): 1.0
```
"""
SinusoidP(f, DC, Q, I) = SinusoidP(promote(f, DC, Q, I)...)
function SinusoidP(f, DC, Q, I ; polar = false)
if polar
# argument Q is amplitude; I is phase
cosamp = Q*cos(I)
sinamp = -Q*sin(I)
return SinusoidP(promote(f, DC, sinamp, cosamp)... )
end
SinusoidP(promote(f, DC, Q, I)...)
end

"""
SinusoidP{T}(; f, DC, Q, I)
Expand Down
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ using Test
@test I -0.577491 atol = 0.01
end

@testset "Polar notation for SinusoidP" begin
s = SinusoidP(1, 0, 1, 0, polar = true) # A pure cosine
@test s.f == 1
@test s.DC == 0
@test s.Q 0
@test s.I 1
s = SinusoidP(1, 0, 1, -π/2, polar = true) # A pure sine
@test s.f == 1
@test s.DC == 0
@test s.Q 1 atol = 1e-12
@test s.I 0 atol = 1e-12
s = SinusoidP(1, 5, 1, -π/4, polar = true) # A mix
@test s.f == 1
@test s.DC == 5
@test s.Q sqrt(2)/2 atol = 1e-12
@test s.I sqrt(2)/2 atol = 1e-12
end

@testset "Easy sinusoidal regression tests" begin
# Easy tests: lots of equidistant points, no noise.
t = range(0, 1, length=100)
Expand Down

0 comments on commit 13bcd78

Please sign in to comment.