不韋 呂 / Mbed 2 deprecated F746_SpectralAnalysis_NoPhoto

Dependencies:   BSP_DISCO_F746NG BUTTON_GROUP LCD_DISCO_F746NG TS_DISCO_F746NG UIT_FFT_Real mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FFT_Analysis.cpp Source File

FFT_Analysis.cpp

00001 //-------------------------------------------------------
00002 // Class for spectrum analysis using FFT
00003 // Copyright (c) 2015 MIKAMI, Naoki,  2015/10/26
00004 //-------------------------------------------------------
00005 
00006 #include "FFT_Analysis.hpp"
00007 
00008 namespace Mikami
00009 {
00010     FftAnalyzer::FftAnalyzer(int nData, int nFft)
00011             : N_DATA_(nData), N_FFT_(nFft),
00012               hm_(nData-1, nFft), fft_(nFft)
00013     {
00014         xData = new float[nData];       // Data to be analyzed
00015         xFft = new float[nFft];         // Input for FFT
00016         yFft = new Complex[nFft/2+1];   // Output of FFT
00017         normY = new float[nFft/2+1];    // Powerspectrum
00018     }
00019    
00020     FftAnalyzer::~FftAnalyzer()
00021     {
00022         delete[] xData;
00023         delete[] xFft;
00024         delete[] yFft;
00025         delete[] normY;
00026     }
00027 
00028     void FftAnalyzer::Execute(float xn[], float db[])
00029     {
00030         // Differencing
00031         for (int n=0; n<N_DATA_-1; n++)
00032             xData[n] = xn[n+1] - xn[n];
00033 
00034         hm_.Execute(xData, xFft);    // Windowing and zero-padding
00035         fft_.Execute(xFft, yFft);    // Execute FFT
00036 
00037         // Squared magnitude
00038         for (int n=0; n<=N_FFT_/2; n++)
00039             normY[n] = Sqr(yFft[n].real()) + Sqr(yFft[n].imag());
00040             
00041         // Search maximum
00042         float max = 0;
00043         for (int n=0; n<=N_FFT_/2; n++)
00044             max = (max > normY[n]) ? max : normY[n];
00045         float invMax = 1.0f/max;
00046 
00047         // Translate to dB
00048         for (int n=0; n<=N_FFT_/2; n++)
00049             db[n] = 10.0f*log10f(invMax*normY[n]);
00050     }
00051 }
00052 
00053