Weird program behavior - CMSIS filtering on STM32F4 - Page 1
Forum section: electronics / general

author: SolarSunrise
author_rank: Regular Contributor
timestamp: August 17, 2015, 09:49:53 am
content: Hello there. I've been programming a EEG DAQ device (ADS1299) with a RN42 bluetooth radio and found a rather puzzling phenomenon. Here is the basic outline of the problematic piece of code (I've changed some of the function names to make the code more readable):this code is for calculating impedance values. What I do here is first apply high pass filter then apply a low pass filter (thus a bandpass filter) then calculate the RMS of the resulting waveform.Code:for (i = 0; i < ADS1299_CHANNELS; i++) {// Stuff for butter worth IIR high pass filterarm_biquad_cascade_df2T_f32(&biquad_HP_Struct, data_Signal_Float, output_IIR_IMPEDANCE, ADS1299_SIGNAL_WINDOW);for (j = 0; j < ADS1299_SIGNAL_WINDOW; j++) output_IIR_IMPEDANCE *= biquad_HP_Output_Gain;// Stuff for butter worth IIR low pass filter on the already high passed signal, thus bandpass-ing the signalarm_biquad_cascade_df2T_f32(&biquad_BP_Struct, output_IIR_IMPEDANCE, output_IIR, ADS1299_SIGNAL_WINDOW);sum_Impedance = 0;for (j = 0; j < ADS1299_SIGNAL_WINDOW; j++) {// Mutiply biquad SOS output by output coeff - IIR high pass filter stuffoutput_IIR *= biquad_BP_Output_Gain;// Sum squared values for RMS calculationsum_Impedance += output_IIR * output_IIR;}// Calculate RMSsum_Impedance = (float)sum_Impedance / (float)ADS1299_SIGNAL_WINDOW;arm_sqrt_f32(sum_Impedance, &rms_Impedance);rms_Impedance_Integer = rms_Impedance * 10000.0f;}}This is for sending out 250 * 8 data samples (as the sample rate is 250SPS and there are 8 channels).Code:for (j = 0; j < ADS1299_SIGNAL_WINDOW; j++) {for (i = 0; i < ADS1299_CHANNELS; i++) {print(data_Signal_Integer);print(' ');}}print('\n');}However the thing is, when I run the code in that order (filtering + calculating the impedance and then sending out data stored in a completely different array), the data I receive is corrupted, which you can see in the image below, indicated by orange arrows. But strangely, when I reverse the order and run the program, the data does not get corrupted, which is again shown below.The images show that the first one or two data samples gets consistently corrupted (spaced every second) which is obviously not a coincidence and must have to do with the program order. But how can working with a one set of array (data_Signal_Floatis used for filtering + impedance calculation whiledata_Signal_Integeris used to transmit) affect the other?Has anyone experienced this problem? Although this problem is "solved" (as I can fix it by reversing the code snippet order above) I don't know what caused the problem in the first place. Any help is appreciated!

author: AndyC_772
author_rank: Super Contributor
timestamp: August 17, 2015, 10:17:58 am
content: Memory corruption, perhaps? Are your two arrays stored adjacent in memory, so an attempt to write beyond the bounds of one actually ends up writing into the other?Is the interval between successive corrupted samples equal to the buffer size?Does your IDE support data breakpoints? Is so, set one on memory accesses that coincide with the address of the corrupt sample. That can be a very helpful technique to catch out *all* instructions that have the effect of writing to a given memory location, not just those which are meant to.

author: SolarSunrise
author_rank: Regular Contributor
timestamp: August 17, 2015, 10:30:50 am
content: Quote from: AndyC_772 on August 17, 2015, 10:17:58 amThe interval of the corrupted samples is indeed equal to the buffer size. I should've specified that in the original post. I don't think there is any problem of arrays being written beyond bounds though. However thanks for the tip on the data breakpoints, I use gdb sogdb watchshould do. Thanks.

author: mikerj
author_rank: Super Contributor
timestamp: August 17, 2015, 05:26:37 pm
content: How are all the buffers defined e.g. statically, auto variable on the stack or dynamically allocated on the heap?