File peak_detector.c¶
FileList > csrc > peak_detector.c
Go to the source code of this file
Median-based impulse detector implementation. More...
#include "peak_detector.h"#include <limits.h>#include <math.h>#include <stdalign.h>#include <stddef.h>#include <stdint.h>#include <stdlib.h>#include <string.h>
Classes¶
| Type | Name |
|---|---|
| struct | detector_state |
| struct | heap_node Uzly pro obousměrné haldy mediánu. |
| struct | per_offset_median |
Public Functions¶
| Type | Name |
|---|---|
| void | detector_deinit (struct detector_state * s) |
| int | detector_feed_block (struct detector_state * s, const int16_t * block, int64_t block_start_offset, struct detector_result * out) Online zpracování jednoho tapu (bloku) vzorků. |
| enum peak_det_state | detector_init (void * mem, size_t mem_size, const struct median_detector_cfg * cfg, struct detector_state ** out) Inicializuje stav v uživatelem dodaném bufferu (bez malloc). |
| void | detector_reset (struct detector_state * s) |
| enum peak_det_state | detector_state_size (const struct median_detector_cfg * cfg, size_t * out_size) Vrátí potřebnou velikost paměťového bloku pro stav podle konfigurace. |
Public Static Functions¶
| Type | Name |
|---|---|
| size_t | align_up (size_t v, size_t a) |
| int | cmp_int16 (const void * a, const void * b) |
| void | heap_clean_top (struct per_offset_median * m, bool is_max_heap, struct heap_node * heap, size_t * size) |
| int | heap_cmp_max (const struct heap_node * a, const struct heap_node * b) |
| int | heap_cmp_min (const struct heap_node * a, const struct heap_node * b) |
| void | heap_compact (struct heap_node * heap, size_t * size, const uint32_t * gen_per_tap, bool is_max_heap) |
| void | heap_heapify (struct heap_node * heap, size_t size, bool is_max_heap) |
| bool | heap_is_stale (const struct heap_node * n, const uint32_t * gen_per_tap) |
| struct heap_node | heap_pop (struct heap_node * heap, size_t * size, bool is_max_heap) |
| void | heap_push (struct heap_node * heap, size_t * size, struct heap_node node, bool is_max_heap) |
| void | heap_sift_down (struct heap_node * heap, size_t size, size_t idx, bool is_max_heap) |
| void | heap_sift_up (struct heap_node * heap, size_t idx, bool is_max_heap) |
| struct heap_node * | heap_top (struct heap_node * heap, size_t size) |
| void | layout_state (void * mem_base, const struct median_detector_cfg * cfg, struct detector_state ** state_out) |
| void | median_insert (struct per_offset_median * m, int16_t value, uint16_t tap_idx, uint32_t gen) |
| int16_t | median_of_slice (const int16_t * arr, size_t len) |
| void | median_rebalance (struct per_offset_median * m) |
| void | median_update_offset (struct per_offset_median * m, int16_t new_value, uint16_t tap_idx, uint32_t gen) |
| int16_t | median_value (struct per_offset_median * m, int16_t fallback) |
Macros¶
| Type | Name |
|---|---|
| define | MEDIAN_SLICE_MAX_LEN 128 |
Detailed Description¶
Algoritmus:
* Data přicházejí po tap blocích (o délce tap_size), které se ukládají do kruhového bufferu s num_taps položkami.
* Pro každý offset v tapu se udržují dvě haldy (max/min) s lazy invalidací přes gen_per_tap, takže median lze aktualizovat v O(log N) bez mazání starých uzlů.
* RMS akumulátor drží sumu čtverců pro celé okno, což umožňuje dynamický práh det_rms * RMS.
* Vyhodnocení probíhá nad "middle" tapem: hledá se největší deviation vůči medianu šumu a pak se kontroluje energie before/after.
Hlavní cíle: determinismus (žádné malloc), jednoduchá portace na embedded a auditovatelnost algoritmu.
Public Functions Documentation¶
function detector_deinit¶
function detector_feed_block¶
Online zpracování jednoho tapu (bloku) vzorků.
int detector_feed_block (
struct detector_state * s,
const int16_t * block,
int64_t block_start_offset,
struct detector_result * out
)
Postup: * Aktualizuje per-offset mediány s lazy invalidací starých generací. * Udržuje RMS akumulátor přes celé okno. * Jakmile je okno naplněné, vyhodnotí deviation v "middle" tapu a provede energetický test before/after.
Parameters:
sInterní stav.blockVstupní vzorky délky tap_size.block_start_offsetLogický offset signálu (pro zpětné určení polohy).outVýsledek detekce (může být NULL, pak se ignoruje).
Returns:
PEAK_DET_OK nebo chybový kód.
function detector_init¶
Inicializuje stav v uživatelem dodaném bufferu (bez malloc).
enum peak_det_state detector_init (
void * mem,
size_t mem_size,
const struct median_detector_cfg * cfg,
struct detector_state ** out
)
Inicializace nastaví kruhové buffery na nulu, zapíše parametry z cfg a připraví struktury pro výpočet mediánu (max/min heap per offset) a RMS akumulátor pro celé okno.
Parameters:
memUkazatel na předalokovaný buffer.mem_sizeVelikost bufferu v bajtech.cfgKonfigurace detektoru.outVýstup: ukazatel na inicializovaný stav uvnitř bufferu.
Returns:
PEAK_DET_OK nebo chybový kód.
function detector_reset¶
function detector_state_size¶
Vrátí potřebnou velikost paměťového bloku pro stav podle konfigurace.
enum peak_det_state detector_state_size (
const struct median_detector_cfg * cfg,
size_t * out_size
)
Funkce nealokuje paměť – pouze spočítá minimální velikost bufferu, který musí volající poskytnout při detector_init.
Parameters:
cfgKonfigurace detektoru (nesmí být NULL).out_sizeVýstup: požadovaná velikost v bajtech, zarovnaná pro interní struktury.
Returns:
PEAK_DET_OK nebo chybový kód.
Public Static Functions Documentation¶
function align_up¶
function cmp_int16¶
function heap_clean_top¶
static void heap_clean_top (
struct per_offset_median * m,
bool is_max_heap,
struct heap_node * heap,
size_t * size
)
function heap_cmp_max¶
function heap_cmp_min¶
function heap_compact¶
static void heap_compact (
struct heap_node * heap,
size_t * size,
const uint32_t * gen_per_tap,
bool is_max_heap
)
function heap_heapify¶
function heap_is_stale¶
function heap_pop¶
function heap_push¶
static void heap_push (
struct heap_node * heap,
size_t * size,
struct heap_node node,
bool is_max_heap
)
function heap_sift_down¶
function heap_sift_up¶
function heap_top¶
function layout_state¶
static void layout_state (
void * mem_base,
const struct median_detector_cfg * cfg,
struct detector_state ** state_out
)
function median_insert¶
static void median_insert (
struct per_offset_median * m,
int16_t value,
uint16_t tap_idx,
uint32_t gen
)
function median_of_slice¶
function median_rebalance¶
function median_update_offset¶
static void median_update_offset (
struct per_offset_median * m,
int16_t new_value,
uint16_t tap_idx,
uint32_t gen
)
function median_value¶
Macro Definition Documentation¶
define MEDIAN_SLICE_MAX_LEN¶
The documentation for this class was generated from the following file scripts/median-filter/csrc/peak_detector.c