Редизайн на основе текущей ветки мейнстрима + новые устройства.
[rtl-433.git] / include / pulse_detect.h
1 /**
2  * Pulse detection functions
3  *
4  * Copyright (C) 2015 Tommy Vestermark
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10
11 #ifndef INCLUDE_PULSE_DETECT_H_
12 #define INCLUDE_PULSE_DETECT_H_
13
14 #include <stdint.h>
15
16 #define PD_MAX_PULSES 1200                      // Maximum number of pulses before forcing End Of Package
17 #define PD_MIN_PULSES 16                        // Minimum number of pulses before declaring a proper package
18 #define PD_MIN_PULSE_SAMPLES 10         // Minimum number of samples in a pulse for proper detection
19 #define PD_MIN_GAP_MS 10                        // Minimum gap size in milliseconds to exceed to declare End Of Package
20 #define PD_MAX_GAP_MS 100                       // Maximum gap size in milliseconds to exceed to declare End Of Package
21 #define PD_MAX_GAP_RATIO 10                     // Ratio gap/pulse width to exceed to declare End Of Package (heuristic)
22 #define PD_MAX_PULSE_MS 100                     // Pulse width in ms to exceed to declare End Of Package (e.g. for non OOK packages)
23
24 /// Data for a compact representation of generic pulse train
25 typedef struct {
26         unsigned int num_pulses;
27         int pulse[PD_MAX_PULSES];       // Contains width of a pulse    (high)
28         int gap[PD_MAX_PULSES];         // Width of gaps between pulses (low)
29         int ook_low_estimate;           // Estimate for the OOK low level (base noise level) at beginning of package
30         int ook_high_estimate;          // Estimate for the OOK high level at end of package
31         int fsk_f1_est;                         // Estimate for the F1 frequency for FSK
32         int fsk_f2_est;                         // Estimate for the F2 frequency for FSK
33 } pulse_data_t;
34
35
36 /// Clear the content of a pulse_data_t structure
37 void pulse_data_clear(pulse_data_t *data);              // Clear the struct
38
39 /// Print the content of a pulse_data_t structure (for debug)
40 void pulse_data_print(const pulse_data_t *data);
41
42
43 /// Demodulate On/Off Keying (OOK) and Frequency Shift Keying (FSK) from an envelope signal
44 ///
45 /// Function is stateful and can be called with chunks of input data
46 /// @param envelope_data: Samples with amplitude envelope of carrier 
47 /// @param fm_data: Samples with frequency offset from center frequency
48 /// @param len: Number of samples in input buffers
49 /// @param samp_rate: Sample rate in samples per second
50 /// @param *pulses: Will return a pulse_data_t structure
51 /// @param *fsk_pulses: Will return a pulse_data_t structure for FSK demodulated data
52 /// @return 0 if all input sample data is processed
53 /// @return 1 if OOK package is detected (but all sample data is still not completely processed)
54 /// @return 2 if FSK package is detected (but all sample data is still not completely processed)
55 int pulse_detect_package(const int16_t *envelope_data, const int16_t *fm_data, int len, int16_t level_limit, uint32_t samp_rate, pulse_data_t *pulses, pulse_data_t *fsk_pulses);
56
57
58 /// Analyze and print result
59 void pulse_analyzer(pulse_data_t *data, uint32_t samp_rate);
60
61
62 #endif /* INCLUDE_PULSE_DETECT_H_ */