不韋 呂 / F746_Spectrogram

Dependencies:   F746_GUI F746_SAI_IO UIT_FFT_Real

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MethodCollection.hpp Source File

MethodCollection.hpp

00001 //--------------------------------------------------------------
00002 //  スペクトログラムで使うグローバル関数
00003 //
00004 //  2017/04/10, Copyright (c) 2017 MIKAMI, Naoki
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 Mikami
00016 {
00017     // 強度を色相の違いに変換
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     // 座標軸
00033     void DrawAxis(int x0, int y0, int w0, int h0, uint32_t axisColor,
00034                   uint16_t ms100, uint16_t px1kHz, LCD_DISCO_F746NG &lcd)
00035     {
00036         const uint16_t TICK = 5;    // 目盛線の長さ
00037         lcd.SetTextColor(axisColor);
00038 
00039         // 横標軸
00040         lcd.DrawHLine(x0, y0+TICK, w0);
00041         for (int n=0; n<=w0/ms100; n++)
00042             if ((n % 10)== 0) lcd.DrawVLine(x0+n*ms100, y0, 5);
00043             else              lcd.DrawVLine(x0+n*ms100, y0+3, 2);
00044         for (int n=0; n<=w0/ms100; n+=10)
00045             NumericLabel<int> num(x0+n*ms100, y0+TICK+3,
00046                                   "%1d", (int)(n*0.1f), Label::CENTER);
00047         Label time(x0+w0/2, y0+22, "TIME [s]", Label::CENTER);
00048 
00049         // 縦標軸
00050         lcd.DrawVLine(x0-TICK, y0-h0, h0);
00051         for (int n=0; n<=h0/px1kHz; n++)
00052             lcd.DrawHLine(x0-TICK, y0-n*px1kHz, TICK);
00053         for (int n=0; n<=h0/px1kHz; n++)
00054             NumericLabel<int> num(x0-TICK-12, y0-n*px1kHz-5, "%1d", n);
00055         Label hz(x0-32, y0-5*px1kHz-20, "[kHz]");
00056     }
00057 
00058     // 色と dB の関係の表示
00059     void ColorDb(int y0, uint32_t axisColor, LCD_DISCO_F746NG &lcd)
00060     {
00061         lcd.SetTextColor(axisColor);
00062         lcd.DrawVLine(455, y0-100, 100);
00063         for (int n=0; n<=8; n++)
00064             lcd.DrawHLine(455, y0-(n*100)/8, 4);
00065         for (int n=0; n<=4; n++)
00066             NumericLabel<int> num(440, y0-(n*100)/4-5, "%2d", n*20);
00067         Label dB(432, y0-120, "[dB]");
00068 
00069         for (int n=0; n<=101; n++)
00070         {
00071             lcd.SetTextColor(HueScale(n/100.0f));
00072             lcd.DrawHLine(460, y0-n, 16);
00073         }
00074     }
00075 
00076     // スペクトルの更新
00077     void SpectrumUpdate(Matrix<uint32_t> &x, FftAnalyzer &analyzer,
00078                         const Array<float> &sn, const Array<float> &db)
00079     {
00080         // 過去のスペクトルを一つずらす
00081         for (int n=0; n<x.Rows()-1; n++)
00082             for (int k=0; k<x.Cols(); k++)
00083                 x[n][k] = x[n+1][k];
00084 
00085         // 新しいスペクトル
00086         analyzer.Execute(sn, db);
00087         const float FACTOR = 1.0f/80.0f;    // 表示範囲: 0 ~ 80 dB
00088         for (int k=0; k<=x.Cols(); k++)
00089             x[x.Rows()-1][k] = HueScale(FACTOR*((db[k] > 20) ? db[k]-20 : 0));
00090     }
00091 
00092     // スペクトルの表示
00093     void DisplaySpectrum(const Matrix<uint32_t> &x, int x0, int y0,
00094                          int hBar, LCD_DISCO_F746NG &lcd)
00095     {
00096         for (int n=0; n<x.Rows(); n++)
00097             for (int k=0; k<x.Cols(); k++)
00098             {
00099                 lcd.SetTextColor(x[n][k]);
00100                 lcd.DrawHLine(x0+n*hBar, y0-k, hBar);
00101             }
00102     }
00103 }
00104 #endif  // METHOD_COLLECTION_HPP