Last active
August 29, 2015 14:07
-
-
Save piyo7/e86487d54398682ce8c5 to your computer and use it in GitHub Desktop.
Scalaで高速フーリエ変換の簡易実装 ref: http://qiita.com/piyo7/items/d942c6e15c988bb5fa9f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sbt.version=0.13.6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := "fft" | |
version := "1.0" | |
scalaVersion := "2.11.2" | |
libraryDependencies ++= Seq( | |
"org.scalanlp" % "breeze_2.11" % "0.10-SNAPSHOT", | |
"org.scalanlp" % "breeze-natives_2.11" % "0.10-SNAPSHOT" | |
) | |
resolvers ++= Seq( | |
"Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/", | |
"Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object FFT extends App { | |
import breeze.linalg.DenseVector | |
import breeze.math.Complex | |
def fft(f: DenseVector[Complex]): DenseVector[Complex] = { | |
require( | |
(f.size == 0) || | |
math.pow(2, (math.log(f.size) / math.log(2)).round).round == f.size, | |
"size " + f.size + " must be a power of 2!") | |
f.size match { | |
case 0 => DenseVector() | |
case 1 => f | |
case n => { | |
val even = fft(f(0 to f.size - 2 by 2)) | |
val odd = fft(f(1 to f.size - 1 by 2)) | |
val w = DenseVector((0 to n / 2 - 1). | |
map(k => (-2 * math.Pi * Complex.i * k / n).exp).toArray) | |
DenseVector.vertcat(even + (odd :* w), even - (odd :* w)) | |
} | |
} | |
} | |
def ifft(f: DenseVector[Complex]): DenseVector[Complex] = { | |
fft(f.map(_.conjugate)).map(_.conjugate) :/ Complex(f.size, 0) | |
} | |
util.Properties.setProp("scala.time","") | |
// テストデータ | |
val test = DenseVector( | |
Complex(1, 0), | |
Complex(1, 0), | |
Complex(1, 0), | |
Complex(1, 0), | |
Complex(0, 0), | |
Complex(0, 0), | |
Complex(0, 0), | |
Complex(0, 0)) | |
fft(test).foreach(println(_)) | |
println | |
ifft(fft(test)).foreach(println(_)) | |
println | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4.0 + 0.0i | |
1.0 + -2.414213562373095i | |
0.0 + 0.0i | |
1.0 + -0.4142135623730949i | |
0.0 + 0.0i | |
0.9999999999999999 + 0.4142135623730949i | |
0.0 + 0.0i | |
0.9999999999999997 + 2.414213562373095i | |
1.0 + -0.0i | |
1.0 + -5.551115123125783E-17i | |
1.0 + 2.4894981252573997E-17i | |
1.0 + -5.551115123125783E-17i | |
5.551115123125783E-17 + 0.0i | |
5.551115123125783E-17 + 5.551115123125783E-17i | |
0.0 + -2.4894981252573997E-17i | |
5.551115123125783E-17 + 5.551115123125783E-17i | |
[total 327ms] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment