Mladen Adamovic / Spectrogram

Dependencies:   F746_GUI F746_SAI_IO UIT_FFT_Real

Fork of F746_Spectrogram by 不韋 呂

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MethodCollection.hpp Source File

MethodCollection.hpp

00001 /********************************************************
00002  *  Global function used in spectrogram
00003  *
00004  *  Mladen Adamovic, 3326/2016
00005  *******************************************************/
00006 
00007 #ifndef METHOD_COLLECTION_HPP
00008 #define METHOD_COLLECTION_HPP
00009 
00010 #include "mbed.h"
00011 #include "NumericLabel.hpp"
00012 #include "Matrix.hpp"
00013 #include "FFT_Analysis.hpp"
00014 
00015 namespace etf
00016 {
00017     // Convert intensity to hue difference
00018     //      0.0 <= x <= 1.0
00019     uint32_t HueScale(float x)
00020     {
00021         if (x > 1) return LCD_COLOR_WHITE;
00022         int r = 0;
00023         int b = 0;
00024 
00025         if (x<0.5f) b = (x<0.33f) ? 255 : -(int)(1500.0f*x) + 750;
00026         else        r = (0.67f<x) ? 255 :  (int)(1500.0f*x) - 750;
00027         int g = 255 - (int)(1020.0f*(x - 0.5f)*(x - 0.5f));
00028 
00029         return 0xFF000000 | (((r << 8) | g) << 8) | b;
00030     }
00031     
00032     // Clear axis
00033     void ClearAxis(int x0, int y0, LCD_DISCO_F746NG &lcd)
00034     {
00035         lcd.SetTextColor(GuiBase::ENUM_BACK);
00036         lcd.FillRect(x0-30,y0-180, 430, 215); 
00037     }
00038 
00039     // Coordinate axis for 2D view
00040     void DrawAxis2D(int x0, int y0, int w0, int h0, uint32_t axisColor,
00041                   float hz100, uint16_t px20dB, LCD_DISCO_F746NG &lcd)
00042     {
00043 
00044         
00045         const uint16_t TICK = 5;    // Length of graduation line
00046         
00047         ClearAxis(x0, y0, lcd);
00048         lcd.SetTextColor(axisColor);
00049 
00050         // Transverse axis
00051         lcd.DrawHLine(x0, y0+TICK, w0);
00052         for (int n=0; n<=60; n++)
00053             if ((n % 10)== 0) lcd.DrawVLine(x0+n*hz100, y0, 5);
00054             else              lcd.DrawVLine(x0+n*hz100, y0+3, 2);
00055         for (int n=0; n<=60; n+=10)
00056             NumericLabel<int> num(x0+n*hz100, y0+TICK+3,
00057                                   "%1d", (int)(n*0.1f), Label::CENTER);
00058         Label frequency(x0+w0/2, y0+22, "FREQUENCY [kHz]", Label::CENTER);
00059 
00060         // Vertical axis
00061         lcd.DrawVLine(x0-TICK, y0-h0, h0);
00062         for (int n=0; n<=h0/px20dB; n++)
00063             lcd.DrawHLine(x0-TICK, y0-n*px20dB, TICK);
00064         for (int n=0; n<=h0/px20dB; n++)
00065             if (n == 0)
00066                 NumericLabel<int> num(x0-TICK-12, y0-n*px20dB-5, "%1d", n*20);
00067             else if( n > 0 && n < 5)
00068                 NumericLabel<int> num(x0-TICK-19, y0-n*px20dB-5, "%1d", n*20);
00069             else
00070                 NumericLabel<int> num(x0-TICK-26, y0-n*px20dB-5, "%1d", n*20);
00071         Label db(x0-27, y0-8*px20dB-20, "[dB]");
00072     }
00073     
00074     // Coordinate axis for 3D view
00075     void DrawAxis3D(int x0, int y0, int w0, int h0, uint32_t axisColor,
00076                   uint16_t ms100, uint16_t px1kHz, LCD_DISCO_F746NG &lcd)
00077     {
00078 
00079         
00080         const uint16_t TICK = 5;    // Length of graduation line
00081         
00082         ClearAxis(x0, y0, lcd);
00083         lcd.SetTextColor(axisColor);
00084 
00085         // Transverse axis
00086         lcd.DrawHLine(x0, y0+TICK, w0);
00087         for (int n=0; n<=w0/ms100; n++)
00088             if ((n % 10)== 0) lcd.DrawVLine(x0+n*ms100, y0, 5);
00089             else              lcd.DrawVLine(x0+n*ms100, y0+3, 2);
00090         for (int n=0; n<=w0/ms100; n+=10)
00091             NumericLabel<int> num(x0+n*ms100, y0+TICK+3,
00092                                   "%1d", (int)(n*0.1f), Label::CENTER);
00093         Label time(x0+w0/2, y0+22, "TIME [s]", Label::CENTER);
00094 
00095         // Vertical axis
00096         lcd.DrawVLine(x0-TICK, y0-h0, h0);
00097         for (int n=0; n<=h0/px1kHz; n++)
00098             lcd.DrawHLine(x0-TICK, y0-n*px1kHz, TICK);
00099         for (int n=0; n<=h0/px1kHz; n++)
00100             NumericLabel<int> num(x0-TICK-12, y0-n*px1kHz-5, "%1d", n);
00101         Label hz(x0-32, y0-5*px1kHz-20, "[kHz]");
00102     }
00103 
00104     // Display of relationship between color and dB
00105     void DrawColorDb(int y0, uint32_t axisColor, LCD_DISCO_F746NG &lcd)
00106     {
00107         lcd.SetTextColor(axisColor);
00108         lcd.DrawVLine(455, y0-100, 100);
00109         for (int n=0; n<=8; n++)
00110             lcd.DrawHLine(455, y0-(n*100)/8, 4);
00111         for (int n=0; n<=4; n++)
00112             NumericLabel<int> num(440, y0-(n*100)/4-5, "%2d", n*20);
00113         Label dB(432, y0-120, "[dB]");
00114 
00115         for (int n=0; n<=101; n++)
00116         {
00117             lcd.SetTextColor(HueScale(n/100.0f));
00118             lcd.DrawHLine(460, y0-n, 16);
00119         }
00120     }
00121     
00122     // Clear relationship between color and dB
00123     void ClearColorDb(int y0, uint32_t axisColor, LCD_DISCO_F746NG &lcd)
00124     {
00125         lcd.SetTextColor(GuiBase::ENUM_BACK);
00126         lcd.FillRect(432,y0-120, 45, 125);
00127     }
00128 
00129     // Spectrum update
00130     void SpectrumUpdate(Matrix<uint32_t> &x, FftAnalyzer &analyzer,
00131                         const Array<float> &sn, const Array<float> &db)
00132     {
00133         // One past spectra are shifted
00134         for (int n=0; n<x.Rows()-1; n++)
00135             for (int k=0; k<x.Cols(); k++)
00136                 x[n][k] = x[n+1][k];
00137 
00138         // New spectrum
00139         analyzer.Execute(sn, db);
00140         const float FACTOR = 1.0f/80.0f;    // Display range: 0 to 80 dB
00141         for (int k=0; k<=x.Cols(); k++)
00142             x[x.Rows()-1][k] = HueScale(FACTOR*((db[k] > 20) ? db[k]-20 : 0));
00143     }
00144 
00145     // Display 3D spectrum
00146     void DisplaySpectrum3D(const Matrix<uint32_t> &x, int x0, int y0,
00147                          int hBar, LCD_DISCO_F746NG &lcd)
00148     {
00149         for (int n=0; n<x.Rows(); n++)
00150             for (int k=0; k<x.Cols(); k++)
00151             {
00152                 lcd.SetTextColor(x[n][k]);
00153                 lcd.DrawHLine(x0+n*hBar, y0-k, hBar);
00154             }
00155     }
00156     
00157     // Display 2D spectrum
00158     void DisplaySpectrum2D(const Array<float> &x, int x0, int y0, int w0,
00159                          int hBar, LCD_DISCO_F746NG &lcd)
00160     {
00161             for (int k=0; k<w0; k++)
00162             {
00163                 lcd.SetTextColor(GuiBase::ENUM_BACK);
00164                 lcd.FillRect(x0+k*hBar, y0-160, hBar, 161);
00165                 lcd.SetTextColor(LCD_COLOR_WHITE);
00166                 lcd.DrawHLine(x0+k*hBar, y0-(unsigned int)(x[k]), hBar);
00167             }
00168     }
00169     
00170     // Clear display
00171     void ClearDisplay(int x0, int y0, int width, int height,  LCD_DISCO_F746NG &lcd)
00172     {
00173         lcd.SetTextColor(GuiBase::ENUM_BACK);
00174         lcd.FillRect(x0, y0-height, width, height);
00175     }
00176 }
00177 #endif  // METHOD_COLLECTION_HPP