简体   繁体   English

如何设计一个流,它是另一个流的每最新 5 个数据的平均值?

[英]How can I design a Flow which is a average value of every latest 5 data of another Flow?

There is a Flow which will emit data every 100ms, and I hope to get a average value of every latest 5 data of the Flow, and convert the average value as Double value into another Flow.有一个 Flow 会每 100ms 发出一次数据,我希望得到 Flow 的每最新 5 个数据的平均值,并将平均值作为 Double 值转换为另一个 Flow。

How can I design the Flow?如何设计流程?

Code A代码 A

   fun soundDbFlow(period: Long = 100) = flow {
        while (true) {
            var data = getAmplitude()
            emit(data)
            delay(period)
        }
    }
    .get_Average_Per5_LatestData {...} //How can I do? or is there other way?
    .map { soundDb(it) }

    private fun getAmplitude(): Int {
        var result = 0
        mRecorder?.let {
            result = it.maxAmplitude
        }
        return result
    }    
        
    private fun soundDb(input:Int, referenceAmp: Double = 1.0): Double {
        return 20 * Math.log10(input / referenceAmp)
    }

Added Content:添加内容:

To plplmax: Thanks!致plplmax:谢谢!

I assume Code B will emit 1,2,3,4,5,6,7,8,9,10.....我假设代码 B 将发出 1,2,3,4,5,6,7,8,9,10 .....

Do you guarantee Code C will calculate (1+2+3+4+5)/5 first, then calculate (6+7+8+9+10)/5 second, .... ?你保证代码 C 会先计算(1+2+3+4+5)/5 ,然后再计算(6+7+8+9+10)/5秒,....? It's my expectation.这是我的期望。

I worry that Code C maybe calculate (1+2+3+4+5)/5 first, the calculate (2+3+4+5+6)/5 sencond, ...我担心代码 C 可能首先计算(1+2+3+4+5)/5 ,然后计算(2+3+4+5+6)/5秒,...

Code B代码 B

suspend fun soundDbFlow(period: Long) = flow {
    while (true) {
        val data = getAmplitude()
        emit(data)
        delay(period)
    }
}

Code C代码 C

private fun reduceFlow(period: Long = 100) = flow {
    while (true) {
        val result = soundDbFlow(period)
            .take(5)
            .map { soundDb((it / 5.0).roundToInt()) }
            .reduce { accumulator, value -> accumulator + value }
        emit(result)
    }
}

You can write a chunked operator like this:您可以像这样编写分块运算符:

/**
 * Returns a Flow that emits sequential [size]d chunks of data from the source flow, 
 * after transforming them with [transform].
 * 
 * The list passed to [transform] is transient and must not be cached.
 */
fun <T, R> Flow<T>.chunked(size: Int, transform: suspend (List<T>)-> R): Flow<R> = flow {
    val cache = ArrayList<T>(size)
    collect {
        cache.add(it)
        if (cache.size == size) {
            emit(transform(cache))
            cache.clear()
        }
    }
}

And then use it like this:然后像这样使用它:

suspend fun soundDbFlow(period: Long) = flow {
    while (true) {
        val data = getAmplitude()
        emit(data)
        delay(period)
    }
}
    .chunked(5) { (it.sum() / 5.0).roundToInt() }
    .map { soundDb(it) }

Is that what you want?那是你要的吗?

var result = 0

suspend fun soundDbFlow(period: Long) = flow {
    while (true) {
        val data = getAmplitude()
        emit(data)
        delay(period)
    }
}

private fun reduceFlow(period: Long = 100) = flow {
    while (true) {
        val result = soundDbFlow(period)
            .take(5)
            .map { soundDb((it / 5.0).roundToInt()) }
            .reduce { accumulator, value -> accumulator + value }
        emit(result)
    }
}

private fun getAmplitude(): Int {
    return ++result
}

private fun soundDb(input: Int, referenceAmp: Double = 1.0): Double {
    return 20 * log10(input / referenceAmp)
}

fun main(): Unit = runBlocking {
    reduceFlow().collect { println(it) }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM