// Algo from Detlef _. (detlef_a) 19.05.2024 20:53 // https://www.mikrocontroller.net/topic/567716#7668204 //typedef float median_t; //typedef int32_t index_t; typedef int16_t median_t; typedef uint8_t index_t; #define NN (11) int cmpfunc (const void* a, const void* b) { return ( *(median_t*)a - *(median_t*)b ); } median_t median(median_t neu) { static index_t idx = 0; static median_t timebased[NN]; static median_t sorted[NN]; timebased[idx++] = neu; idx %= NN; memcpy(sorted, timebased, sizeof(sorted)); qsort(sorted, NN, sizeof(median_t), cmpfunc); return (sorted[(NN - 1) / 2]); } void setup() { Serial.begin(115200); } void loop() { uint32_t ts; median_t x; median_t y; x = random(-32768,32767); ts = micros(); y = median(x); uint32_t ende = micros(); Serial.println("value" + String(x) + " time used [us]: " + String(ende - ts)); delay(10); }