Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save asubb/75a1f62e2a730740bb17899dbe19bcae to your computer and use it in GitHub Desktop.
Save asubb/75a1f62e2a730740bb17899dbe19bcae to your computer and use it in GitHub Desktop.
wave-blog/using-jupyter/long.signal.query.func.kt
fun fftData(table: String, freqCutOff: Pair<Int, Int>): Map<String, List<Any>> {
val k = Klaxon()
val server = "http://localhost:6800"
// 1. read and deserializae the data
val data = URL("$server/table/$table/last?interval=10s").openStream().reader().readLines()
.map { k.parse<Result>(it)!! }
// 2. Calculate the time value to shift values to 0 by x-axis
val timeShift = data.asSequence().map { it.value.time }.min() ?: 0
// 3. remap the FftSample to list of tuples
val table = data.asSequence().map {v ->
// magnitudes and frequencies are making pairs like (10.0dB, 100hz), (20.0dB, 150Hz), etc.
v.value.magnitude.asSequence()
.zip(v.value.frequency.asSequence())
// filtering out frequencies out of the provided range
.filter { it.second >= freqCutOff.first && it.second <= freqCutOff.second}
// map values to tuples
.map { arrayOf(
(v.value.time - timeShift) / 1e+6, // to milliseconds
it.second,
it.first
)}
}
// convert list of lists to plain list
.flatten()
.toList() // this gives a list of tuples [ (time, freq, value), ... , (time, freq, value)]
// 4. remap tuples to columnar structure by index
val dataFrame = mapOf(
"time" to table.map { it[0] },
"freq" to table.map { it[1] },
"value" to table.map { it[2] }
)
return dataFrame
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment