Final version.

Dependencies:   F746_GUI F746_SAI_IO UIT_FFT_Real

Fork of F746_Spectrogram by 不韋 呂

main.cpp

Committer:
mladjo1993
Date:
2017-08-16
Revision:
7:23b60827582d
Parent:
6:b3885567877c
Child:
8:99d57d6e0ea1

File content as of revision 7:23b60827582d:

/********************************************************
 *  Real time spectrogram
 *      Input: MEMS microphone
 *
 *  Mladen Adamovic, 3326/2016
 ********************************************************/

#include "SAI_InOut.hpp"
#include "F746_GUI.hpp"
#include "MethodCollection.hpp"
using namespace Mikami;

int main()
{
    const int FS = AUDIO_FREQUENCY_16K; // Sampling frequency: 16 kHz
    const int N_FFT = 512;              // FFT score
    SaiIO mySai(SaiIO::INPUT, N_FFT+1, FS,          // Use with force
                INPUT_DEVICE_DIGITAL_MICROPHONE_2); // Input device: MEMS microphone

    LCD_DISCO_F746NG &lcd = GuiBase::GetLcd();  // Obtain reference of object of LCD display
    lcd.Clear(GuiBase::ENUM_BACK);
    Label myLabel1(240, 2, "Real-time spectrogram", Label::CENTER, Font16);

    // Set ButtonGroup
    const uint16_t B_W = 50;
    const uint16_t B_Y = 242;
    const uint16_t B_H = 30;
    const string RUN_STOP[2] = {"RUN", "STOP"};
    ButtonGroup runStop(325, B_Y, B_W, B_H, 2, RUN_STOP, 0, 0, 2, 1);

    Button clearButton(430, B_Y, B_W, B_H, "CLEAR");
    clearButton.Inactivate();

    // Coordinate axis
    const uint16_t X0 = 40;         // The origin of the x coordinate of the display area
    const uint16_t Y0 = 200;        // The origin of the y coordinate of the display area
    const uint16_t PX_1KHZ = 32;    // Number of pixels corresponding to 1 kHz
    const uint16_t H0 = PX_1KHZ*5;  // Number of pixels corresponding to the length of the frequency axis (corresponding to 5 kHz)
    const uint16_t W0 = 360;        // Width in the horizontal direction (unit: pixels)
    const float FRAME = (N_FFT/(float)FS)*1000.0f;  // Time corresponding to one frame (unit: ms)
    const uint16_t H_BAR = 2;       // The number of pixels in the horizontal direction corresponding to one frame when displaying
    const uint16_t MS100 = 100*H_BAR/FRAME; // Number of pixels corresponding to 100 ms
    const uint32_t AXIS_COLOR = LCD_COLOR_WHITE;    
    DrawAxis(X0, Y0, W0, H0, AXIS_COLOR, MS100, PX_1KHZ, lcd);

    // Display of relationship between color and dB
    ColorDb(Y0, AXIS_COLOR, lcd);

    Array<float> sn(N_FFT+1);   // Buffer for storing signals for spectrum analysis
    Array<float> db(N_FFT/2+1); // A buffer storing the calculated log spectrum
    // Two-dimensional array that stores color data corresponding to the size of spectrum
    Matrix<uint32_t> spectra(W0/H_BAR, H0+1, GuiBase::ENUM_BACK);
    FftAnalyzer fftAnalyzer(N_FFT+1, N_FFT);

    // Initialization of variable used in loop
    int stop = 0;       // 0: run, 1: stop

    while(!runStop.Touched(0)) {}   // Wait till you touch "RUN"
    // Start reading data
    mySai.RecordIn();
    while(!mySai.IsCaptured()) {}

    while (true)
    {
        runStop.GetTouchedNumber(stop);
        if (stop == 0)
        {
            clearButton.Inactivate();
            if (mySai.IsCaptured())
            {
                // Input of signal for one frame
                for (int n=0; n<mySai.GetLength(); n++)
                {
                    int16_t xL, xR;
                    mySai.Input(xL, xR);
                    sn[n] = (float)xL;
                }

                // Spectrum update
                SpectrumUpdate(spectra, fftAnalyzer, sn, db);
                // Display spectrum
                DisplaySpectrum(spectra, X0, Y0, H_BAR, lcd);
            }
        }
        else
        {
            clearButton.Activate();
            if (clearButton.Touched())
            {
                spectra.Fill(GuiBase::ENUM_BACK);   // Process for clearing the spectrum display
                DisplaySpectrum(spectra, X0, Y0, H_BAR, lcd);   // Clear spectrum display
                clearButton.Draw();
            }
        }
    }
}