-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
265 additions
and
204 deletions.
There are no files selected for viewing
204 changes: 0 additions & 204 deletions
204
NoiseCapture/src/commonMain/kotlin/org/noise_planet/noisecapture/signal/FFT.kt
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
NoiseCapture/src/commonMain/kotlin/org/noise_planet/noisecapture/signal/fft.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.noise_planet.noisecapture.signal | ||
|
||
import kotlin.math.PI | ||
import kotlin.math.cos | ||
import kotlin.math.pow | ||
import kotlin.math.sin | ||
import kotlin.math.log2 | ||
|
||
|
||
fun fft(length: Int, riArray: DoubleArray) { | ||
val m = log2(length.toDouble()).toInt() | ||
val n = (2.0.pow(m) + 0.5).toInt() | ||
require(n <= riArray.size) | ||
var wCos: Double | ||
var wSin: Double | ||
var uCos: Double | ||
var uSin: Double | ||
val n2 = n shr 1 | ||
var l1 = n | ||
var l2: Int | ||
|
||
for (l in 0 until m) { | ||
l2 = l1 shr 1 | ||
wCos = 1.0 | ||
wSin = 0.0 | ||
uCos = cos(PI / l2) | ||
uSin = -sin(PI / l2) | ||
|
||
for (j in 0 until l2) { | ||
for (i in j until n step l1) { | ||
val ir = 2 * i | ||
val ii = ir + 1 | ||
val l22 = 2 * l2 | ||
val sumRe = riArray[ir] + riArray[ir + l22] | ||
val sumIm = riArray[ii] + riArray[ii + l22] | ||
val diffRe = riArray[ir] - riArray[ir + l22] | ||
val diffIm = riArray[ii] - riArray[ii + l22] | ||
riArray[ir + l22] = diffRe * wCos - diffIm * wSin | ||
riArray[ii + l22] = diffRe * wSin + diffIm * wCos | ||
riArray[ir] = sumRe | ||
riArray[ii] = sumIm | ||
} | ||
val w = wCos * uCos - wSin * uSin | ||
wSin = wCos * uSin + wSin * uCos | ||
wCos = w | ||
} | ||
l1 = l1 shr 1 | ||
} | ||
|
||
var k: Int | ||
var j = 0 | ||
val n1 = n - 1 | ||
|
||
for (i in 0 until n1) { | ||
if (i < j) { | ||
val jr = 2 * j | ||
val ji = jr + 1 | ||
val ir = 2 * i | ||
val ii = ir + 1 | ||
val tre = riArray[jr] | ||
val tim = riArray[ji] | ||
riArray[jr] = riArray[ir] | ||
riArray[ji] = riArray[ii] | ||
riArray[ir] = tre | ||
riArray[ii] = tim | ||
} | ||
k = n2 | ||
while (k <= j) { | ||
j -= k | ||
k = k shr 1 | ||
} | ||
j += k | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
NoiseCapture/src/commonMain/kotlin/org/noise_planet/noisecapture/signal/ifft.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.noise_planet.noisecapture.signal | ||
|
||
import kotlin.math.PI | ||
import kotlin.math.cos | ||
import kotlin.math.log2 | ||
import kotlin.math.pow | ||
import kotlin.math.sin | ||
|
||
fun iFFT(length: Int, riArray: DoubleArray) { | ||
val m = log2(length.toDouble()).toInt() | ||
val n = (2.0.pow(m) + 0.5).toInt() | ||
require(n <= riArray.size) | ||
var wCos: Double | ||
var wSin: Double | ||
var uCos: Double | ||
var uSin: Double | ||
val n2 = n shr 1 | ||
var l1 = n | ||
var l2: Int | ||
|
||
for (l in 0 until m) { | ||
l2 = l1 shr 1 | ||
wCos = 1.0 | ||
wSin = 0.0 | ||
uCos = cos(PI / l2) | ||
uSin = sin(PI / l2) | ||
|
||
for (j in 0 until l2) { | ||
for (i in j until n step l1) { | ||
val ir = 2 * i | ||
val ii = ir + 1 | ||
val l22 = 2 * l2 | ||
val sumRe = 0.5 * (riArray[ir] + riArray[ir + l22]) | ||
val sumIm = 0.5 * (riArray[ii] + riArray[ii + l22]) | ||
val diffRe = 0.5 * (riArray[ir] - riArray[ir + l22]) | ||
val diffIm = 0.5 * (riArray[ii] - riArray[ii + l22]) | ||
riArray[ir + l22] = diffRe * wCos - diffIm * wSin | ||
riArray[ii + l22] = diffRe * wSin + diffIm * wCos | ||
riArray[ir] = sumRe | ||
riArray[ii] = sumIm | ||
} | ||
val w = wCos * uCos - wSin * uSin | ||
wSin = wCos * uSin + wSin * uCos | ||
wCos = w | ||
} | ||
l1 = l1 shr 1 | ||
} | ||
|
||
var k: Int | ||
var j = 0 | ||
val n1 = n - 1 | ||
|
||
for (i in 0 until n1) { | ||
if (i < j) { | ||
val jr = 2 * j | ||
val ji = jr + 1 | ||
val ir = 2 * i | ||
val ii = ir + 1 | ||
val tre = riArray[jr] | ||
val tim = riArray[ji] | ||
riArray[jr] = riArray[ir] | ||
riArray[ji] = riArray[ii] | ||
riArray[ir] = tre | ||
riArray[ii] = tim | ||
} | ||
k = n2 | ||
while (k <= j) { | ||
j -= k | ||
k = k shr 1 | ||
} | ||
j += k | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
NoiseCapture/src/commonMain/kotlin/org/noise_planet/noisecapture/signal/realfft.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.noise_planet.noisecapture.signal | ||
|
||
import kotlin.math.PI | ||
import kotlin.math.cos | ||
import kotlin.math.log2 | ||
import kotlin.math.pow | ||
import kotlin.math.sin | ||
|
||
fun realFFT(length: Int, realArray: DoubleArray) { | ||
val m = log2(length.toDouble()).toInt() | ||
val n = (2.0.pow(m) + 0.5).toInt() | ||
require(n <= realArray.size) | ||
fft(n/2, realArray) | ||
val a = DoubleArray(n) | ||
val b = DoubleArray(n) | ||
|
||
for (i in 0 until n / 2) { | ||
a[2 * i] = 0.5 * (1 - sin(2 * PI / n * i)) | ||
a[2 * i + 1] = -0.5 * cos(2 * PI / n * i) | ||
b[2 * i] = 0.5 * (1 + sin(2 * PI / n * i)) | ||
b[2 * i + 1] = 0.5 * cos(2 * PI / n * i) | ||
} | ||
|
||
for (k in 1 until n / 4 + 1) { | ||
val k2 = 2 * k | ||
val xr = | ||
realArray[k2] * a[k2] - realArray[k2 + 1] * a[k2 + 1] + realArray[n - k2] * b[k2] + realArray[n - k2 + 1] * b[k2 + 1] | ||
val xi = | ||
realArray[k2] * a[k2 + 1] + realArray[k2 + 1] * a[k2] + realArray[n - k2] * b[k2 + 1] - realArray[n - k2 + 1] * b[k2] | ||
val xrN = | ||
realArray[n - k2] * a[n - k2] - realArray[n - k2 + 1] * a[n - k2 + 1] + realArray[k2] * b[n - k2] + realArray[k2 + 1] * b[n - k2 + 1] | ||
val xiN = | ||
realArray[n - k2] * a[n - k2 + 1] + realArray[n - k2 + 1] * a[n - k2] + realArray[k2] * b[n - k2 + 1] - realArray[k2 + 1] * b[n - k2] | ||
realArray[k2] = xr | ||
realArray[k2 + 1] = xi | ||
realArray[n - k2] = xrN | ||
realArray[n - k2 + 1] = xiN | ||
} | ||
} | ||
|
Oops, something went wrong.