Skip to content

Commit

Permalink
use 64-bit precision for nanoseconds_per_sample (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrevels authored Oct 31, 2022
1 parent 0789d48 commit 9829236
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TimeSpans"
uuid = "bb34ddd2-327f-4c4a-bfb0-c98fc494ece1"
authors = ["Beacon Biosignals, Inc."]
version = "0.3.4"
version = "0.3.5"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
5 changes: 4 additions & 1 deletion src/TimeSpans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,11 @@ duration(span) = stop(span) - start(span)
TimeSpans.nanoseconds_per_sample(sample_rate)
Given `sample_rate` in Hz, return the number of nanoseconds corresponding to one sample.
Note that this function performs the relevant calculation using `Float64(sample_rate)`
in order to improve the accuracy of the result.
"""
nanoseconds_per_sample(sample_rate) = inv(sample_rate) * NS_IN_SEC
nanoseconds_per_sample(sample_rate) = NS_IN_SEC / Float64(sample_rate)

"""
index_from_time(sample_rate, sample_time::Period)
Expand Down
18 changes: 9 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,7 @@ end
@test index == index_from_time(Float64(rate), sample_time)
end
end
end

@testset "`in` and `findall`" begin
@test findall(in(TimeSpan(1, 10)), Nanosecond.(5:15)) == 1:5
@test findall(in(TimeSpan(1, 10)), map(Nanosecond, (9,10,11))) == 1:1
@test in(TimeSpan(1,2))(Nanosecond(1))
@test !in(TimeSpan(1,2))(Nanosecond(2))
end

@testset "index_from_time" begin
@testset "docstring" begin
@test index_from_time(1, Second(0)) == 1
@test index_from_time(1, Second(1)) == 2
Expand All @@ -147,13 +138,22 @@ end
@test index_from_time(200, ns) == 30001
@test index_from_time(200e0, ns) == 30001
@test index_from_time(200f0, ns) == 30001
@test time_from_index(143.5, 8611) == Nanosecond(60000000000)
@test time_from_index(Float32(143.5), 8611) == Nanosecond(60000000000)
end

for i in 1:10
@test index_from_time(1.5, time_from_index(1.5, 1:i)) == 1:i
end
end

@testset "`in` and `findall`" begin
@test findall(in(TimeSpan(1, 10)), Nanosecond.(5:15)) == 1:5
@test findall(in(TimeSpan(1, 10)), map(Nanosecond, (9,10,11))) == 1:1
@test in(TimeSpan(1,2))(Nanosecond(1))
@test !in(TimeSpan(1,2))(Nanosecond(2))
end

@testset "merge_spans!" begin
spans = [TimeSpan(0, 10), TimeSpan(6, 12), TimeSpan(15, 20),
TimeSpan(21, 30), TimeSpan(29, 31)]
Expand Down

2 comments on commit 9829236

@jrevels
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/71365

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.5 -m "<description of version>" 9829236e1474137f8bdcef306dd3701d8f9ac066
git push origin v0.3.5

Please sign in to comment.