Skip to content

Instantly share code, notes, and snippets.

@asubb
asubb / compressHash.kt
Last active January 22, 2020 15:35
Hash values are in fact very long number written as hex number. For using it as string it is sometimes waste of space (i.e. as identifier or something). That function kinda compresses the string by using more digits for this number. How many digits? It's up to you.
import kotlin.math.max
import kotlin.random.Random
fun compressHash(hexV: String, digits: String = "0123456789abcdefghijklmnopqrstuvwxyz"): String {
val hexDigits = "0123456789abcdef"
val zero = hexDigits.first()
fun dropLeadingZeros(hexV: String): String {
var v = hexV
@asubb
asubb / wave-blog:using-jupyter.Dockerfile
Last active May 20, 2020 03:16
wave-blog/using-jupyter/dockerfile
FROM jupyter/base-notebook
USER root
# Install OpenJDK-8
RUN apt-get update && \
apt-get install -y openjdk-8-jdk && \
apt-get install -y ant && \
apt-get clean;
@asubb
asubb / wave-blog:using-jupyter:imports.kt
Created April 10, 2020 20:31
wave-blog/using-jupyter/imports.kt
%use lets-plot
@file:Repository("https://dl.bintray.com/wavebeans/wavebeans")
@file:DependsOn("io.wavebeans:lib:0.0.3")
import io.wavebeans.lib.*
import io.wavebeans.lib.io.*
import io.wavebeans.lib.math.*
import io.wavebeans.lib.stream.*
import io.wavebeans.lib.stream.fft.*
@asubb
asubb / wave-blog:using-jupyter:short.signal.kt
Created April 10, 2020 21:01
wave-blog/using-jupyter/short.signal.kt
val o = (880.sine() + 440.sine() + 220.sine())
.trim(1000, MILLISECONDS)
val values = o.asSequence(44100.0f)
.drop(500)
.take(400)
.map { it.asDouble() }
.toList()
val values2 = o.asSequence(44100.0f)
@asubb
asubb / wave-blog:using-jupiter:long.imports.kt
Created April 10, 2020 21:12
wave-blog/using-jupiter/long.imports.kt
%use lets-plot
@file:Repository("https://dl.bintray.com/wavebeans/wavebeans")
@file:DependsOn("io.wavebeans:lib:0.0.3")
@file:DependsOn("io.wavebeans:exe:0.0.3")
@file:DependsOn("io.wavebeans:http:0.0.3")
import io.wavebeans.lib.*
import io.wavebeans.lib.io.*
import io.wavebeans.lib.math.*
@asubb
asubb / wave-blog:using-jupyter:long.signal.kt
Created April 10, 2020 21:17
wave-blog/using-jupyter/long.signal.kt
//Specify the parameters of future calculations:
val windowSize = 801
val stepSize = 256
val fftSize = 1024
// Define a signal, windowed sum of sinusoids:
val signal = (880.sine() + 440.sine() + 220.sine())
.window(windowSize, stepSize)
// The first FFT applying the triangular window function:
@asubb
asubb / wave-blog:using-jupyter:long.signal.exe.kt
Created April 10, 2020 21:19
wave-blog/using-jupyter/long.signal.exe.kt
val overseer = LocalDistributedOverseer(
listOf(triangularFft, hammingFft),
threadsCount = 2,
partitionsCount = 2
)
val errors = overseer.eval(44100.0f)
.map { it.get().exception }
.filterNotNull()
.toList()
@asubb
asubb / wave-blog:using-jupyter:long.signal.query.imports.kt
Created April 10, 2020 21:21
wave-blog/using-jupyter/long.signal.query.imports.kt
%use klaxon
%use lets-plot
import java.net.*
import jetbrains.letsPlot.scale.*
@asubb
asubb / wave-blog:using-jupyter:long.signal.query.types.kt
Created April 10, 2020 21:23
wave-blog/using-jupyter/long.signal.query.types.kt
data class FftSample(
val index: Long,
val binCount: Int,
val samplesCount: Int,
val sampleRate: Float,
val magnitude: List<Double>,
val phase: List<Double>,
val frequency: List<Double>,
val time: Long
)
@asubb
asubb / wave-blog:using-jupyter:long.signal.query.func.kt
Created April 10, 2020 21:27
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