1 | // Algo from Detlef _. (detlef_a) 19.05.2024 20:53
|
2 | // https://www.mikrocontroller.net/topic/567716#7668204
|
3 |
|
4 | //typedef float median_t;
|
5 | //typedef int32_t index_t;
|
6 |
|
7 | typedef int16_t median_t;
|
8 | typedef uint8_t index_t;
|
9 |
|
10 | #define NN (11)
|
11 |
|
12 | int cmpfunc (const void* a, const void* b) {
|
13 |
|
14 | return ( *(median_t*)a - *(median_t*)b );
|
15 |
|
16 | }
|
17 |
|
18 | median_t median(median_t neu)
|
19 |
|
20 | {
|
21 | static index_t idx = 0;
|
22 | static median_t timebased[NN];
|
23 | static median_t sorted[NN];
|
24 |
|
25 | timebased[idx++] = neu;
|
26 | idx %= NN;
|
27 | memcpy(sorted, timebased, sizeof(sorted));
|
28 | qsort(sorted, NN, sizeof(median_t), cmpfunc);
|
29 | return (sorted[(NN - 1) / 2]);
|
30 | }
|
31 |
|
32 |
|
33 | void setup()
|
34 | {
|
35 | Serial.begin(115200);
|
36 | }
|
37 |
|
38 | void loop()
|
39 | {
|
40 | uint32_t ts;
|
41 | median_t x;
|
42 | median_t y;
|
43 |
|
44 | x = random(-32768,32767);
|
45 |
|
46 | ts = micros();
|
47 | y = median(x);
|
48 | uint32_t ende = micros();
|
49 |
|
50 | Serial.println("value" + String(x) + " time used [us]: " + String(ende - ts));
|
51 | delay(10);
|
52 |
|
53 | }
|