Skip to content

Commit c83a241

Browse files
Allocate to heap, instead of stack, for peak detector filter.
This way we can remove the pxt.json config setting the stack to a larger value, which might conflict with other extensions.
1 parent abf17ca commit c83a241

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

mlrunner/mldataprocessor.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8584
MldpReturn_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

pxt.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@
4242
"mbdal"
4343
],
4444
"yotta": {
45-
"config": {
46-
"codal": {
47-
"STACK_SIZE": 8192
48-
}
49-
}
45+
"config": { }
5046
}
5147
}

0 commit comments

Comments
 (0)