1 /** @file 2 Pulse detection functions. 3 4 Copyright (C) 2015 Tommy Vestermark 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 */ 11 12 #ifndef INCLUDE_PULSE_DETECT_H_ 13 #define INCLUDE_PULSE_DETECT_H_ 14 15 #include <stdint.h> 16 #include <stdio.h> 17 #include "data.h" 18 19 #define PD_MAX_PULSES 1200 // Maximum number of pulses before forcing End Of Package 20 #define PD_MIN_PULSES 16 // Minimum number of pulses before declaring a proper package 21 #define PD_MIN_PULSE_SAMPLES 10 // Minimum number of samples in a pulse for proper detection 22 #define PD_MIN_GAP_MS 10 // Minimum gap size in milliseconds to exceed to declare End Of Package 23 #define PD_MAX_GAP_MS 100 // Maximum gap size in milliseconds to exceed to declare End Of Package 24 #define PD_MAX_GAP_RATIO 10 // Ratio gap/pulse width to exceed to declare End Of Package (heuristic) 25 #define PD_MAX_PULSE_MS 100 // Pulse width in ms to exceed to declare End Of Package (e.g. for non OOK packages) 26 27 /// Data for a compact representation of generic pulse train. 28 typedef struct pulse_data { 29 uint64_t offset; ///< Offset to first pulse in number of samples from start of stream. 30 uint32_t sample_rate; ///< Sample rate the pulses are recorded with. 31 unsigned depth_bits; ///< Sample depth in bits. 32 unsigned start_ago; ///< Start of first pulse in number of samples ago. 33 unsigned end_ago; ///< End of last pulse in number of samples ago. 34 unsigned int num_pulses; 35 int pulse[PD_MAX_PULSES]; ///< Width of pulses (high) in number of samples. 36 int gap[PD_MAX_PULSES]; ///< Width of gaps between pulses (low) in number of samples. 37 int ook_low_estimate; ///< Estimate for the OOK low level (base noise level) at beginning of package. 38 int ook_high_estimate; ///< Estimate for the OOK high level at end of package. 39 int fsk_f1_est; ///< Estimate for the F1 frequency for FSK. 40 int fsk_f2_est; ///< Estimate for the F2 frequency for FSK. 41 float freq1_hz; 42 float freq2_hz; 43 float centerfreq_hz; 44 float range_db; 45 float rssi_db; 46 float snr_db; 47 float noise_db; 48 } pulse_data_t; 49 50 // Package types 51 enum package_types { 52 PULSE_DATA_OOK = 1, 53 PULSE_DATA_FSK = 2, 54 }; 55 56 /// state data for pulse_FSK_detect() 57 typedef struct { 58 unsigned int fsk_pulse_length; ///< Counter for internal FSK pulse detection 59 enum { 60 PD_FSK_STATE_INIT = 0, ///< Initial frequency estimation 61 PD_FSK_STATE_FH = 1, ///< High frequency (pulse) 62 PD_FSK_STATE_FL = 2, ///< Low frequency (gap) 63 PD_FSK_STATE_ERROR = 3 ///< Error - stay here until cleared 64 } fsk_state; 65 66 int fm_f1_est; ///< Estimate for the F1 frequency for FSK 67 int fm_f2_est; ///< Estimate for the F2 frequency for FSK 68 69 int16_t var_test_max; 70 int16_t var_test_min; 71 int16_t maxx; 72 int16_t minn; 73 int16_t midd; 74 int skip_samples; 75 } pulse_FSK_state_t; 76 77 typedef struct pulse_detect pulse_detect_t; 78 79 /// Clear the content of a pulse_data_t structure. 80 void pulse_data_clear(pulse_data_t *data); 81 82 /// Shift out part of the data to make room for more. 83 void pulse_data_shift(pulse_data_t *data); 84 85 /// Print the content of a pulse_data_t structure (for debug). 86 void pulse_data_print(pulse_data_t const *data); 87 88 /// Dump the content of a pulse_data_t structure as raw binary. 89 void pulse_data_dump_raw(uint8_t *buf, unsigned len, uint64_t buf_offset, pulse_data_t const *data, uint8_t bits); 90 91 /// Print a header for the VCD format. 92 void pulse_data_print_vcd_header(FILE *file, uint32_t sample_rate); 93 94 /// Print the content of a pulse_data_t structure in VCD format. 95 void pulse_data_print_vcd(FILE *file, pulse_data_t const *data, int ch_id); 96 97 /// Read the next pulse_data_t structure from OOK text. 98 void pulse_data_load(FILE *file, pulse_data_t *data, uint32_t sample_rate); 99 100 /// Print a header for the OOK text format. 101 void pulse_data_print_pulse_header(FILE *file); 102 103 /// Print the content of a pulse_data_t structure as OOK text. 104 void pulse_data_dump(FILE *file, pulse_data_t *data); 105 106 /// Print the content of a pulse_data_t structure as OOK json. 107 data_t *pulse_data_print_data(pulse_data_t *data); 108 109 pulse_detect_t *pulse_detect_create(void); 110 111 void pulse_detect_free(pulse_detect_t *pulse_detect); 112 113 /// Set pulse detector level values. 114 /// 115 /// @param pulse_detect The pulse_detect instance 116 /// @param use_mag_est Use magnitude instead of amplitude 117 /// @param fixed_high_level Manual high level override, default is 0 (auto) 118 /// @param min_high_level Minimum high level, default is -12 dB 119 /// @param high_low_ratio Minimum signal noise ratio, default is 9 dB 120 /// @param verbosity Debug output verbosity, 0=None, 1=Levels, 2=Histograms 121 void pulse_detect_set_levels(pulse_detect_t *pulse_detect, int use_mag_est, float fixed_high_level, float min_high_level, float high_low_ratio, int verbosity); 122 123 /// Demodulate On/Off Keying (OOK) and Frequency Shift Keying (FSK) from an envelope signal. 124 /// 125 /// Function is stateful and can be called with chunks of input data. 126 /// 127 /// @param pulse_detect The pulse_detect instance 128 /// @param envelope_data Samples with amplitude envelope of carrier 129 /// @param fm_data Samples with frequency offset from center frequency 130 /// @param len Number of samples in input buffers 131 /// @param samp_rate Sample rate in samples per second 132 /// @param sample_offset Offset tracking for ringbuffer 133 /// @param[in,out] pulses Will return a pulse_data_t structure 134 /// @param[in,out] fsk_pulses Will return a pulse_data_t structure for FSK demodulated data 135 /// @param fpdm Index of filter setting to use 136 /// @return if a package is detected 137 /// @retval 0 all input sample data is processed 138 /// @retval 1 OOK package is detected (but all sample data is still not completely processed) 139 /// @retval 2 FSK package is detected (but all sample data is still not completely processed) 140 int pulse_detect_package(pulse_detect_t *pulse_detect, int16_t const *envelope_data, int16_t const *fm_data, int len, uint32_t samp_rate, uint64_t sample_offset, pulse_data_t *pulses, pulse_data_t *fsk_pulses, unsigned fpdm); 141 142 143 #endif /* INCLUDE_PULSE_DETECT_H_ */ 144