I am developing a library for basic waveform characterization. Linked WaveformAnalysis.jl
I have a few of the functions done pretty well done in the most efficient way I can think of. I am getting hung up right now with my “pulses” function which will simultaneously measure all of the pulse widths for the given polarity. I am trying to have a more finessed way of handling all the edge cases for a captured waveform. The simplest case (to me) is to count all the positive and negative crosses in the waveform, then subtract either the positives from the negatives for looking for negative pulses or subtracting the positive crosses from the negative crosses to get the positive pulses.
The trouble comes when there are not an equal number of detected edges. I have the incomplete function pasted below: (incomplete because it is missing code to handle negative pulses)
Edit: I think I came up with a good solution, pasted below
function detectcrosses(x::Vector{T}, thresh::T, edge::Edge) where {T <: Real}
events = (edge == Rising ? x .>= thresh : x .<= thresh)
findall(events .& (.~events >> 1))
end
pulses(x::Vector{T}, thresh::T, pol::Polarity) where T <: Real =
measurepulses(detectcrosses(x, thresh, Rising), detectcrosses(x, thresh, Falling), pol)
function measurepulses(poscrosses::Vector{T}, negcrosses::Vector{T}, pol::Polarity) where T <: Int64
if !isempty(poscrosses) && !isempty(negcrosses)
if pol == ActiveHigh
alignedges!(poscrosses, negcrosses)
negcrosses .- poscrosses
else
alignedges!(negcrosses, poscrosses)
poscrosses .- negcrosses
end
else
nothing
end
end
for more context, please take a look at my code in src/WaveformAnalysis.jl. Any additional feedback is welcome for my code not pertaining to my exact question as I am a relatively new Julia coder.
Thanks in advance!