【问题标题】:Scala: Haar Wavelet TransformScala:哈尔小波变换
【发布时间】:2017-05-25 21:37:04
【问题描述】:

我正在尝试在 Scala 中实现 Haar 小波变换。我正在使用此 Python 代码作为参考 Github Link to Python implementation of HWT

我还在这里提供了我的 Scala 代码版本。我是 Scala 新手,所以请原谅我的代码不太好。

/**
  * 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
}
}

但是代码卡在“var deltas1 = encode(samples(i), ubound)”并且没有给出任何输出。如何改进我的实施?提前致谢!

【问题讨论】:

  • 请将您遇到的错误告诉我们。
  • @marstran 嘿!我摆脱了错误,现在我的代码没有给出任何错误,但它卡在“var deltas1 = encode(samples(i), ubound)”这一行并且没有给出任何输出。
  • “卡住”是什么意思?你有无限循环吗?
  • 您只是用 Scala 语法编写了 Python 代码,但没有编写惯用的 Scala 代码。您的代码是必要的,而您可以使用函数式编程概念编写更好的代码。最重要的是避免使用var,而是使用val。并尽量避免使用while/for 并改用mapflatMap。如果您编写惯用的 Scala 代码,您会发现大部分运行时错误都会转移到编译时。
  • @AmirKarimi 当然先生!我会记住这一点。谢谢你帮助我学习!这是我在 stackverflow 上的第一个问题,我得到了如此积极的回应!再次感谢! :)

标签: scala math heap-memory haar-wavelet


【解决方案1】:

你的错误在这一行:

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

这不会从列表中删除索引 0。它删除元素 1(如果存在)。这意味着列表永远不会为空。因此,while 循环 while (lst.nonEmpty) 将永远不会终止。

要删除列表的第一个元素,只需使用lst.remove(0)

【讨论】:

    猜你喜欢
    • 2013-02-24
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 2018-10-20
    相关资源
    最近更新 更多