Skip to content

Commit

Permalink
WIP: more tests for the PBS tonal stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dingraha committed Apr 19, 2024
1 parent dd8cdec commit b7c8c5c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/AcousticMetrics.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module AcousticMetrics

using Base.Iterators: Iterators
using Base.Order: ord, Forward
using ConcreteStructs: @concrete
using FFTW: r2r!, R2HC, HC2R, rfftfreq
using FLOWMath: abs_cs_safe
Expand Down
5 changes: 4 additions & 1 deletion src/proportional_bands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,10 @@ Return the proportional band spectrum amplitude for the `i`th non-zero band in `

# What is the last index we want?
# It's the last one that has f_nb[i] <= fu
iend = searchsortedlast(f_nb, fu)
# iend = searchsortedlast(f_nb, fu)
# But we don't want to double-count frequencies, so we actually want f_nb[i] < fu.
# Could just do `searchsortedlast(f_nb, fu; lt=<=)`, but this avoids the possibly-slow keyword arguments.
iend = searchsortedlast(f_nb, fu, ord(<=, identity, nothing, Forward))
if iend == 0
# All the frequencies are lower than the band we're looking for.
return zero(eltype(pbs))
Expand Down
61 changes: 60 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ end
# First index we want in f_nb is the one that is greater than or equal to lband.
istart = searchsortedfirst(f_nb, lband)
# Last index we want in f_nb is th one that is less than or equal to uband.
iend = searchsortedlast(f_nb, uband)
iend = searchsortedlast(f_nb, uband; lt=<=)
# Now check that we get the right answer.
@test sum(msp[istart:iend]) amp
end
Expand Down Expand Up @@ -1737,6 +1737,43 @@ end
@test center_bands(pbs_scaled_non_lazy) === center_bands(pbs_scaled)
@test upper_bands(pbs_scaled_non_lazy) === upper_bands(pbs_scaled)
end

# Now, for the tonal stuff.
scaler = 1
tonal = true
pbs_tonal = LazyNBProportionalBandSpectrum(ApproximateOctaveBands, freq_min_nb, df_nb, msp, scaler, tonal)
# Narrowband frequencies go from 87 Hz to 1950 Hz, so check that.
cbands = center_bands(pbs_tonal)
@test band_start(cbands) == 6
@test band_end(cbands) == 11

# Now make sure we get the right answer.
lbands = lower_bands(pbs_tonal)
ubands = upper_bands(pbs_tonal)
for (lband, uband, amp) in zip(lbands, ubands, pbs_tonal)
# First index we want in f_nb is the one that is greater than or equal to lband.
istart = searchsortedfirst(f_nb, lband)
# Last index we want in f_nb is th one that is less than or equal to uband.
iend = searchsortedlast(f_nb, uband; lt=<=)
# Now check that we get the right answer.
@test sum(msp[istart:iend]) amp
end

# Now for the scaler stuff, can use the same trick for the non-tonal.
for scaler in [0.1, 0.5, 1.0, 1.5, 2.0]
freq_min_nb_scaled = freq_min_nb*scaler
freq_max_nb_scaled = freq_max_nb*scaler
df_nb_scaled = df_nb*scaler
msp_scaled = psd .* df_nb_scaled
pbs_scaled = LazyNBProportionalBandSpectrum(ApproximateOctaveBands, freq_min_nb_scaled, df_nb_scaled, msp_scaled, scaler, tonal)

# We've changed the frequencies, but not the PSD, so the scaled PBS should be the same as the original as long as we account for the different frequency bin widths via the `scaler`.
@test all(pbs_scaled./scaler .≈ pbs_tonal)
# And the band frequencies should all be scaled.
@test all(lower_bands(pbs_scaled)./scaler .≈ lower_bands(pbs_tonal))
@test all(center_bands(pbs_scaled)./scaler .≈ center_bands(pbs_tonal))
@test all(upper_bands(pbs_scaled)./scaler .≈ upper_bands(pbs_tonal))
end
end

@testset "spectrum, lowest narrowband on a left edge" begin
Expand Down Expand Up @@ -1805,6 +1842,28 @@ end
@test center_bands(pbs_scaled_non_lazy) === center_bands(pbs_scaled)
@test upper_bands(pbs_scaled_non_lazy) === upper_bands(pbs_scaled)
end

# Now, for the tonal stuff.
scaler = 1
tonal = true
pbs_tonal = LazyNBProportionalBandSpectrum(ApproximateOctaveBands, freq_min_nb, df_nb, msp, scaler, tonal)
# Narrowband frequencies go from 89 Hz to 1950 Hz, so check that.
cbands = center_bands(pbs_tonal)
@test band_start(cbands) == 7
@test band_end(cbands) == 11

# Now make sure we get the right answer.
lbands = lower_bands(pbs_tonal)
ubands = upper_bands(pbs_tonal)
for (lband, uband, amp) in zip(lbands, ubands, pbs_tonal)
# First index we want in f_nb is the one that is greater than or equal to lband.
istart = searchsortedfirst(f_nb, lband)
# Last index we want in f_nb is th one that is less than or equal to uband.
iend = searchsortedlast(f_nb, uband; lt=<=)
# Now check that we get the right answer.
@test sum(msp[istart:iend]) amp
end

end

@testset "spectrum, highest narrowband on a left edge" begin
Expand Down

0 comments on commit b7c8c5c

Please sign in to comment.