![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Real-time spectrum analyzer for ST Nucleo F401RE using Seeed Studio 2.8'' TFT Touch Shield V2.0.
Dependencies: SeeedStudioTFTv2 UITDSP_ADDA UIT_FFT_Real mbed
SpactrumAnalysisClasses/FFT_Analysis.cpp@0:c5b026c2d07e, 2015-07-26 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Sun Jul 26 02:48:23 2015 +0000
- Revision:
- 0:c5b026c2d07e
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:c5b026c2d07e | 1 | //------------------------------------------------------- |
MikamiUitOpen | 0:c5b026c2d07e | 2 | // Class for spectrum analysis using FFT |
MikamiUitOpen | 0:c5b026c2d07e | 3 | // Copyright (c) 2014 MIKAMI, Naoki, 2014/12/30 |
MikamiUitOpen | 0:c5b026c2d07e | 4 | //------------------------------------------------------- |
MikamiUitOpen | 0:c5b026c2d07e | 5 | |
MikamiUitOpen | 0:c5b026c2d07e | 6 | #include "FFT_Analysis.hpp" |
MikamiUitOpen | 0:c5b026c2d07e | 7 | |
MikamiUitOpen | 0:c5b026c2d07e | 8 | namespace Mikami |
MikamiUitOpen | 0:c5b026c2d07e | 9 | { |
MikamiUitOpen | 0:c5b026c2d07e | 10 | FftAnalyzer::FftAnalyzer(int nData, int nFft) |
MikamiUitOpen | 0:c5b026c2d07e | 11 | : N_DATA_(nData), N_FFT_(nFft), |
MikamiUitOpen | 0:c5b026c2d07e | 12 | hm_(nData-1, nFft), fft_(nFft) |
MikamiUitOpen | 0:c5b026c2d07e | 13 | { |
MikamiUitOpen | 0:c5b026c2d07e | 14 | pkHolder_ = new PeakHolder[nFft/2+1]; |
MikamiUitOpen | 0:c5b026c2d07e | 15 | for (int n=0; n<=nFft/2; n++) |
MikamiUitOpen | 0:c5b026c2d07e | 16 | pkHolder_[n].SetCoefs(0.2f); |
MikamiUitOpen | 0:c5b026c2d07e | 17 | |
MikamiUitOpen | 0:c5b026c2d07e | 18 | xData = new float[nData]; // Data to be analyzed |
MikamiUitOpen | 0:c5b026c2d07e | 19 | xFft = new float[nFft]; // Input for FFT |
MikamiUitOpen | 0:c5b026c2d07e | 20 | yFft = new Complex[nFft/2+1]; // Output of FFT |
MikamiUitOpen | 0:c5b026c2d07e | 21 | normY = new float[nFft/2+1]; // Powerspectrum |
MikamiUitOpen | 0:c5b026c2d07e | 22 | } |
MikamiUitOpen | 0:c5b026c2d07e | 23 | |
MikamiUitOpen | 0:c5b026c2d07e | 24 | FftAnalyzer::~FftAnalyzer() |
MikamiUitOpen | 0:c5b026c2d07e | 25 | { |
MikamiUitOpen | 0:c5b026c2d07e | 26 | delete[] pkHolder_; |
MikamiUitOpen | 0:c5b026c2d07e | 27 | delete[] xData; |
MikamiUitOpen | 0:c5b026c2d07e | 28 | delete[] xFft; |
MikamiUitOpen | 0:c5b026c2d07e | 29 | delete[] yFft; |
MikamiUitOpen | 0:c5b026c2d07e | 30 | delete[] normY; |
MikamiUitOpen | 0:c5b026c2d07e | 31 | } |
MikamiUitOpen | 0:c5b026c2d07e | 32 | |
MikamiUitOpen | 0:c5b026c2d07e | 33 | void FftAnalyzer::Execute(float xn[], float db[]) |
MikamiUitOpen | 0:c5b026c2d07e | 34 | { |
MikamiUitOpen | 0:c5b026c2d07e | 35 | // Differencing |
MikamiUitOpen | 0:c5b026c2d07e | 36 | for (int n=0; n<N_DATA_-1; n++) |
MikamiUitOpen | 0:c5b026c2d07e | 37 | xData[n] = xn[n+1] - xn[n]; |
MikamiUitOpen | 0:c5b026c2d07e | 38 | |
MikamiUitOpen | 0:c5b026c2d07e | 39 | hm_.Execute(xData, xFft); // Windowing and zero-padding |
MikamiUitOpen | 0:c5b026c2d07e | 40 | fft_.Execute(xFft, yFft); // Execute FFT |
MikamiUitOpen | 0:c5b026c2d07e | 41 | |
MikamiUitOpen | 0:c5b026c2d07e | 42 | // Smoothing |
MikamiUitOpen | 0:c5b026c2d07e | 43 | for (int n=0; n<=N_FFT_/2; n++) |
MikamiUitOpen | 0:c5b026c2d07e | 44 | normY[n] = pkHolder_[n].Execute(Sqr(yFft[n].real()) |
MikamiUitOpen | 0:c5b026c2d07e | 45 | + Sqr(yFft[n].imag())); |
MikamiUitOpen | 0:c5b026c2d07e | 46 | |
MikamiUitOpen | 0:c5b026c2d07e | 47 | // Translate to dB |
MikamiUitOpen | 0:c5b026c2d07e | 48 | for (int n=0; n<=N_FFT_/2; n++) |
MikamiUitOpen | 0:c5b026c2d07e | 49 | db[n] = 10.0f*log10f(normY[n]); |
MikamiUitOpen | 0:c5b026c2d07e | 50 | } |
MikamiUitOpen | 0:c5b026c2d07e | 51 | } |