rtl433  UNKNOWN
RTL-433 utility
pulse_detect.c File Reference

Pulse detection functions. More...

Data Structures

struct  pulse_FSK_state_t
 Internal state data for pulse_FSK_detect() More...
 
struct  pulse_detect
 Internal state data for pulse_pulse_package() More...
 
struct  hist_bin_t
 Histogram data for single bin. More...
 
struct  histogram_t
 Histogram data for all bins. More...
 

Functions

void pulse_data_clear (pulse_data_t *data)
 Clear the content of a pulse_data_t structure. More...
 
void pulse_data_print (pulse_data_t const *data)
 Print the content of a pulse_data_t structure (for debug). More...
 
static void * bounded_memset (void *b, int c, int64_t size, int64_t offset, int64_t len)
 
void pulse_data_dump_raw (uint8_t *buf, unsigned len, uint64_t buf_offset, pulse_data_t const *data, uint8_t bits)
 Dump the content of a pulse_data_t structure as raw binary. More...
 
void pulse_data_print_vcd_header (FILE *file, uint32_t sample_rate)
 Print a header for the VCD format. More...
 
void pulse_data_print_vcd (FILE *file, pulse_data_t const *data, int ch_id)
 Print the content of a pulse_data_t structure in VCD format. More...
 
void pulse_data_load (FILE *file, pulse_data_t *data)
 Read the next pulse_data_t structure from OOK text. More...
 
void pulse_data_print_pulse_header (FILE *file)
 Print a header for the OOK text format. More...
 
void pulse_data_dump (FILE *file, pulse_data_t *data)
 Print the content of a pulse_data_t structure as OOK text. More...
 
void pulse_FSK_detect (int16_t fm_n, pulse_data_t *fsk_pulses, pulse_FSK_state_t *s)
 Demodulate Frequency Shift Keying (FSK) sample by sample. More...
 
void pulse_FSK_wrap_up (pulse_data_t *fsk_pulses, pulse_FSK_state_t *s)
 Wrap up FSK modulation and store last data at End Of Package. More...
 
pulse_detect_tpulse_detect_create ()
 
void pulse_detect_free (pulse_detect_t *pulse_detect)
 
int pulse_detect_package (pulse_detect_t *pulse_detect, int16_t const *envelope_data, int16_t const *fm_data, int len, int16_t level_limit, uint32_t samp_rate, uint64_t sample_offset, pulse_data_t *pulses, pulse_data_t *fsk_pulses)
 Demodulate On/Off Keying (OOK) and Frequency Shift Keying (FSK) from an envelope signal. More...
 
void histogram_sum (histogram_t *hist, int const *data, unsigned len, float tolerance)
 Generate a histogram (unsorted) More...
 
void histogram_delete_bin (histogram_t *hist, unsigned index)
 Delete bin from histogram. More...
 
void histogram_swap_bins (histogram_t *hist, unsigned index1, unsigned index2)
 Swap two bins in histogram. More...
 
void histogram_sort_mean (histogram_t *hist)
 Sort histogram with mean value (order lowest to highest) More...
 
void histogram_sort_count (histogram_t *hist)
 Sort histogram with count value (order lowest to highest) More...
 
void histogram_fuse_bins (histogram_t *hist, float tolerance)
 Fuse histogram bins with means within tolerance. More...
 
void histogram_print (histogram_t const *hist, uint32_t samp_rate)
 Print a histogram. More...
 
void pulse_analyzer (pulse_data_t *data, int package_type)
 Analyze the statistics of a pulse data structure and print result. More...
 

Detailed Description

Pulse detection functions.

Copyright (C) 2015 Tommy Vestermark

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

Function Documentation

◆ bounded_memset()

static void* bounded_memset ( void *  b,
int  c,
int64_t  size,
int64_t  offset,
int64_t  len 
)
static

Referenced by pulse_data_dump_raw().

◆ histogram_delete_bin()

void histogram_delete_bin ( histogram_t hist,
unsigned  index 
)

Delete bin from histogram.

References histogram_t::bins, and histogram_t::bins_count.

Referenced by histogram_fuse_bins(), and pulse_analyzer().

◆ histogram_fuse_bins()

void histogram_fuse_bins ( histogram_t hist,
float  tolerance 
)

◆ histogram_print()

void histogram_print ( histogram_t const *  hist,
uint32_t  samp_rate 
)

◆ histogram_sort_count()

void histogram_sort_count ( histogram_t hist)

Sort histogram with count value (order lowest to highest)

References histogram_t::bins, histogram_t::bins_count, hist_bin_t::count, and histogram_swap_bins().

Referenced by pulse_analyzer().

◆ histogram_sort_mean()

void histogram_sort_mean ( histogram_t hist)

Sort histogram with mean value (order lowest to highest)

References histogram_t::bins, histogram_t::bins_count, histogram_swap_bins(), and hist_bin_t::mean.

Referenced by pulse_analyzer().

◆ histogram_sum()

void histogram_sum ( histogram_t hist,
int const *  data,
unsigned  len,
float  tolerance 
)

◆ histogram_swap_bins()

void histogram_swap_bins ( histogram_t hist,
unsigned  index1,
unsigned  index2 
)

Swap two bins in histogram.

References histogram_t::bins.

Referenced by histogram_sort_count(), and histogram_sort_mean().

◆ pulse_analyzer()

◆ pulse_data_clear()

void pulse_data_clear ( pulse_data_t data)

Clear the content of a pulse_data_t structure.

Referenced by pulse_data_load(), and pulse_detect_package().

◆ pulse_data_dump()

void pulse_data_dump ( FILE *  file,
pulse_data_t data 
)

Print the content of a pulse_data_t structure as OOK text.

References pulse_data::freq1_hz, pulse_data::freq2_hz, pulse_data::fsk_f2_est, pulse_data::gap, pulse_data::num_pulses, pulse_data::pulse, and pulse_data::sample_rate.

Referenced by sdr_callback().

◆ pulse_data_dump_raw()

void pulse_data_dump_raw ( uint8_t buf,
unsigned  len,
uint64_t  buf_offset,
pulse_data_t const *  data,
uint8_t  bits 
)

Dump the content of a pulse_data_t structure as raw binary.

References bounded_memset(), pulse_data::gap, pulse_data::num_pulses, pulse_data::offset, and pulse_data::pulse.

Referenced by sdr_callback().

◆ pulse_data_load()

void pulse_data_load ( FILE *  file,
pulse_data_t data 
)

Read the next pulse_data_t structure from OOK text.

References pulse_data::freq1_hz, pulse_data::freq2_hz, pulse_data::gap, pulse_data::num_pulses, pulse_data::pulse, pulse_data_clear(), and pulse_data::sample_rate.

Referenced by main().

◆ pulse_data_print()

void pulse_data_print ( pulse_data_t const *  data)

Print the content of a pulse_data_t structure (for debug).

References pulse_data::gap, pulse_data::num_pulses, and pulse_data::pulse.

Referenced by sdr_callback().

◆ pulse_data_print_pulse_header()

void pulse_data_print_pulse_header ( FILE *  file)

Print a header for the OOK text format.

References format_time_str().

Referenced by add_dumper().

◆ pulse_data_print_vcd()

void pulse_data_print_vcd ( FILE *  file,
pulse_data_t const *  data,
int  ch_id 
)

Print the content of a pulse_data_t structure in VCD format.

References pulse_data::gap, pulse_data::num_pulses, pulse_data::offset, pulse_data::pulse, and pulse_data::sample_rate.

Referenced by sdr_callback().

◆ pulse_data_print_vcd_header()

void pulse_data_print_vcd_header ( FILE *  file,
uint32_t  sample_rate 
)

Print a header for the VCD format.

References format_time_str(), and nice_freq().

Referenced by add_dumper().

◆ pulse_detect_create()

pulse_detect_t* pulse_detect_create ( void  )

Referenced by main().

◆ pulse_detect_free()

void pulse_detect_free ( pulse_detect_t pulse_detect)

Referenced by r_free_cfg().

◆ pulse_detect_package()

int pulse_detect_package ( pulse_detect_t pulse_detect,
int16_t const *  envelope_data,
int16_t const *  fm_data,
int  len,
int16_t  level_limit,
uint32_t  samp_rate,
uint64_t  sample_offset,
pulse_data_t pulses,
pulse_data_t fsk_pulses 
)

Demodulate On/Off Keying (OOK) and Frequency Shift Keying (FSK) from an envelope signal.

Function is stateful and can be called with chunks of input data.

Parameters
envelope_dataSamples with amplitude envelope of carrier
fm_dataSamples with frequency offset from center frequency
lenNumber of samples in input buffers
samp_rateSample rate in samples per second
[in,out]pulsesWill return a pulse_data_t structure
[in,out]fsk_pulsesWill return a pulse_data_t structure for FSK demodulated data
Returns
0 if all input sample data is processed
1 if OOK package is detected (but all sample data is still not completely processed)
2 if FSK package is detected (but all sample data is still not completely processed)

References pulse_detect::data_counter, pulse_data::end_ago, pulse_FSK_state_t::fm_f1_est, pulse_FSK_state_t::fm_f2_est, pulse_data::fsk_f1_est, pulse_data::fsk_f2_est, pulse_detect::FSK_state, pulse_data::gap, pulse_detect::lead_in_counter, pulse_detect::max_pulse, pulse_data::num_pulses, pulse_data::offset, pulse_data::ook_high_estimate, pulse_detect::ook_high_estimate, pulse_data::ook_low_estimate, pulse_detect::ook_low_estimate, pulse_detect::ook_state, pulse_data::pulse, pulse_data_clear(), PULSE_DATA_FSK, PULSE_DATA_OOK, pulse_FSK_detect(), pulse_FSK_wrap_up(), pulse_detect::pulse_length, pulse_data::sample_rate, and pulse_data::start_ago.

Referenced by sdr_callback().

◆ pulse_FSK_detect()

void pulse_FSK_detect ( int16_t  fm_n,
pulse_data_t fsk_pulses,
pulse_FSK_state_t s 
)

Demodulate Frequency Shift Keying (FSK) sample by sample.

Function is stateful between calls Builds estimate for initial frequency. When frequency deviates more than a threshold value it will determine whether the deviation is positive or negative to classify it as a pulse or gap. It will then transition to other state (F1 or F2) and build an estimate of the other frequency. It will then transition back and forth when current frequency is closer to other frequency estimate. Includes spurious suppression by coalescing pulses when pulse/gap widths are too short. Pulses equal higher frequency (F1) and Gaps equal lower frequency (F2)

Parameters
fm_nOne single sample of FM data
*fsk_pulsesWill return a pulse_data_t structure for FSK demodulated data
*sInternal state

References pulse_FSK_state_t::fm_f1_est, pulse_FSK_state_t::fm_f2_est, pulse_FSK_state_t::fsk_pulse_length, pulse_FSK_state_t::fsk_state, pulse_data::gap, pulse_data::num_pulses, and pulse_data::pulse.

Referenced by pulse_detect_package().

◆ pulse_FSK_wrap_up()

void pulse_FSK_wrap_up ( pulse_data_t fsk_pulses,
pulse_FSK_state_t s 
)

Wrap up FSK modulation and store last data at End Of Package.

Parameters
fm_nOne single sample of FM data
*fsk_pulsesPulse_data_t structure for FSK demodulated data
*sInternal state

References pulse_FSK_state_t::fsk_pulse_length, pulse_FSK_state_t::fsk_state, pulse_data::gap, pulse_data::num_pulses, and pulse_data::pulse.

Referenced by pulse_detect_package().