Created
April 10, 2020 21:27
-
-
Save asubb/75a1f62e2a730740bb17899dbe19bcae to your computer and use it in GitHub Desktop.
wave-blog/using-jupyter/long.signal.query.func.kt
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
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