Skip to content

Commit

Permalink
Fixed Bandpower to actually calculate bandpower and not bandenergy
Browse files Browse the repository at this point in the history
  • Loading branch information
cem-yalcin committed Nov 20, 2018
1 parent 07d57e0 commit e8cd90d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/main/scala/features/Bandpower.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package features

import breeze.numerics.log10
import chisel3._
import dsptools.numbers._
import dspjunctions._
Expand All @@ -24,7 +25,9 @@ object BandpowerIO {
def apply[T <: Data](params: BandpowerParams[T]): BandpowerIO[T] = new BandpowerIO(params)
}

class Bandpower[T <: Data : Real](val params: BandpowerParams[T]) extends Module {
class Bandpower[T <: Data : Real : BinaryRepresentation](val params: BandpowerParams[T]) extends Module {
require( params.idxEndBin > params.idxStartBin )
require( ( (params.idxEndBin - params.idxStartBin + 1) & (params.idxEndBin - params.idxStartBin + 1 - 1)) == 0 )
val io = IO(new BandpowerIO[T](params))

// TODO: sqrt, division
Expand All @@ -35,7 +38,7 @@ class Bandpower[T <: Data : Real](val params: BandpowerParams[T]) extends Module
// Concatenate back in unscaled DC and sampling freq elems
val p1 = VecInit(p2(0)) ++ p1Scaled ++ VecInit(p2(params.nBins/2))
// Just sum because already squared
io.out.bits := p1.slice(params.idxStartBin, params.idxEndBin).reduce(_ + _)
io.out.bits := p1.slice(params.idxStartBin, params.idxEndBin).reduce(_ + _) >> (log10(params.idxEndBin - params.idxStartBin + 1) / log10(2)).toInt

io.out.valid := io.in.valid
io.out.sync := io.in.sync
Expand Down
16 changes: 9 additions & 7 deletions src/test/scala/BandpowerTester.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import chisel3.core.FixedPoint
import dsptools.numbers._
import dsptools.DspTester
import breeze.math.Complex
import breeze.numerics.floor

import scala.util.Random

class GoldenDoubleBandpower(nBins: Int, idxStartBin: Int, idxEndBin: Int) {
class GoldenDoubleBandpower(nBins: Int, idxStartBin: Int, idxEndBin: Int, testType: Int) {
def poke(input: Seq[Complex]): Double = {
// Take mag squared of FFT output
val p2 = input.map{ case x => x.abs * x.abs}
Expand All @@ -19,18 +20,19 @@ class GoldenDoubleBandpower(nBins: Int, idxStartBin: Int, idxEndBin: Int) {
val p1 = Seq(p2(0)) ++ p1Scaled ++ Seq(p2(nBins / 2))
// Just sum because already squared
val output = p1.slice(idxStartBin, idxEndBin).sum
output
if(testType == 0) floor(output/(idxEndBin - idxStartBin + 1))
else output/(idxEndBin - idxStartBin + 1)
}
}

class BandpowerTester[T <: Data](c: Bandpower[T], params: BandpowerParams[T], testType: Int = 0) extends DspTester(c) {
val nBins = params.nBins
val idxStartBin = params.idxStartBin
val idxEndBin = params.idxEndBin
val bandpower = new GoldenDoubleBandpower(nBins, idxStartBin, idxEndBin)
val bandpower = new GoldenDoubleBandpower(nBins, idxStartBin, idxEndBin, testType)

var input = Seq.fill(nBins)(Complex(Random.nextDouble(), Random.nextDouble()))
if (testType == 1) { // UInt
if (testType == 0) { // UInt
input = Seq.fill(nBins)(Complex(Random.nextInt(16), Random.nextInt(16)))
}

Expand All @@ -40,7 +42,7 @@ class BandpowerTester[T <: Data](c: Bandpower[T], params: BandpowerParams[T], te
poke(c.io.in.valid, value = 1)
step(1)

if (testType == 0) { // FixedPoint
if (testType == 1) { // FixedPoint
fixTolLSBs.withValue(19) {
expect(c.io.out.bits, goldenModelResult, msg = s"Input: $input, Golden: $goldenModelResult, ${peek(c.io.out.bits)}")
}
Expand All @@ -51,14 +53,14 @@ class BandpowerTester[T <: Data](c: Bandpower[T], params: BandpowerParams[T], te
object UIntBandpowerTester {
def apply(params: BandpowerParams[UInt]): Boolean = {
dsptools.Driver.execute(() => new Bandpower(params), TestSetup.dspTesterOptions) {
c => new BandpowerTester(c, params, 1)
c => new BandpowerTester(c, params, 0)
}
}
}
object FixedPointBandpowerTester {
def apply(params: BandpowerParams[FixedPoint]): Boolean = {
dsptools.Driver.execute(() => new Bandpower(params), TestSetup.dspTesterOptions) {
c => new BandpowerTester(c, params, 0)
c => new BandpowerTester(c, params, 1)
}
}
}
2 changes: 2 additions & 0 deletions src/test/scala/WellnessIntegrationTester.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ class wellnessTester[T <: chisel3.Data](c: WellnessModule[T], goldenModelParamet
goldenModelParameters.bandpower1Params.nBins,
goldenModelParameters.bandpower1Params.idxStartBin,
goldenModelParameters.bandpower1Params.idxEndBin,
testType
)
val bandpower2 = new GoldenDoubleBandpower(
goldenModelParameters.bandpower2Params.nBins,
goldenModelParameters.bandpower2Params.idxStartBin,
goldenModelParameters.bandpower2Params.idxEndBin,
testType
)
val SVM = new GoldenSVM(
goldenModelParameters.svmParams.nSupports,
Expand Down

0 comments on commit e8cd90d

Please sign in to comment.