example_processing_static_presence.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2023-2025
2 // All rights reserved
3 // This file is subject to the terms and conditions defined in the file
4 // 'LICENSES/license_acconeer.txt', (BSD 3-Clause License) which is part
5 // of this source code package.
6 
7 /** \example example_processing_static_presence.c
8  * @brief example_processing_static_presence.c
9  * Example program that shows how the spread of the phase values can be used
10  * to determine if there is a static object in front of the radar.
11  *
12  * In this example we use the the ratio of the coherent average to the noncoherent
13  * average to calculate the spread and determine if the phase values are aligned or
14  * not. It is also possible to measure the spread in other way, for example by
15  * calculating the standard deviation of the phase values.
16  *
17  * Note that this example is mainly intended for detecting the precense of static
18  * objects in front of the radar sensor, while Acconeer's presence detector detects
19  * objects that moves or vibrates sligtly, ignoring static objects.
20  */
21 
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 
26 #include "acc_config.h"
29 #include "acc_integration_log.h"
30 #include "acc_rss_a121.h"
31 #include "acc_version.h"
32 
33 #include "acc_processing_helpers.h"
34 
35 #define SENSOR_ID (1U)
36 
37 #define SWEEPS_PER_FRAME 16
38 #define PHASE_SPREAD_TRESHOLD (0.01f)
39 
41 
42 int acconeer_main(int argc, char *argv[]);
43 
44 int acconeer_main(int argc, char *argv[])
45 {
46  (void)argc;
47  (void)argv;
48 
49  acc_control_helper_t control_helper_state = {0};
50 
51  printf("Acconeer software version %s\n", acc_version_get());
52 
54 
55  if (!acc_rss_hal_register(hal))
56  {
57  return EXIT_FAILURE;
58  }
59 
60  bool res = acc_control_helper_create(&control_helper_state, SENSOR_ID);
61 
62  if (!res)
63  {
64  printf("acc_control_helper_create() failed\n");
65  return EXIT_FAILURE;
66  }
67 
68  update_configuration(control_helper_state.config);
69 
70  res = acc_control_helper_activate(&control_helper_state);
71 
72  if (!res)
73  {
74  printf("acc_control_helper_activate() failed\n");
75  acc_control_helper_destroy(&control_helper_state);
76  return EXIT_FAILURE;
77  }
78 
79  uint32_t sweep_data_length = control_helper_state.proc_meta.sweep_data_length;
80  uint32_t point_vector_length = SWEEPS_PER_FRAME;
81 
82  acc_vector_float_t *phase_spread = acc_vector_float_alloc(sweep_data_length);
83  acc_vector_iq_t *point_vector = acc_vector_iq_alloc(point_vector_length);
84 
85  bool mem_ok = (phase_spread != NULL) && (point_vector != NULL);
86  if (!mem_ok)
87  {
88  printf("Memory allocation for vectors failed\n");
89  goto clean_up;
90  }
91 
92  uint32_t iterations = 5U;
93  for (uint32_t i = 0U; i < iterations; i++)
94  {
95  res = acc_control_helper_get_next(&control_helper_state);
96  if (!res)
97  {
98  printf("acc_control_helper_get_next() failed\n");
99  break;
100  }
101 
102  for (uint32_t p = 0U; p < sweep_data_length; p++)
103  {
104  acc_get_iq_point_vector(&control_helper_state, p, point_vector);
105 
106  // The ratio of the coherent average to the noncoherent average is used to determine
107  // the amount of phase spread. When the phases of the values in the array are similar,
108  // the coherent average approaches the noncoherent average. However, if the phase
109  // values are uniformly distributed around the unit circle and have the same
110  // amplitudes, they cancel out, resulting in a coherent average of 0, while the
111  // noncoherent average is still the average of the amplitudes.
112 
113  // As a result, when there is an object in front of the radar and the spread is low,
114  // the ratio is close to 1. When there is no object in front of the radar and the
115  // phase values are random, the ratio is closer to 0.
116 
117  // We set the phase spread to be 1 minus the ratio.
118 
119  float coherent_mean = acc_vector_iq_coherent_mean_amplitude(point_vector);
120  float noncoherent_mean = acc_vector_iq_noncoherent_mean_amplitude(point_vector);
121 
122  phase_spread->data[p] = 1 - coherent_mean / noncoherent_mean;
123  }
124 
125  // Print a line with a dot or star for each distance point. A star ('*') means that there
126  // is an object in front of the radar at the distance point and a dot ('.') means that
127  // there is no object in front of the radar.
128  for (uint32_t p = 0U; p < sweep_data_length; p++)
129  {
130  printf("%c", phase_spread->data[p] > PHASE_SPREAD_TRESHOLD ? '.' : '*');
131  }
132 
133  printf("\n");
134  }
135 
136 clean_up:
137  acc_vector_iq_free(point_vector);
138  acc_vector_float_free(phase_spread);
139  acc_control_helper_destroy(&control_helper_state);
140 
141  if (res && mem_ok)
142  {
143  printf("Application finished OK\n");
144  return EXIT_SUCCESS;
145  }
146  else
147  {
148  return EXIT_FAILURE;
149  }
150 }
151 
153 {
154  int32_t start_point = 100; // start at 250 mm
155  uint16_t step_length = 24; // 24*2.5 mm = 60 mm
156  uint16_t num_points = 20; // range length 20*24*2.5 mm = 1200 mm
157 
158  acc_config_start_point_set(config, start_point);
159  acc_config_num_points_set(config, num_points);
160  acc_config_step_length_set(config, step_length);
163  // The processing in this example assumes that sweeps_per_frame > 1
166 }
acc_config_start_point_set
void acc_config_start_point_set(acc_config_t *config, int32_t start_point)
Set the starting point of the sweep.
SENSOR_ID
#define SENSOR_ID
Definition: example_processing_static_presence.c:35
acc_control_helper_create
bool acc_control_helper_create(acc_control_helper_t *radar, acc_sensor_id_t sensor_id)
Create a helper instance.
Definition: acc_control_helper.c:41
acc_rss_a121.h
acc_vector_iq_coherent_mean_amplitude
float acc_vector_iq_coherent_mean_amplitude(const acc_vector_iq_t *vector_a)
Coherent mean amplitude of IQ vector.
Definition: acc_processing_helpers.c:316
acc_get_iq_point_vector
void acc_get_iq_point_vector(const acc_control_helper_t *control_helper_state, uint32_t point, acc_vector_iq_t *vector_out)
Extract an IQ vector with sweep data for a specific point from a newly captured IQ frame with multipl...
Definition: acc_processing_helpers.c:152
acc_vector_float_free
void acc_vector_float_free(acc_vector_float_t *vector)
Free storage of data elements in a float vector.
Definition: acc_processing_helpers.c:85
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_processing_static_presence.c:44
acc_vector_iq_free
void acc_vector_iq_free(acc_vector_iq_t *vector)
Free storage of data elements in an IQ vector.
Definition: acc_processing_helpers.c:73
acc_control_helper_t::config
acc_config_t * config
Definition: acc_control_helper.h:25
PHASE_SPREAD_TRESHOLD
#define PHASE_SPREAD_TRESHOLD
Definition: example_processing_static_presence.c:38
acc_version.h
acc_config_profile_set
void acc_config_profile_set(acc_config_t *config, acc_config_profile_t profile)
Set a profile.
acc_config_sweeps_per_frame_set
void acc_config_sweeps_per_frame_set(acc_config_t *config, uint16_t sweeps)
Set sweeps per frame.
acc_vector_float_t::data
float * data
Definition: acc_processing_helpers.h:36
acc_control_helper_destroy
void acc_control_helper_destroy(acc_control_helper_t *radar)
Destroy a helper instance.
Definition: acc_control_helper.c:50
config
cargo_config_t config
Definition: i2c_example_cargo.c:34
acc_hal_rss_integration_get_implementation
const acc_hal_a121_t * acc_hal_rss_integration_get_implementation(void)
Get hal implementation reference.
Definition: acc_hal_integration_stm32cube_xm.c:152
acc_vector_iq_t
Definition: acc_processing_helpers.h:27
acc_hal_a121_t
Definition: acc_hal_definitions_a121.h:82
acc_control_helper_t::proc_meta
acc_processing_metadata_t proc_meta
Definition: acc_control_helper.h:32
acc_rss_hal_register
bool acc_rss_hal_register(const acc_hal_a121_t *hal)
Register an integration.
acc_config_hwaas_set
void acc_config_hwaas_set(acc_config_t *config, uint16_t hwaas)
Set the hardware accelerated average samples (HWAAS)
acc_processing_metadata_t::sweep_data_length
uint16_t sweep_data_length
Definition: acc_processing.h:41
update_configuration
static void update_configuration(acc_config_t *config)
Definition: example_processing_static_presence.c:152
acc_hal_integration_a121.h
acc_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
acc_config_t
struct acc_config acc_config_t
Definition: acc_config.h:24
printf
#define printf
Definition: printf.h:60
acc_config_step_length_set
void acc_config_step_length_set(acc_config_t *config, uint16_t step_length)
Set the step length in a sweep.
acc_config_num_points_set
void acc_config_num_points_set(acc_config_t *config, uint16_t num_points)
Set the number of data points to measure.
acc_hal_definitions_a121.h
acc_processing_helpers.h
acc_vector_float_t
Definition: acc_processing_helpers.h:33
acc_integration_log.h
acc_vector_iq_alloc
acc_vector_iq_t * acc_vector_iq_alloc(uint32_t data_length)
Allocate storage for an IQ vector.
Definition: acc_processing_helpers.c:27
acc_vector_float_alloc
acc_vector_float_t * acc_vector_float_alloc(uint32_t data_length)
Allocate storage for a float vector.
Definition: acc_processing_helpers.c:50
SWEEPS_PER_FRAME
#define SWEEPS_PER_FRAME
Definition: example_processing_static_presence.c:37
acc_control_helper_t
Definition: acc_control_helper.h:23
acc_vector_iq_noncoherent_mean_amplitude
float acc_vector_iq_noncoherent_mean_amplitude(const acc_vector_iq_t *vector_a)
Non-coherent mean amplitude of IQ vector.
Definition: acc_processing_helpers.c:331
acc_control_helper_activate
bool acc_control_helper_activate(acc_control_helper_t *radar)
Activate the sensor.
Definition: acc_control_helper.c:81
acc_config_prf_set
void acc_config_prf_set(acc_config_t *config, acc_config_prf_t prf)
Set Pulse Repetition Frequency.
ACC_CONFIG_PRF_13_0_MHZ
@ ACC_CONFIG_PRF_13_0_MHZ
Definition: acc_definitions_a121.h:118
acc_config.h
acc_control_helper_get_next
bool acc_control_helper_get_next(acc_control_helper_t *radar)
Perform a radar measurement and wait for the result.
Definition: acc_control_helper.c:157
ACC_CONFIG_PROFILE_3
@ ACC_CONFIG_PROFILE_3
Definition: acc_definitions_a121.h:54