No photo version of "F746_SpectralAnalysis_Example".
Dependencies: BSP_DISCO_F746NG BUTTON_GROUP LCD_DISCO_F746NG TS_DISCO_F746NG UIT_FFT_Real mbed
Diff: SpactrumAnalysisClasses/FFT_Analysis.cpp
- Revision:
- 0:9d7f931c704a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SpactrumAnalysisClasses/FFT_Analysis.cpp Tue Nov 24 12:34:55 2015 +0000 @@ -0,0 +1,53 @@ +//------------------------------------------------------- +// Class for spectrum analysis using FFT +// Copyright (c) 2015 MIKAMI, Naoki, 2015/10/26 +//------------------------------------------------------- + +#include "FFT_Analysis.hpp" + +namespace Mikami +{ + FftAnalyzer::FftAnalyzer(int nData, int nFft) + : N_DATA_(nData), N_FFT_(nFft), + hm_(nData-1, nFft), fft_(nFft) + { + xData = new float[nData]; // Data to be analyzed + xFft = new float[nFft]; // Input for FFT + yFft = new Complex[nFft/2+1]; // Output of FFT + normY = new float[nFft/2+1]; // Powerspectrum + } + + FftAnalyzer::~FftAnalyzer() + { + delete[] xData; + delete[] xFft; + delete[] yFft; + delete[] normY; + } + + void FftAnalyzer::Execute(float xn[], float db[]) + { + // Differencing + for (int n=0; n<N_DATA_-1; n++) + xData[n] = xn[n+1] - xn[n]; + + hm_.Execute(xData, xFft); // Windowing and zero-padding + fft_.Execute(xFft, yFft); // Execute FFT + + // Squared magnitude + for (int n=0; n<=N_FFT_/2; n++) + normY[n] = Sqr(yFft[n].real()) + Sqr(yFft[n].imag()); + + // Search maximum + float max = 0; + for (int n=0; n<=N_FFT_/2; n++) + max = (max > normY[n]) ? max : normY[n]; + float invMax = 1.0f/max; + + // Translate to dB + for (int n=0; n<=N_FFT_/2; n++) + db[n] = 10.0f*log10f(invMax*normY[n]); + } +} + +