0

I am trying to implement Haar Wavelet Transform in Scala. I am using this Python Code for reference Github Link to Python implementation of HWT

I am also giving here my Scala code version. I am new to Scala so forgive me for not-so-good-code.

/**
  * Created by vipul vaibhaw on 1/11/2017.
  */

import scala.collection.mutable.{ListBuffer, MutableList,ArrayBuffer}

object HaarWavelet {

def main(args: Array[String]): Unit = {

  var samples = ListBuffer(
  ListBuffer(1,4),
  ListBuffer(6,1),
  ListBuffer(0,2,4,6,7,7,7,7),
  ListBuffer(1,2,3,4),
  ListBuffer(7,5,1,6,3,0,2,4),
 ListBuffer(3,2,3,7,5,5,1,1,0,2,5,1,2,0,1,2,0,2,1,0,0,2,1,2,0,2,1,0,0,2,1,2)
)

for (i <- 0 to samples.length){
  var ubound = samples(i).max+1
  var length = samples(i).length
  var deltas1 = encode(samples(i), ubound)
  var deltas = deltas1._1
  var avg = deltas1._2


  println( "Input:      %s, boundary = %s, length = %s" format(samples(i), ubound, length))
  println( "Haar output:%s, average = %s" format(deltas, avg))
  println("Decoded:    %s" format(decode(deltas, avg, ubound)))
  }
}

def wrap(value:Int, ubound:Int):Int = {
(value+ubound)%ubound
}

def encode(lst1:ListBuffer[Int], ubound:Int):(ListBuffer[Int],Int)={
   //var lst = ListBuffer[Int]()
   //lst1.foreach(x=>lst+=x)
   var lst = lst1
   var deltas = new ListBuffer[Int]()
   var avg = 0

   while (lst.length>=2) {
   var avgs = new ListBuffer[Int]()

   while (lst.nonEmpty) {
    // getting first two element from the list and removing them
    val a = lst.head
    lst -= 1 // removing index 0 element from the list
    val b = lst.head
    lst -= 1 // removing index 0 element from the list

    if (a<=b) {
      avg = (a + b)/2
    }
    else{
      avg = (a+b+ubound)/2
    }
    var delta = wrap(b-a,ubound)
    avgs += avg
    deltas += delta
  }
  lst = avgs
}
(deltas, avg%ubound)
}

def decode(deltas:ListBuffer[Int],avg:Int,ubound:Int):ListBuffer[Int]={
var avgs = ListBuffer[Int](avg)
var l = 1

while(deltas.nonEmpty){
  for(i <- 0 to l ){
    val delta = deltas.last
    deltas -= -1
    val avg = avgs.last
    avgs -= -1

    val a = wrap(math.ceil(avg-delta/2.0).toInt,ubound)
    val b = wrap(math.ceil(avg+delta/2.0).toInt,ubound)
  }
  l*=2
}
avgs
}

def is_pow2(n:Int):Boolean={
(n & -n) == n
}
}

But Code gets stuck at "var deltas1 = encode(samples(i), ubound)" and doesn't give any output. How can I improve my implementation? Thanks in advance!

Vipul Vaibhaw
  • 395
  • 7
  • 15
  • Please give us the error you get. – marstran Jan 11 '17 at 07:33
  • @marstran Hey! I got rid of the error, now my code doesn't gives any error but it gets stuck at "var deltas1 = encode(samples(i), ubound)" this line and doesn't give any output. – Vipul Vaibhaw Jan 11 '17 at 07:39
  • What do you mean "gets stuck"? Do you have an infinite loop? – marstran Jan 11 '17 at 07:48
  • You just wrote the Python code in Scala syntax but you didn't write idiomatic Scala code. Your code is imperative while you can write a better code using functional programming concepts. The most important thing is to avoid `var` and use `val` instead. And try to avoid `while`/`for` and use `map` or `flatMap` instead. If you write idiomatic Scala code you will see that most of your runtime errors would be moved to the compile time. – Amir Karimi Jan 11 '17 at 08:21
  • 1
    @AmirKarimi Sure Sir! I will keep that in mind. Thanks for helping me to learn! It was my first question on stackverflow and I got such positive response! Thanks again! :) – Vipul Vaibhaw Jan 11 '17 at 08:24

1 Answers1

0

Your error is on this line:

lst -= 1 // removing index 0 element from the list. 

This doesn't remove index 0 from the list. It removes the element 1 (if it exists). This means that the list never becomes empty. The while-loop while (lst.nonEmpty) will therefore never terminate.

To remove the first element of the list, simply use lst.remove(0).

marstran
  • 26,413
  • 5
  • 61
  • 67