Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ MetalExt = "Metal"
[compat]
AbstractFFTs = "1.5.0"
Adapt = "4.7.0"
CUDA = "6.1"
CUDA = "6.2"
cuFFT = "6.2"
Documenter = "0.27, 1"
FFTW = "1.4"
Interpolations = "0.13, 0.14, 0.15, 0.16"
LinearAlgebra = "1"
Metal = "1.10"
SpecialFunctions = "1.3, 2"
Wavelets = "0.9, 0.10"
cuFFT = "6.1"
julia = "1.9"

[extras]
Expand Down
1 change: 1 addition & 0 deletions src/ContinuousWavelets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export qmf,
mother,
father,
getMeanFreq,
getPeakFreq,
isAnalytic,
getDualCoverage,
caveats
Expand Down
21 changes: 15 additions & 6 deletions src/apply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,23 @@ struct DualFrames <: InverseType end
struct NaiveDelta <: InverseType end
struct PenroseDelta <: InverseType end

_backend(x::AbstractArray) = Base.typename(typeof(x)).wrapper
#= Remedy for an error that emerged from GPU implementation. _backend was not
returning the compute device as expected. To remedy, recurses on `parent`
covers for every wrapper that defines it — Base's views, reshapes and reinterprets,
LinearAlgebra's Adjoint/Transpose, and package types such as OffsetArrays —
and leaves anything that does not define `parent` reporting its own type. =#
function _unwrapped(x::AbstractArray)
p = parent(x)
return p === x ? x : _unwrapped(p)
end

_backend(x::AbstractArray) = Base.typename(typeof(_unwrapped(x))).wrapper

function _checkMatchingDevice(Y::AbstractArray, daughters::AbstractArray)
_backend(Y) === _backend(daughters) ||
error("cwt: signal is a $(_backend(Y)) but daughters is a " *
"$(_backend(daughters)) -- move daughters onto the same " *
"device as the signal before calling cwt, e.g. " *
"CuArray(daughters) or MtlArray(daughters).")
_backend(Y) === _backend(daughters) || error(
"cwt: signal is on $(_backend(Y)) but daughters is on $(_backend(daughters)). " *
"Both must be on the same device: convert the signal with " *
"$(_backend(daughters))(Y), or pass daughters yourself via cwt(Y, cWav, daughters).")
return nothing
end

Expand Down
60 changes: 37 additions & 23 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,31 +256,47 @@ function getUpperBound(c::CWT{W,T,<:Morse,N}, s) where {W,T,N}
end

"""
getMeanFreq(Ŵ, fsample=2000) -> arrayOfFreqs
#getMeanFreq(Ŵ, ω, fsample, n1) -> arrayOfFreqs

Calculate each of the mean frequencies of a collection of analytic or real wavelets `Ŵ`, using the power spectral density.
`ω` is the raw (fsample-independent) frequency axis used to construct `Ŵ`, and `n1` is the original signal length —
both are needed to correctly convert `Ŵ`'s row-index axis into real Hz for the given `fsample`.
"""
function getMeanFreq(Ŵ::Array, fsample = 2000)
eachNorm = [norm(w, 2)^2 for w in eachcol(Ŵ)]
freqs = range(0, fsample / 2, length = size(Ŵ, 1))
return map(ŵ -> sum(abs2.(ŵ) .* freqs), eachcol(Ŵ)) ./ eachNorm
end
#=
function getMeanFreq(Ŵ::Array, ω, fsample, n1)
getMeanFreq(Ŵ, fsample=2000, decimation=1) -> arrayOfFreqs

PSD-weighted mean frequency of each filter in `Ŵ`.

`decimation` is the cumulative subsampling applied before this bank (1 for the
first layer, ∏ poolingRate for deeper ones). It sets the bank's true Nyquist:
without it the row-index axis is stretched to `fsample/2` regardless of the rate
the filters actually see, inflating every value by the decimation factor.

Note this returns the *centroid* of the power spectrum, which for low-Q banks and
near-lowpass filters sits well below the peak response — see `getPeakFreq` if you
want the frequency that maximally excites a filter.
"""
function getMeanFreq(Ŵ::Array, fsample = 2000, decimation = 1)
eachNorm = [norm(w, 2)^2 for w in eachcol(Ŵ)]
freqsHz = ω .* (fsample / 2) ./ (n1 >> 1 + 1)
return map(ŵ -> sum(abs2.(ŵ) .* freqsHz), eachcol(Ŵ)) ./ eachNorm
end=#
freqs = range(0, fsample / (2 * decimation), length = size(Ŵ, 1))
return map(ŵ -> sum(abs2.(ŵ) .* freqs), eachcol(Ŵ)) ./ eachNorm
end

function getMeanFreq(n1, cw::CWT)
Ŵ, ω = computeWavelets(n1, cw)
getMeanFreq(Ŵ, cw.fsample)
#getMeanFreq(Ŵ, ω, cw.fsample, n1)
function getMeanFreq(n1, cw::CWT, decimation = 1)
Ŵ, ω = computeWavelets(n1, cw)
getMeanFreq(Ŵ, cw.fsample, decimation)
end

"""
getPeakFreq(Ŵ, fsample=2000, decimation=1) -> arrayOfFreqs

Frequency of each filter's maximum response, rather than the PSD-weighted mean
returned by `getMeanFreq`. For a signal that maximally excites a given filter,
this is the frequency to use: the mean is pulled toward DC by the low-frequency
tail, badly so for low-Q banks and for near-lowpass filters.
"""
function getPeakFreq(Ŵ::Array, fsample = 2000, decimation = 1)
freqs = range(0, fsample / (2 * decimation), length = size(Ŵ, 1))
return map(ŵ -> freqs[argmax(abs2.(ŵ))], eachcol(Ŵ))
end

function getPeakFreq(n1, cw::CWT, decimation = 1)
Ŵ, ω = computeWavelets(n1, cw)
getPeakFreq(Ŵ, cw.fsample, decimation)
end

# orthogonal wavelet utils
function getContWaveFromOrtho(c, N)
Expand Down Expand Up @@ -326,7 +342,6 @@ end


# create interpolater for the orthogonal cases
#genInterp(ψ) = scale(interpolate(ψ, BSpline(Cubic(Reflect(OnGrid())))), 1:length(ψ))
genInterp(ψ) = linear_interpolation(1:length(ψ),ψ)


Expand Down Expand Up @@ -439,7 +454,6 @@ function caveats(n1, c::CWT; coiTolerance = exp(-2))
# Fourier equivalent frequencies
Ŵ, ω = computeWavelets(n1, c)
freqs = getMeanFreq(Ŵ, c.fsample)
#freqs = getMeanFreq(Ŵ, ω, c.fsample, n1)
coi = directCoiComputation(n1, c; coiTolerance = coiTolerance)
return sRange, freqs, coi
end
Expand Down
2 changes: 1 addition & 1 deletion test/CUDATests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ using BenchmarkTools
# to move `daughters` over) should fail fast with a clear message,
# not the opaque GPU-kernel-compilation KernelError this used to
# produce.
@test_throws "signal is a" cwt(x_gpu, wfc, daughters_cpu)
@test_throws "same device" cwt(x_gpu, wfc, daughters_cpu)

# icwt on a fully-GPU forward transform should now work
# transparently for all three InverseType variants: β/
Expand Down
2 changes: 1 addition & 1 deletion test/MetalTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ using BenchmarkTools
daughters_cpu = Float32.(daughters_cpu)
daughters_mtl = MtlArray(daughters_cpu)

@test_throws "signal is a" cwt(x_mtl, wfc, daughters_cpu)
@test_throws "same device" cwt(x_mtl, wfc, daughters_cpu)

y_mtl = with_logger(ConsoleLogger(stderr, Logging.Error)) do
cwt(x_mtl, wfc, daughters_mtl)
Expand Down
18 changes: 18 additions & 0 deletions test/basicTypesAndNumber.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,21 @@
end
# xSize = xSizes[3]; boundary = boundaries[3]; s = sVals[end]; β = βs[3]; wave = waveTypes[4]
# xSize = 33; boundary = SymBoundary(); s = 1; β = 1; wave = dog2

@testset "wrapped CPU inputs" begin
x = randn(Float32, 4, 128, 2)
X = randn(Float32, 2, 128)
with_logger(ConsoleLogger(stderr, Logging.Error)) do
v = view(x, 1, :, 1) # SubArray
@test cwt(v, Morlet()) ≈ cwt(collect(v), Morlet())

t = transpose(X) # Transpose, (128, 2)
@test cwt(t, Morlet()) ≈ cwt(collect(t), Morlet())

p = PermutedDimsArray(X, (2, 1)) # PermutedDimsArray, (128, 2)
@test cwt(p, Morlet()) ≈ cwt(collect(p), Morlet())

r = reshape(view(x, 1, :, :), 256) # ReshapedArray over a strided view
@test cwt(r, Morlet()) ≈ cwt(collect(r), Morlet())
end
end
Loading