# Problem with FFT Functions in FreeRTOS

**URL:** <https://forums.freertos.org/t/problem-with-fft-functions-in-freertos/6865>\
**Category:** Kernel\
**Created:** [November 30, 2017, 9:22am UTC](https://forums.freertos.org/t/problem-with-fft-functions-in-freertos/6865 "2017-11-30T09:22:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 30, 2017, 9:22am UTC](https://forums.freertos.org/t/problem-with-fft-functions-in-freertos/6865/1 "2017-11-30T09:22:08Z")

</div>

**[moh-ferdows](https://sourceforge.net/u/moh-ferdows/)** wrote on [Thursday, November 30, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/58d84e92/#b25b):

Hello,  
I’m running freeRTOS on STM32F4. I have no problem with using cmsis-dsp functions without freeRTOS.  
But when I want to call a function (in which cmsis-dsp functions like arm\_cfft\_radix4\_f3,arm\_rfft\_f32 ,… are called) from a task(thread):  
1.If I pass local arrays (defined in that task) as fft input and output ,output becomes zero!!  
2.If I pass global arrays, I have non-zero(correct) output, but using global variables in freeRTOS tasks can cause some problems as we know.  
3.If I pass global arrays as fft input and output but send output to another task using queues I receive zero values there!!  
I don’t know if there is problem with freeRTOS variables that is defined in its heap and cmsis-dsp functions defined out of freeRTOS heap.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 30, 2017, 2:05pm UTC](https://forums.freertos.org/t/problem-with-fft-functions-in-freertos/6865/2 "2017-11-30T14:05:00Z")

</div>

**[kenchang1](https://sourceforge.net/u/kenchang1/)** wrote on [Thursday, November 30, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/58d84e92/#b996):

Are you using the FPU (floating point calculations)?  
Try this define  
`#define configUSE_TASK_FPU_SUPPORT 2`

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [December 1, 2017, 9:42pm UTC](https://forums.freertos.org/t/problem-with-fft-functions-in-freertos/6865/3 "2017-12-01T21:42:30Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, December 01, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/58d84e92/#b996/fd74):

I don’t think configUSE\_TASK\_FPU\_SUPPORT has any effect for the STM32.

Do you have stack overflow checking turned on?  
[https://www.freertos.org/Stacks-and-stack-overflow-checking.html](https://www.freertos.org/Stacks-and-stack-overflow-checking.html)

How are you passing the data via a queue? Do you just queue a pointer  
to the data? That might not work if the array is on the stack but the  
tack frame no longer exists. I presume you are talking about the task  
stack, not the stack of main(), as the main() stack will also no longer  
exist.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 2, 2017, 6:23am UTC](https://forums.freertos.org/t/problem-with-fft-functions-in-freertos/6865/4 "2017-12-02T06:23:31Z")

</div>

**[moh-ferdows](https://sourceforge.net/u/moh-ferdows/)** wrote on [Saturday, December 02, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/58d84e92/#b996/fd74/dda7):

Stack overflow method 1 is turned on. Heap size for my task is sufficeint and no overflow is occurred. I pass data via queue using pointer to an array defined in task stack. Note that I use cmsis-rtos functions so instead of xQueueSend or xQueueReceive I have osMailPut and osMailGet but they are defined based on freeRTOS functions as you know.  
Here is my code:  
void waveformFFT(float32\_t \*input,float32\_t \*output,uint16\_t fftSize)  
{

arm\_cfft\_radix4\_instance\_f32 fftStruct;  
arm\_cfft\_radix4\_init\_f32(&fftStruct,fftSize,0,1);  
arm\_cfft\_radix4\_f32(&fftStruct,input);  
arm\_cmplx\_mag\_f32(input, output, fftSize);  
}  
// waveformProcess task  
void waveformProccess(void const \* argument)  
{  
/\* USER CODE BEGIN waveformProccess _/  
uint16\_t i,j,k;  
osEvent tenCycle;  
uint16\_t buffAddr,waveSample;  
uint8\_t hBuff[4480];  
uint8\_t cylceNum;  
int32\_t waveBuff[256];  
float32\_t fftRes[256];  
char uartBuff[20];  
waveformInfo waveInfo= (waveformInfo )osMailAlloc(waveToUarthandle,100);  
/ Infinite loop /  
for(;😉  
{  
tenCycle=osSignalWait(BIT\_0,200); // waits for recieveing waveform ten cycle samples.  
if(tenCycle.status == osEventSignal)  
{  
for(i=0;i\<4480;i++)  
hBuff[i]=hBuff\_t[i];  
for(i=0 ; i \<160 ; i++){  
waveBuff[160cylceNum+i]=(uint32\_t)(hBuff[28i+4] \<\< 24 ) | (uint32\_t)(hBuff[28_i+5] \<\< 16 ) | (uint32\_t)(hBuff[28_i+6] \<\< 8 ) | (uint32\_t)(hBuff[28_i+7]); //  
waveInfo-\>waveform[i]=waveBuff[i];  
}  
cylceNum++;  
// Take FFT of one cycle of waveform  
if(cylceNum==1)  
{  
osMailFree(waveToUarthandle,waveInfo);  
waveformInfo \*waveInfo= (waveformInfo \*)osMailAlloc(waveToUarthandle,100);  
for(i=160;i\<256;i++)  
{  
waveBuff[i]=0;  
waveInfo-\>waveform[i]=waveBuff[i];  
}  
waveformFFT((float32\_t _)waveBuff,fftRes,256);  
cylceNum = 0;  
for(i=0;i\<256;i++)  
{  
waveInfo-\>waveFFT[i]=fftRes[i];  
}  
osMailPut(waveToUarthandle,waveInfo); // sends fft input and output to uart task using mail.  
}  
}  
}  
/_ USER CODE END waveformProccess \*/  
}  
I have the same problem if I use cmsis-dsp function directly in the task ( instead of calling a function like waveformProccess in which cmsis-dsp functions are called)  
Please help me with this problem/  
Thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 2, 2017, 6:27am UTC](https://forums.freertos.org/t/problem-with-fft-functions-in-freertos/6865/5 "2017-12-02T06:27:32Z")

</div>

**[moh-ferdows](https://sourceforge.net/u/moh-ferdows/)** wrote on [Saturday, December 02, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/58d84e92/#b996/06cb):

Yes,I’m using FPU. configUSE\_TASK\_FPU\_SUPPORT was defined 1. I did so ( defining as 2) but still have the problem.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [December 2, 2017, 7:06am UTC](https://forums.freertos.org/t/problem-with-fft-functions-in-freertos/6865/6 "2017-12-02T07:06:01Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Saturday, December 02, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/58d84e92/#8b58):

Mahammad, when posting code, please put it between two lines that only contain three tildas `````  
In that way the text will be show literally:

````auto
```
// waveformProcess task
void waveformProccess(void const * argument)
{
    /* USER CODE BEGIN waveformProccess */
    uint16_t i,j,k;
    osEvent tenCycle;
    uint16_t buffAddr,waveSample;
    uint8_t hBuff[4480];
    uint8_t cylceNum;
    int32_t waveBuff[256];
    float32_t fftRes[256];
    char uartBuff[20];
    waveformInfo waveInfo= (waveformInfo )osMailAlloc(waveToUarthandle,100);

```

````

Could it be that you allocate about 8612 bytes on the stack?  
Does the task that runs this code have at least `8612 / 4 = 2153` words of stack space?

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [December 2, 2017, 7:27am UTC](https://forums.freertos.org/t/problem-with-fft-functions-in-freertos/6865/7 "2017-12-02T07:27:40Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Saturday, December 02, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/58d84e92/#7945):

I pressed on Post too early, wanted to ask you more:

Can you make sure that all floating point variables on stack are well-aligned?

> but using global variables in freeRTOS tasks can cause some problems as we know

I don’t know, why? It is considered as “bad practice” to use global variables, but they don’t cause problems as such.  
You can also make them static within the module, or static within the function. When you do that, the function can run with a normal-size stack.

Don’t you check the result of `osMailAlloc()`? Can it fail?

> but send output to another task using queues I receive zero values there!!

How do you send these values? Does each byte go into a FreeRTOS queue? Or do you allocate a new buffer and pass a pointer ( and the ownership ) of the buffer to the queue?
