Skip to content

Commit

Permalink
refactor(dsp): do not export sfilt, sfiltfilt and sresample, but re-e…
Browse files Browse the repository at this point in the history
…xport DSP.stft
  • Loading branch information
mchitre committed Jul 21, 2024
1 parent 61082d5 commit 956be32
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SignalAnalysis"
uuid = "df1fea92-c066-49dd-8b36-eace3378ea47"
authors = ["Mandar Chitre <[email protected]>"]
version = "0.8.0"
version = "0.9.0"

[deps]
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
Expand Down
2 changes: 1 addition & 1 deletion src/SignalAnalysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using SignalBase
using SignalBase.Units

export nframes, nchannels, sampletype, framerate, duration
export db2amp, amp2db, pow2db, db2pow
export db2amp, amp2db, pow2db, db2pow, stft
export 𝓈, ms, Hz, kHz

const 𝓈 = Units.s
Expand Down
33 changes: 14 additions & 19 deletions src/dsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Optim: optimize, minimizer, BFGS
export fir, removedc, removedc!, demon
export upconvert, downconvert, rrcosfir, rcosfir
export mseq, gmseq, circconv, goertzel, pll, hadamard
export sfilt, sfiltfilt, sresample, mfilter, findsignal
export mfilter, findsignal
export istft, whiten, filt, filtfilt, resample, delay!, compose

"""
Expand Down Expand Up @@ -410,45 +410,40 @@ function pll(x::AbstractVecOrMat, fc=0.0, bandwidth=1e-3; fs=framerate(x))
signal(y, fs)
end

sfilt(f::AbstractVector{<:Number}, x::AbstractVector) = signal(filt(f, samples(x)), framerate(x))
sfilt(f::AbstractVector{<:Number}, x::AbstractVector, si) = signal(filt(f, samples(x), si), framerate(x))
sfilt(b::AbstractVector{<:Number}, a::Union{Number,AbstractVector}, x::AbstractVector) = signal(filt(b, a, samples(x)), framerate(x))
sfilt(b::AbstractVector{<:Number}, a::Union{Number,AbstractVector}, x::AbstractVector, si) = signal(filt(b, a, samples(x), si), framerate(x))
sfiltfilt(coef, x) = signal(filtfilt(coef, samples(x)), framerate(x))
sresample(x, rate) = signal(resample(samples(x), rate; dims=1), rate * framerate(x))
sresample(x, rate, coef) = signal(resample(samples(x), rate, coef; dims=1), rate * framerate(x))

"""
sfilt(f, x[, si])
filt(f, x::SampledSignal[, si])
sfilt(b, a, x[, si])
filt(b, a, x::SampledSignal[, si])
Same as [`filt`](https://docs.juliadsp.org/stable/filters/#DSP.filt),
but retains sampling rate information.
"""
sfilt(f::AbstractVector{<:Number}, x::AbstractVector) = signal(filt(f, samples(x)), framerate(x))
sfilt(f::AbstractVector{<:Number}, x::AbstractVector, si) = signal(filt(f, samples(x), si), framerate(x))
sfilt(b::AbstractVector{<:Number}, a::Union{Number,AbstractVector}, x::AbstractVector) = signal(filt(b, a, samples(x)), framerate(x))
sfilt(b::AbstractVector{<:Number}, a::Union{Number,AbstractVector}, x::AbstractVector, si) = signal(filt(b, a, samples(x), si), framerate(x))
DSP.filt(f::AbstractVector{<:Number}, x::SampledSignal) = sfilt(f, x)
DSP.filt(f::AbstractVector{<:Number}, x::SampledSignal, si) = sfilt(f, x, si)
DSP.filt(b::AbstractVector{<:Number}, a::Union{Number,AbstractVector}, x::SampledSignal) = sfilt(b, a, x)
DSP.filt(b::AbstractVector{<:Number}, a::Union{Number,AbstractVector}, x::SampledSignal, si) = sfilt(b, a, x, si)

"""
sfiltfilt(coef, x)
filtfilt(coef, x::SampledSignal)
Same as [`filtfilt`](https://docs.juliadsp.org/stable/filters/#DSP.Filters.filtfilt),
but retains sampling rate information.
"""
sfiltfilt(coef, x) = signal(filtfilt(coef, samples(x)), framerate(x))
DSP.Filters.filtfilt(coef::AbstractVector{<:Number}, x::SampledSignal) = sfiltfilt(coef, x)

"""
sresample(x, rate[, coef])
resample(x::SampledSignal, rate[, coef])
Same as [`resample`](https://docs.juliadsp.org/stable/filters/#DSP.Filters.resample),
but correctly handles sampling rate conversion.
"""
sresample(x, rate) = signal(resample(samples(x), rate; dims=1), rate * framerate(x))
sresample(x, rate, coef) = signal(resample(samples(x), rate, coef; dims=1), rate * framerate(x))

# overload DSP versions of the above functions
DSP.filt(f::AbstractVector{<:Number}, x::SampledSignal) = sfilt(f, x)
DSP.filt(f::AbstractVector{<:Number}, x::SampledSignal, si) = sfilt(f, x, si)
DSP.filt(b::AbstractVector{<:Number}, a::Union{Number,AbstractVector}, x::SampledSignal) = sfilt(b, a, x)
DSP.filt(b::AbstractVector{<:Number}, a::Union{Number,AbstractVector}, x::SampledSignal, si) = sfilt(b, a, x, si)
DSP.Filters.filtfilt(coef::AbstractVector{<:Number}, x::SampledSignal) = sfiltfilt(coef, x)
DSP.Filters.resample(x::SampledSignal, rate::Real) = sresample(x, rate)
DSP.Filters.resample(x::SampledSignal, rate::Real, coef::Vector) = sresample(x, rate, coef)

Expand Down
16 changes: 2 additions & 14 deletions test/tests-core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -528,28 +528,16 @@ function test_dsp()

x = cw(7kHz, 1s, 44.1kHz)
f = fir(127, 5kHz, 10kHz; fs=44.1kHz)
x1 = sfilt(f, x)
@test framerate(x1) == framerate(x)
x1 = filt(f, x)
@test framerate(x1) == framerate(x)
x1 = sfilt(f, 1, x)
@test framerate(x1) == framerate(x)
x1 = filt(f, 1, x)
@test framerate(x1) == framerate(x)
x1 = sfiltfilt(f, x)
@test framerate(x1) == framerate(x)
x1 = filtfilt(f, x)
@test framerate(x1) == framerate(x)
x1 = sresample(x, 3//2)
@test framerate(x1) == 3 * framerate(x) / 2
x1 = sresample(x, 3//2, [1,1,1])
@test framerate(x1) == 3 * framerate(x) / 2
x1 = sresample([x x], 3//2)
@test framerate(x1) == 3 * framerate(x) / 2
x1 = sresample([x x], 3//2, [1,1,1])
@test framerate(x1) == 3 * framerate(x) / 2
x1 = resample(x, 3//2)
@test framerate(x1) == 3 * framerate(x) / 2
x1 = resample(x, 3//2, [1,1,1])
@test framerate(x1) == 3 * framerate(x) / 2
x1 = resample([x x], 3//2)
@test framerate(x1) == 3 * framerate(x) / 2
x1 = resample([x x], 3//2, [1,1,1])
Expand Down

0 comments on commit 956be32

Please sign in to comment.