Skip to content

File peak_detector.h

File List > csrc > peak_detector.h

Go to the documentation of this file

#ifndef PEAK_DETECTOR_H
#define PEAK_DETECTOR_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

enum peak_det_state {
  PEAK_DET_OK = 0,                         
  PEAK_DET_ERR_CFG_UNINITIALIZED = -200,   
  PEAK_DET_ERR_BUFFER_TOO_SMALL = -201,    
  PEAK_DET_ERR_INVALID_ARG = -202          
};

struct median_detector_levels {
  int16_t det_level;
  int16_t det_rms;
  int16_t det_energy;
};

struct median_detector_cfg {
  uint8_t num_taps;                    
  uint16_t tap_size;                   
  struct median_detector_levels levels;
};

struct detector_result {
  bool hit;       
  int peak_index; 
};

// Forward declaration for opaque state
struct detector_state;

// Functions
enum peak_det_state detector_state_size(const struct median_detector_cfg *cfg,
                                        size_t *out_size);

enum peak_det_state detector_init(void *mem, size_t mem_size,
                                  const struct median_detector_cfg *cfg,
                                  struct detector_state **out);

void detector_deinit(struct detector_state *s); // optional cleanup
void detector_reset(struct detector_state *s);  // reset execution

int detector_feed_block(struct detector_state *s, const int16_t *block,
                        int64_t block_start_offset,
                        struct detector_result *out);

int detect_recording_i16(const int16_t *samples, size_t n,
                         const struct median_detector_cfg *cfg, int *positions,
                         size_t capacity);

#ifdef PEAK_DETECTOR_TESTING
void peak_test_median_update(struct detector_state *s, uint16_t offset,
                             int16_t value, uint16_t tap_idx, uint32_t gen);
int16_t peak_test_median_value(struct detector_state *s, uint16_t offset);
uint64_t peak_test_rms_acc(const struct detector_state *s);
#endif

#endif // PEAK_DETECTOR_H