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 main.cpp Source File

main.cpp

00001 /********************************************************
00002  *  Real time spectrogram
00003  *      Input: MEMS microphone
00004  *
00005  *  Mladen Adamovic, 3326/2016
00006  ********************************************************/
00007 
00008 #include "SAI_InOut.hpp"
00009 #include "F746_GUI.hpp"
00010 #include "MethodCollection.hpp"
00011 using namespace etf;
00012 
00013 //typedef enum {2D,3D}
00014 
00015 int main()
00016 {
00017     const int FS = AUDIO_FREQUENCY_16K; // Sampling frequency: 16 kHz
00018     const int N_FFT = 512;              // FFT score
00019     SaiIO mySai(SaiIO::INPUT, N_FFT+1, FS,          // Use with force
00020                 INPUT_DEVICE_DIGITAL_MICROPHONE_2); // Input device: MEMS microphone
00021 
00022     LCD_DISCO_F746NG &lcd = GuiBase::GetLcd();  // Obtain reference of object of LCD display
00023     lcd.Clear(GuiBase::ENUM_BACK);
00024     Label myLabel1(240, 2, "Real-time spectrogram", Label::CENTER, Font16);
00025 
00026     // Set ButtonGroup
00027     const uint16_t B_W = 50;
00028     const uint16_t B_Y = 242;
00029     const uint16_t B_H = 30;
00030     const string RUN_STOP[3] = {"2D","3D", "STOP"};
00031     ButtonGroup runStop(275, B_Y, B_W, B_H, 3, RUN_STOP, 0, 0, 3, 2);
00032 
00033     Button clearButton(430, B_Y, B_W, B_H, "CLEAR");
00034     clearButton.Inactivate();
00035 
00036     // Coordinate axis
00037     const uint16_t X0 = 40;         // The origin of the x coordinate of the display area
00038     const uint16_t Y0 = 200;        // The origin of the y coordinate of the display area
00039     const uint16_t PX_1KHZ = 32;    // Number of pixels corresponding to 1 kHz - 3D view
00040     const uint16_t PX_20dB = 20;    // Number of pixels corresponding to 20 dB - 2D view
00041     const uint16_t H0 = PX_1KHZ*5;  // Number of pixels corresponding to the length of the frequency axis (corresponding to 5 kHz)
00042     const uint16_t W0_3D = 360;        // Width in the horizontal direction 3D (unit: pixels)
00043     const uint16_t W0_2D = 384;        // Width in the horizontal direction 2D (unit: pixels)
00044     const float FRAME = (N_FFT/(float)FS)*1000.0f;  // Time corresponding to one frame (unit: ms)
00045     const uint16_t H_BAR = 2;       // The number of pixels in the horizontal direction corresponding to one frame when displaying
00046     const uint16_t MS100 = 100*H_BAR/FRAME; // Number of pixels corresponding to 100 ms 3D
00047     const float Hz100 = 6.4;                // Number of pixels corresponding to 100 hz 2D
00048     const uint32_t AXIS_COLOR = LCD_COLOR_WHITE;    
00049     DrawAxis2D(X0, Y0, W0_2D, H0, AXIS_COLOR, Hz100, PX_20dB, lcd);
00050 
00051     Array<float> sn(N_FFT+1);   // Buffer for storing signals for spectrum analysis
00052     Array<float> db(N_FFT/2+1); // A buffer storing the calculated log spectrum
00053     // Two-dimensional array that stores color data corresponding to the size of spectrum
00054     Matrix<uint32_t> spectra(W0_3D/H_BAR, H0+1, GuiBase::ENUM_BACK);
00055     FftAnalyzer fftAnalyzer(N_FFT+1, N_FFT);
00056 
00057     // Initialization of variable used in loop
00058     int stop = 0;       // 0: run, 1: stop
00059     int screen = 0;     // 0: 2D,  1: 3D
00060 
00061     while(!runStop.Touched(0) && !runStop.Touched(1)) {}   // Wait till you touch "2D" or "3D"
00062     // Start reading data
00063     mySai.RecordIn();
00064     while(!mySai.IsCaptured()) {}
00065 
00066     while (true)
00067     {
00068         runStop.GetTouchedNumber(stop);
00069         switch(stop){
00070         case 0:
00071             clearButton.Inactivate();
00072             if (screen == 1)
00073             {
00074                 // Clear relationship between color and dB
00075                 ClearColorDb(Y0, AXIS_COLOR, lcd);
00076                 // Draw 2D axis
00077                 DrawAxis2D(X0, Y0, W0_2D, H0, AXIS_COLOR, Hz100, PX_20dB, lcd);
00078                 // Screen - 2D
00079                 screen = 0;
00080             }
00081             if (mySai.IsCaptured())
00082             {
00083                 // Input of signal for one frame
00084                 for (int n=0; n<mySai.GetLength(); n++)
00085                 {
00086                     int16_t xL, xR;
00087                     mySai.Input(xL, xR);
00088                     sn[n] = (float)xL;
00089                 }
00090                 // Spectrum update
00091                 SpectrumUpdate(spectra, fftAnalyzer, sn, db);
00092                 // Display 2D spectrum
00093                 DisplaySpectrum2D(db, X0, Y0, W0_2D/H_BAR, H_BAR, lcd);
00094             }
00095             break;
00096         case 1:
00097             clearButton.Inactivate();
00098             if (screen == 0)
00099             {
00100                 // Draw relationship between color and dB
00101                 DrawColorDb(Y0, AXIS_COLOR, lcd);
00102                 // Draw 3D axis
00103                 DrawAxis3D(X0, Y0, W0_3D, H0, AXIS_COLOR, MS100, PX_1KHZ, lcd);
00104                 // Screen - 3D
00105                 screen = 1;
00106             }
00107             if (mySai.IsCaptured())
00108             {
00109                 // Input of signal for one frame
00110                 for (int n=0; n<mySai.GetLength(); n++)
00111                 {
00112                     int16_t xL, xR;
00113                     mySai.Input(xL, xR);
00114                     sn[n] = (float)xL;
00115                 }
00116                 // Spectrum update
00117                 SpectrumUpdate(spectra, fftAnalyzer, sn, db);
00118                 // Display spectrum
00119                 DisplaySpectrum3D(spectra, X0, Y0, H_BAR, lcd);
00120               }
00121               break;
00122         case 2:
00123             clearButton.Activate();
00124             if (clearButton.Touched())
00125             {
00126                 if (screen == 1)
00127                 {
00128                     spectra.Fill(GuiBase::ENUM_BACK);   // Process for clearing the spectrum display
00129                     DisplaySpectrum3D(spectra, X0, Y0, H_BAR, lcd);   // Clear spectrum display
00130                 }
00131                 else
00132                     ClearDisplay(X0, Y0, W0_2D, H0+1, lcd);
00133                 clearButton.Draw();
00134             }
00135             break;
00136         }
00137     }
00138 }
00139