2020#include "misc.h"
2121#include "print.h"
2222
23- enum samples {BUFFER_SIZE = 400 };
23+ enum samples {numberOfSamples = 400 };
2424struct
25- accel_axis
25+ accelAxis
2626{
2727 /*
2828 * Hold data and info about single acceleration axis:
2929 */
30- float data [BUFFER_SIZE ]; /*
30+ float data [numberOfSamples ]; /*
3131 * array to store filtered data
3232 */
3333float max ; /*
@@ -37,7 +37,7 @@ accel_axis
3737 float min ; /*
3838 * minimum data entry
3939 */
40- float p2p ; /*
40+ float peakToPeak ; /*
4141 * peak to peak value
4242 */
4343 float thresh ; /*
@@ -46,17 +46,17 @@ accel_axis
4646};
4747
4848void
49- findAxisProperties (struct accel_axis * axis )
49+ findAxisProperties (struct accelAxis * axis )
5050{
5151 /*
5252 * Find peak-to-peak value and threshold for single-axis data
5353 */
5454 float max = axis -> data [0 ];
5555 float min = axis -> data [0 ];
56- float p2p ;
56+ float peakToPeak ;
5757 float thresh ;
5858
59- for (int i = 0 ; i < BUFFER_SIZE ; i ++ )
59+ for (int i = 0 ; i < numberOfSamples ; i ++ )
6060{
6161 if (axis -> data [i ] > max )
6262{
@@ -68,55 +68,55 @@ findAxisProperties(struct accel_axis *axis)
6868 }
6969 }
7070
71- p2p = max - min ;
71+ peakToPeak = max - min ;
7272 thresh = (max + min )/2.0 ;
7373
7474 /*
7575 * Write values into struct
7676 */
7777 axis -> max = max ;
7878 axis -> min = min ;
79- axis -> p2p = p2p ;
79+ axis -> peakToPeak = peakToPeak ;
8080 axis -> thresh = thresh ;
8181}
8282
8383int
84- chooseAxis (struct accel_axis * x , struct accel_axis * y , struct accel_axis * z , float calib )
84+ chooseAxis (struct accelAxis * x , struct accelAxis * y , struct accelAxis * z , float calib )
8585{
8686 /*
8787 * Perform maximum activity axis selection
8888 */
8989
90- float p2p [3 ];
91- float max_p2p = 0 ;
92- int max_index ;
90+ float peakToPeak [3 ];
91+ float maxPeakToPeak = 0 ;
92+ int maxIndex ;
9393
9494 findAxisProperties (x );
9595 findAxisProperties (y );
9696 findAxisProperties (z );
9797
98- p2p [0 ] = x -> p2p ;
99- p2p [1 ] = y -> p2p ;
100- p2p [2 ] = z -> p2p ;
98+ peakToPeak [0 ] = x -> peakToPeak ;
99+ peakToPeak [1 ] = y -> peakToPeak ;
100+ peakToPeak [2 ] = z -> peakToPeak ;
101101
102102 /*
103103 * Find axis with greatest peak-to-peak (p2p) amplitude
104104 */
105105 for (int i = 0 ; i < 3 ; i ++ )
106106{
107- if (p2p [i ] > max_p2p )
107+ if (peakToPeak [i ] > maxPeakToPeak )
108108{
109- max_p2p = p2p [i ];
110- max_index = i ;
109+ maxPeakToPeak = peakToPeak [i ];
110+ maxIndex = i ;
111111 }
112112 }
113113
114114 /*
115115 * If p2p value of chosen axis is above amplitude calibration value then return chosen axis
116116 */
117- if (max_p2p > calib )
117+ if (maxPeakToPeak > calib )
118118{
119- return (max_index + 1 );
119+ return (maxIndex + 1 );
120120 }
121121
122122 /*
@@ -130,15 +130,15 @@ chooseAxis(struct accel_axis *x, struct accel_axis *y, struct accel_axis *z, flo
130130}
131131
132132float
133- detectSteps (struct accel_axis * chosen )
133+ detectSteps (struct accelAxis * chosen )
134134{
135135/*
136136 * Finds where threshold is crossed in negative slope direction
137137 */
138138
139139 float steps = 0 ;
140140 float current , next ;
141- for (int i = 0 ; i < BUFFER_SIZE - 1 ;i ++ )
141+ for (int i = 0 ; i < numberOfSamples - 1 ;i ++ )
142142{
143143 current = chosen -> data [i ];
144144 next = chosen -> data [i + 1 ];
@@ -151,9 +151,9 @@ detectSteps(struct accel_axis *chosen)
151151}
152152
153153void
154- readRawData (float * t_buffer , float * x_buffer , float * y_buffer , float * z_buffer )
154+ readRawData (float * tBuffer , float * xBuffer , float * yBuffer , float * zBuffer )
155155{
156- for (int j = 0 ; j < BUFFER_SIZE ; j ++ )
156+ for (int j = 0 ; j < numberOfSamples ; j ++ )
157157 {
158158/*
159159 * Insert delay of 2500 uSeconds to simulate 400 Hz sample rate
@@ -162,16 +162,17 @@ readRawData(float *t_buffer, float *x_buffer, float *y_buffer, float *z_buffer)
162162/*
163163 * Read sensor readings from sigsrc 0 which is the x-axis accelerometer readings
164164 */
165- x_buffer [j ] = devsignal_read (0 );
165+ xBuffer [j ] = devsignal_read (0 );
166166/*
167167 * Read sensor readings from sigsrc 1 which is the y-axis accelerometer readings
168168 */
169- y_buffer [j ] = devsignal_read (1 );
169+ yBuffer [j ] = devsignal_read (1 );
170170/*
171171 * Read sensor readings from sigsrc 2 which is the z-axis accelerometer readings
172172 */
173- z_buffer [j ] = devsignal_read (2 );
174- /* Please note that sigsrc simulates a real signal which changes in time.
173+ zBuffer [j ] = devsignal_read (2 );
174+ /*
175+ * Please note that sigsrc simulates a real signal which changes in time.
175176 * The value returned by devsignal_read() will be different at different simulation times.
176177 * Here we use delay to wait for 1/f seconds where f is the 400 Hz sample frequency specified in the run.m file.
177178 * If we don't wait for this amount of time and instead sample as quickly as we can we will see the same sensor value many times.
@@ -182,7 +183,7 @@ readRawData(float *t_buffer, float *x_buffer, float *y_buffer, float *z_buffer)
182183}
183184
184185void
185- MovingAvgFilter (float input [], struct accel_axis * output )
186+ MovingAvgFilter (float input [], struct accelAxis * output )
186187{
187188/*
188189 * Implement moving average low pass filtering
@@ -194,13 +195,13 @@ MovingAvgFilter(float input[], struct accel_axis *output)
194195 /*
195196 * calculate mean of input array
196197 */
197- for (int i = 0 ; i < BUFFER_SIZE ; i ++ )
198+ for (int i = 0 ; i < numberOfSamples ; i ++ )
198199{
199200 sum += input [i ];
200201 }
201- mean = sum /BUFFER_SIZE ;
202+ mean = sum /numberOfSamples ;
202203
203- for (int i = 0 ; i < BUFFER_SIZE ; i ++ )
204+ for (int i = 0 ; i < numberOfSamples ; i ++ )
204205{
205206 /*
206207 * Find mean-subtracted input
@@ -212,7 +213,7 @@ MovingAvgFilter(float input[], struct accel_axis *output)
212213 output -> data [i ] = 0 ;
213214 }
214215
215- for (int i = 0 ; i < BUFFER_SIZE ; i ++ )
216+ for (int i = 0 ; i < numberOfSamples ; i ++ )
216217{
217218 output -> data [i ] = input [i ];
218219 }
@@ -222,41 +223,35 @@ MovingAvgFilter(float input[], struct accel_axis *output)
222223int
223224main (void )
224225{
225- int chosen_axis ;
226+ int chosenAxis ;
226227float steps ;
227- float steps_total = 0 ;
228- float calib_max ;/*
229- * used in calibration stage
230- */
231- bool use_butterworth ;/*
232- * if true use Butterworth filter, else use Moving Average filter
233- */
228+ float totalSteps = 0 ;
229+
230+ /*
231+ * used in calibration stage
232+ */
233+ float calibrationMax ;
234234
235235/*
236236 * Declare buffers to hold raw unfiltered acceleration data
237237 */
238- float t_buffer [ BUFFER_SIZE ];
239- float x_buffer [ BUFFER_SIZE ];
240- float y_buffer [ BUFFER_SIZE ];
241- float z_buffer [ BUFFER_SIZE ];
238+ float tBuffer [ numberOfSamples ];
239+ float xBuffer [ numberOfSamples ];
240+ float yBuffer [ numberOfSamples ];
241+ float zBuffer [ numberOfSamples ];
242242
243243/*
244244 * Declare struct for each acceleration axis (for use after filtering)
245245 */
246- struct accel_axis x_accel ;
247- struct accel_axis y_accel ;
248- struct accel_axis z_accel ;
249-
250- /*
251- * Select type of filtering
252- */
253- use_butterworth = false;
246+ struct accelAxis xAcceleration ;
247+ struct accelAxis yAcceleration ;
248+ struct accelAxis zAcceleration ;
254249
255250/*
256251 * empirically calculated minimum allowable calibration value
257252 * in units of 0.25 mg
258253 */
259- calib_max = 1 ;
254+ calibrationMax = 1 ;
260255
261256/*
262257 * Stage 2: Step Detection
@@ -265,44 +260,44 @@ main(void)
265260 /*
266261 * Read raw tri-axial accelerometer data and store in buffers
267262 */
268- readRawData (t_buffer , x_buffer , y_buffer , z_buffer );
263+ readRawData (tBuffer , xBuffer , yBuffer , zBuffer );
269264
270- MovingAvgFilter (x_buffer , & x_accel );
271- MovingAvgFilter (y_buffer , & y_accel );
272- MovingAvgFilter (z_buffer , & z_accel );
265+ MovingAvgFilter (xBuffer , & xAcceleration );
266+ MovingAvgFilter (yBuffer , & yAcceleration );
267+ MovingAvgFilter (zBuffer , & zAcceleration );
273268
274269 /*
275270 * Peform maximal activity axis selection
276271 */
277- chosen_axis = chooseAxis (& x_accel , & y_accel , & z_accel , calib_max );
278- if (chosen_axis == 1 )
272+ chosenAxis = chooseAxis (& xAcceleration , & yAcceleration , & zAcceleration , calibrationMax );
273+ if (chosenAxis == 1 )
279274{
280- steps = detectSteps (& x_accel );
275+ steps = detectSteps (& xAcceleration );
281276 }
282277
283- else if (chosen_axis == 2 )
278+ else if (chosenAxis == 2 )
284279{
285- steps = detectSteps (& y_accel );
280+ steps = detectSteps (& yAcceleration );
286281 }
287282
288- else if (chosen_axis == 3 )
283+ else if (chosenAxis == 3 )
289284{
290- steps = detectSteps (& z_accel );
285+ steps = detectSteps (& zAcceleration );
291286 }
292287
293288 else
294289{
295290 steps = 0 ;
296291 }
297- steps_total += steps ;
292+ totalSteps += steps ;
298293
299294 /*
300295 * Reset count to 0 if it reaches 100
301296 */
302- if (steps_total > 99 )
297+ if (totalSteps > 99 )
303298{
304- steps_total = 0 ;
299+ totalSteps = 0 ;
305300 }
306- printf ("Total steps = %f\n" , steps_total );
301+ printf ("Total steps = %f\n" , totalSteps );
307302return 0 ;
308303}
0 commit comments