不韋 呂 / 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 SpectrumDisplay.cpp Source File

SpectrumDisplay.cpp

00001 //-------------------------------------------------------
00002 // Class for display spectrum
00003 // Copyright (c) 2015 MIKAMI, Naoki,  2015/10/27
00004 //-------------------------------------------------------
00005 
00006 #include "SpectrumDisplay.hpp"
00007 
00008 namespace Mikami
00009 {
00010     SpectrumDisplay::SpectrumDisplay(
00011             LCD_DISCO_F746NG &lcd,
00012             int nFft, int x0, int y0,
00013             float db1, int bin, float maxDb, int fs)
00014         : N_FFT_(nFft), X0_(x0), Y0_(y0),
00015           DB1_(db1), BIN_(bin), MAX_DB_(maxDb), FS_(fs),
00016           LCD_(&lcd)
00017     {
00018         AxisX();
00019         AxisY();
00020     }
00021 
00022     void SpectrumDisplay::BarChart(float db[], uint32_t backColor)
00023     {
00024         const float OFFSET = 60.0f;
00025         for (int n=1; n<=N_FFT_/2; n++)
00026         {
00027             float h = ((db[n] + OFFSET) >= 0)? db[n] + OFFSET : 0;
00028             
00029             int y = Y0_ - (int)(h*DB1_);
00030             LCD_->SetTextColor(LCD_COLOR_CYAN);
00031             LCD_->DrawLine(X0_+n, Y0_-1, X0_+n, y);
00032             LCD_->SetTextColor(backColor);
00033             LCD_->DrawLine(X0_+n, y-1, X0_+n, Y0_-(int)(MAX_DB_*DB1_));
00034         }
00035         LCD_->SetTextColor(LCD_COLOR_YELLOW);
00036         LCD_->DrawLine(X0_, Y0_, X0_+BIN_*N_FFT_/2, Y0_);
00037     }
00038 
00039     void SpectrumDisplay::LineChart(float db[], uint32_t backColor)
00040     {
00041         const float OFFSET = 60.0f;
00042         LCD_->SetTextColor(backColor);
00043         LCD_->FillRect(X0_+1, Y0_-(int)(MAX_DB_*DB1_),
00044                        N_FFT_*BIN_/2, MAX_DB_*DB1_);
00045 
00046         float h = ((db[1] + OFFSET) > 0)? db[1] + OFFSET : 0;
00047         int y1 = Y0_ - (int)(h*DB1_);
00048         for (int n=1; n<N_FFT_/2; n++)
00049         {
00050             float h2 = ((db[n+1] + OFFSET) > 0)? db[n+1] + OFFSET : 0;
00051             if (h2 > MAX_DB_) h2 = MAX_DB_;
00052             int y2 = Y0_ - (int)(h2*DB1_);
00053             LCD_->SetTextColor(LCD_COLOR_CYAN);
00054             LCD_->DrawLine(X0_+n, y1, X0_+n+1, y2);
00055             y1 = y2;
00056         }
00057         LCD_->SetTextColor(LCD_COLOR_YELLOW);
00058         LCD_->DrawLine(X0_, Y0_, X0_+BIN_*N_FFT_/2, Y0_);
00059     }
00060 
00061     // Clear Spectrum
00062     void SpectrumDisplay::Clear(uint32_t backColor)
00063     {
00064         LCD_->SetTextColor(backColor);
00065         LCD_->FillRect(X0_+1, Y0_-(int)(MAX_DB_*DB1_),
00066                        N_FFT_*BIN_/2, MAX_DB_*DB1_);
00067     }
00068 
00069     // x-axis
00070     void SpectrumDisplay::AxisX()
00071     {
00072         LCD_->SetFont(&Font12);
00073 
00074         LCD_->SetTextColor(LCD_COLOR_YELLOW);
00075         LCD_->DrawLine(X0_, Y0_, X0_+BIN_*N_FFT_/2, Y0_);
00076         float dx = BIN_*(N_FFT_*1000.0f)/(float)FS_;
00077         for (int n=0; n<=(FS_/1000)/2; n++)
00078         {
00079             int xTick = X0_ + (int)(dx*n + 0.5f);
00080             char s[5];
00081             sprintf(s, "%d", n);
00082             LCD_->DisplayStringAt(xTick-3, Y0_+10, (uint8_t *)s, LEFT_MODE);
00083             LCD_->DrawLine(xTick, Y0_, xTick, Y0_+5);
00084         }
00085         LCD_->DisplayStringAt(X0_+74, Y0_+24, (uint8_t *)"Frequency [kHz]", LEFT_MODE);
00086     }
00087 
00088     // y-axis
00089     void SpectrumDisplay::AxisY()
00090     {
00091         LCD_->SetFont(&Font12);
00092         
00093         LCD_->SetTextColor(LCD_COLOR_YELLOW);
00094         LCD_->DrawLine(X0_, Y0_+5, X0_, Y0_-(int)(MAX_DB_*DB1_));
00095         for (int n=0; n<=(int)MAX_DB_; n+=20)
00096         {
00097             int yTick = Y0_-(int)(DB1_*n);
00098             char s[5];
00099             sprintf(s, "%3d", n-60);
00100             LCD_->DisplayStringAt(X0_-30, yTick-5, (uint8_t *)s, LEFT_MODE);
00101             LCD_->DrawLine(X0_-5, yTick, X0_, yTick);
00102         }
00103         LCD_->DisplayStringAt(X0_-27, Y0_-(int)(DB1_*MAX_DB_)-18, (uint8_t *)"[dB]", LEFT_MODE);
00104     }
00105 }
00106