Skip to content

Commit

Permalink
better fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ericphanson committed Oct 16, 2023
1 parent e67744d commit 9fa77c8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/export_edf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ function reencode_samples(samples::Samples, sample_type::Type{<:Integer}=Int16)
# maximize the dynamic range of Int16 encoding.
samples = decode(samples)
smin, smax = extrema(samples.data)
# If the input is flat, normalize to zero and one
if smin == smax
smax = one(smax)
smin = zero(smin)
end

emin, emax = typemin(sample_type), typemax(sample_type)

Expand Down
4 changes: 0 additions & 4 deletions src/import_edf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ function edf_signal_encoding(edf_signal_header, edf_seconds_per_record)
dmin, dmax = edf_signal_header.digital_minimum, edf_signal_header.digital_maximum
pmin, pmax = edf_signal_header.physical_minimum, edf_signal_header.physical_maximum
sample_resolution_in_unit = (pmax - pmin) / (dmax - dmin)
if !isfinite(sample_resolution_in_unit) || iszero(sample_resolution_in_unit)
@warn "Unexpected computed `sample_resolution_in_unit`. Computed: $(sample_resolution_in_unit). Setting to 1." digital_minimum=dmin digital_maximum=dmax physical_minimum=pmin physical_maximum=pmax maxlog=10
sample_resolution_in_unit = one(sample_resolution_in_unit)
end
sample_offset_in_unit = pmin - (sample_resolution_in_unit * dmin)
sample_rate = edf_signal_header.samples_per_record / edf_seconds_per_record
sample_type = (dmax > typemax(Int16) || dmin < typemin(Int16)) ? "int32" : "int16"
Expand Down
3 changes: 1 addition & 2 deletions test/export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@

data = zeros(UInt64, 1, 2)
samples = Samples(data, info, false)
result = (@test_logs (:warn, r"Unexpected computed `sample_resolution_in_unit`.") OndaEDF.reencode_samples(samples))
@test result isa Samples
@test OndaEDF.reencode_samples(samples) isa Samples
end

end

0 comments on commit 9fa77c8

Please sign in to comment.