@@ -81,7 +81,6 @@ MldpReturn_t filterStdDev(const float *data_in, const int in_size, float *data_o
8181// Count the number of peaks
8282// Warning! This can allocate 5x the in_size of the data in the stack
8383// so ensure DEVICE_STACK_SIZE is appropriately set
84- // TODO: Move to the heap, pxt automatically uses its allocator
8584MldpReturn_t filterPeaks (const float * data_in , const int in_size , float * data_out , const int out_size ) {
8685 const int lag = 5 ;
8786 const float threshold = 3.5 ;
@@ -91,11 +90,13 @@ MldpReturn_t filterPeaks(const float *data_in, const int in_size, float *data_ou
9190 return MLDP_ERROR_CONFIG ;
9291 }
9392
94- float signals [in_size ];
95- float filtered_y [in_size ];
93+ // micro:bit allocator panics if there is not enough memory, no need to check
94+ float * signals = (float * )malloc (in_size * sizeof (float ));
95+ float * filtered_y = (float * )malloc (in_size * sizeof (float ));
96+ float * avg_filter = (float * )malloc (in_size * sizeof (float ));
97+ float * std_filter = (float * )malloc (in_size * sizeof (float ));
9698 float lead_in [lag ];
97- float avg_filter [in_size ];
98- float std_filter [in_size ];
99+
99100 memset (signals , 0 , in_size * sizeof (float ));
100101 memcpy (filtered_y , data_in , in_size * sizeof (float ));
101102 memcpy (lead_in , data_in , lag * sizeof (float ));
@@ -140,6 +141,11 @@ MldpReturn_t filterPeaks(const float *data_in, const int in_size, float *data_ou
140141 }
141142 * data_out = peaksCounter ;
142143
144+ free (signals );
145+ free (filtered_y );
146+ free (avg_filter );
147+ free (std_filter );
148+
143149 return MLDP_SUCCESS ;
144150}
145151
0 commit comments