WaveBeans / wavebeans

Audio Processing. On scale.
https://wavebeans.io
Apache License 2.0
23 stars 0 forks source link

Ifft is not computed correctly #133

Open alexandrucaraus opened 3 months ago

alexandrucaraus commented 3 months ago

The inverse fft is not computed correctly

fun fft1() {
    // Example data
    val magnitudeFrame = doubleArrayOf(1.0, 2.0, 3.0, 4.0)
    val phaseCumulative = doubleArrayOf(0.1, 0.2, 0.3, 0.4)

    val inputFrame = magnitudeFrame.zip(phaseCumulative) { mag, phase -> mag * ComplexNumber(.0, phase).exp() }.asSequence()

    println(inputFrame.toList())

    val output = fft(x = inputFrame, n = 4, inversed = true).map { it.re }
    println("Out ${output.toList()}")
}

[0.9950041652780258+0.09983341664682815i, 1.9601331556824833+0.39733866159012243i, 2.866009467376818+0.8865606199840186i, 3.6842439760115404+1.557673369234602i] Out [2.3763476910872168, -0.757835002435818, -0.44584087475979484, -0.1776676486135782]

fun ff2() {
    val magnitudeFrame = doubleArrayOf(1.0, 2.0, 3.0, 4.0)
    val phaseCumulative = doubleArrayOf(0.1, 0.2, 0.3, 0.4)

    val inputFrame = magnitudeFrame.zip(phaseCumulative) { mag, phase -> Complex(.0, phase).exp().multiply(mag) }.toTypedArray()

    println(inputFrame.toList())

    val transformer = FastFourierTransformer(DftNormalization.STANDARD)

    val output = transformer.transform(inputFrame, TransformType.INVERSE).map { it.real }

    println("Out ${output.toList()}")

}

[(0.9950041652780258, 0.09983341664682815), (1.9601331556824833, 0.39733866159012243), (2.866009467376818, 0.8865606199840186), (3.6842439760115404, 1.557673369234602)] Out [2.3763476910872168, -0.1776676486135782, -0.44584087475979495, -0.757835002435818]

fun main () {
    fft1()
    ff2()
}

fft1 is computed using wavebeans

ff2 is computed using implementation("org.apache.commons:commons-math3:3.6.1")