Skip to content

Commit

Permalink
Introduce Signal Measurement Demo
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed Oct 4, 2023
1 parent b3e4ee0 commit 5e4282b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/stage1/forward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ function (::∂☆{N})(::ZeroBundle{N, typeof(map)}, f::ATB{N}, args::ATB{N, <:A
rebundle(map(FwdMap(f), map(unbundle, args)...))
end


function (::∂☆{N})(::ZeroBundle{N, typeof(map)}, f::ATB{N}, args::ATB{N}...) where {N}
∂☆recurse{N}()(ZeroBundle{N, typeof(map)}(map), f, args...)
end
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const bwd = Diffractor.PrimeDerivativeBack
"forward.jl",
"reverse.jl",
"regression.jl",
"AbstractDifferentiationTests.jl"
"AbstractDifferentiationTests.jl",
"signal_measurement.jl",
#"pinn.jl", # Higher order control flow not yet supported (https://github.com/JuliaDiff/Diffractor.jl/issues/24)
)
include(file)
Expand Down
64 changes: 64 additions & 0 deletions test/signal_measurement.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
This is a demo of using Diffractor for Very basic signal processing.
Where you have a signal that is represented as a series of sampled readings.
Not even something as sophistricated as FFT, just time domain edge detection stuff.
Its an important use case so we test it directly.
"""
module SignalMeasurement

using Diffractor
using Diffractor: ∂☆, ZeroBundle, TaylorBundle
using Diffractor: bundle, first_partial, TaylorTangentIndex


function make_soft_square_pulse(width, hardness=100)
function soft_square(t)
1/(1+exp(-hardness*(t-0.25))) - 1/(1+exp(-hardness*(t-0.25-width)))
end
end
#soft_square = make_soft_square_pulse(0.5, 8)
#signal = soft_square.(0:0.001:1)
#scatter(signal)

function determine_width(xs, ts)
# vs real signal processing functions this is not very robust, but for sake of demonstration it is fine.
@assert eachindex(xs) == eachindex(ts)

start_idx = nothing
end_idx = nothing
for ii in eachindex(xs)
x = xs[ii]
if isnothing(start_idx)
if x > 0.5
start_idx = ii
end
else
if x < 0.5
end_idx = ii
break
end
end
end

(isnothing(start_idx) || isnothing(end_idx)) && throw(DomainError("no pulse found"))
return ts[end_idx] - ts[start_idx]
end

#ts = 0:0.001:1
#determine_width(make_soft_square_pulse(0.5, 5).(ts), ts)


function signal_problem(width)
func = make_soft_square_pulse(width, 8)
ts = 0.0:0.001:1.0
signal = func.(ts)
return determine_width(signal, ts)
end



🐰 = ∂☆{1}()(ZeroBundle{1}(signal_problem), TaylorBundle{1}(0.5, (1.0,)))
@test primal(🐰) signal_problem(0.5)
@test first_partial(🐰) 1.0

end # module

0 comments on commit 5e4282b

Please sign in to comment.