Skip to content

Commit

Permalink
optimize left edge finding in autotune
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Oct 10, 2020
1 parent dd1ac88 commit 88fc14a
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions autotune.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,18 @@ func (tune *autoTune) Sample(bit bool, seq uint32) {
// Period
// Falling Edge Rising Edge
func (tune *autoTune) FindPeriod(bit bool) int {
// find left and right edges for a bit
// last pulse and initial index setup
lastPulse := tune.pulses[0]
idx := 1

// left edge
var leftEdge int
for ; idx < len(tune.pulses); idx++ {
if lastPulse.seq+1 == tune.pulses[idx].seq {
if lastPulse.bit != bit && tune.pulses[idx].bit == bit {
if lastPulse.bit != bit && tune.pulses[idx].bit == bit { // edge found
if lastPulse.seq+1 == tune.pulses[idx].seq { // ensure edge continuity
leftEdge = idx
break
}
} else {
return -1
}
lastPulse = tune.pulses[idx]
}
Expand All @@ -51,8 +49,8 @@ func (tune *autoTune) FindPeriod(bit bool) int {
idx = leftEdge + 1

for ; idx < len(tune.pulses); idx++ {
if lastPulse.seq+1 == tune.pulses[idx].seq {
if lastPulse.bit == bit && tune.pulses[idx].bit != bit {
if lastPulse.seq+1 == tune.pulses[idx].seq { // ensure pulses in this level monotonic
if lastPulse.bit == bit && tune.pulses[idx].bit != bit { // edge found
rightEdge = idx
break
}
Expand Down

0 comments on commit 88fc14a

Please sign in to comment.