Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years ago.
Have FFT example for DISCO-D746NG?
I would like to see spectrum analysis on LCD screen. Thank you very much.
Question relating to:
1 Answer
5 years ago.
To calculate the FFT:
Import the library mbed-dsp
#include "mbed.h" #include "arm_math.h" const unsigned int fftSamples = 1024; // number of samples to run the FFT on. Must be a power of 2 const unsigned int sampleRate = 1000; // sample rate of data in Hz const float FFTBinSize = ((float)sampleRate)/fftSamples; // resolution of the FFT in Hz float sampleBuffer[fftSamples]; // buffer for storing measurements float outputBuffer[fftSamples]; // buffer for storing FFT results data void doFFT(float *dataIn, float *results) { memset(results,sizeof(float)*fftSamples,0); arm_rfft_fast_instance_f32 S; arm_rfft_fast_init_f32(&S, fftSamples); arm_rfft_fast_f32(&S, dataIn, results,0); arm_cmplx_mag_f32(results,results,fftSamples/2); } main () { // you need to define this function to collect the data for the FFT collectData(sampleBuffer); doFFT(sampleBuffer, outputBuffer); // print a table of the results pc.printf("Frequency,Signal\r\n"); for (int i=0;i<fftSamples/2; i++) { pc.printf("%f,%f\r\n",FFTBinSize*i + FFTBinSize/2,outputBuffer[i]); } }
Note that after the doFFT() function the top half of the results array doesn't contain any useful data and should be ignored.
Since I've never used the DISCO-D746NG you'll have to figure out how to plot this yourself, there are probably some graph plotting examples around.
You may want to only start plotting from the 3rd or 4th result onward, the first few results are normally massively larger than the rest due to the DC component of any input. Printing out a text list to start with would give you a good idea of this so you can work out how best to handle the plotting scale.