Final version.

Dependencies:   F746_GUI F746_SAI_IO UIT_FFT_Real

Fork of F746_Spectrogram by 不韋 呂

Committer:
mladjo1993
Date:
Wed Aug 16 10:06:28 2017 +0000
Revision:
7:23b60827582d
Parent:
6:b3885567877c
Child:
8:99d57d6e0ea1
Comments changed to english.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mladjo1993 7:23b60827582d 1 /********************************************************
mladjo1993 7:23b60827582d 2 * Real time spectrogram
mladjo1993 7:23b60827582d 3 * Input: MEMS microphone
mladjo1993 7:23b60827582d 4 *
mladjo1993 7:23b60827582d 5 * Mladen Adamovic, 3326/2016
mladjo1993 7:23b60827582d 6 ********************************************************/
MikamiUitOpen 0:9470a174c910 7
MikamiUitOpen 0:9470a174c910 8 #include "SAI_InOut.hpp"
MikamiUitOpen 2:1f092ac020e1 9 #include "F746_GUI.hpp"
MikamiUitOpen 0:9470a174c910 10 #include "MethodCollection.hpp"
MikamiUitOpen 0:9470a174c910 11 using namespace Mikami;
MikamiUitOpen 0:9470a174c910 12
MikamiUitOpen 0:9470a174c910 13 int main()
MikamiUitOpen 0:9470a174c910 14 {
mladjo1993 7:23b60827582d 15 const int FS = AUDIO_FREQUENCY_16K; // Sampling frequency: 16 kHz
mladjo1993 7:23b60827582d 16 const int N_FFT = 512; // FFT score
mladjo1993 7:23b60827582d 17 SaiIO mySai(SaiIO::INPUT, N_FFT+1, FS, // Use with force
mladjo1993 7:23b60827582d 18 INPUT_DEVICE_DIGITAL_MICROPHONE_2); // Input device: MEMS microphone
MikamiUitOpen 0:9470a174c910 19
mladjo1993 7:23b60827582d 20 LCD_DISCO_F746NG &lcd = GuiBase::GetLcd(); // Obtain reference of object of LCD display
MikamiUitOpen 5:c0877670b0ac 21 lcd.Clear(GuiBase::ENUM_BACK);
MikamiUitOpen 2:1f092ac020e1 22 Label myLabel1(240, 2, "Real-time spectrogram", Label::CENTER, Font16);
MikamiUitOpen 0:9470a174c910 23
mladjo1993 7:23b60827582d 24 // Set ButtonGroup
MikamiUitOpen 0:9470a174c910 25 const uint16_t B_W = 50;
MikamiUitOpen 0:9470a174c910 26 const uint16_t B_Y = 242;
MikamiUitOpen 0:9470a174c910 27 const uint16_t B_H = 30;
MikamiUitOpen 0:9470a174c910 28 const string RUN_STOP[2] = {"RUN", "STOP"};
MikamiUitOpen 0:9470a174c910 29 ButtonGroup runStop(325, B_Y, B_W, B_H, 2, RUN_STOP, 0, 0, 2, 1);
MikamiUitOpen 6:b3885567877c 30
MikamiUitOpen 6:b3885567877c 31 Button clearButton(430, B_Y, B_W, B_H, "CLEAR");
MikamiUitOpen 6:b3885567877c 32 clearButton.Inactivate();
MikamiUitOpen 0:9470a174c910 33
mladjo1993 7:23b60827582d 34 // Coordinate axis
mladjo1993 7:23b60827582d 35 const uint16_t X0 = 40; // The origin of the x coordinate of the display area
mladjo1993 7:23b60827582d 36 const uint16_t Y0 = 200; // The origin of the y coordinate of the display area
mladjo1993 7:23b60827582d 37 const uint16_t PX_1KHZ = 32; // Number of pixels corresponding to 1 kHz
mladjo1993 7:23b60827582d 38 const uint16_t H0 = PX_1KHZ*5; // Number of pixels corresponding to the length of the frequency axis (corresponding to 5 kHz)
mladjo1993 7:23b60827582d 39 const uint16_t W0 = 360; // Width in the horizontal direction (unit: pixels)
mladjo1993 7:23b60827582d 40 const float FRAME = (N_FFT/(float)FS)*1000.0f; // Time corresponding to one frame (unit: ms)
mladjo1993 7:23b60827582d 41 const uint16_t H_BAR = 2; // The number of pixels in the horizontal direction corresponding to one frame when displaying
mladjo1993 7:23b60827582d 42 const uint16_t MS100 = 100*H_BAR/FRAME; // Number of pixels corresponding to 100 ms
MikamiUitOpen 6:b3885567877c 43 const uint32_t AXIS_COLOR = LCD_COLOR_WHITE;
MikamiUitOpen 0:9470a174c910 44 DrawAxis(X0, Y0, W0, H0, AXIS_COLOR, MS100, PX_1KHZ, lcd);
MikamiUitOpen 0:9470a174c910 45
mladjo1993 7:23b60827582d 46 // Display of relationship between color and dB
MikamiUitOpen 0:9470a174c910 47 ColorDb(Y0, AXIS_COLOR, lcd);
MikamiUitOpen 6:b3885567877c 48
mladjo1993 7:23b60827582d 49 Array<float> sn(N_FFT+1); // Buffer for storing signals for spectrum analysis
mladjo1993 7:23b60827582d 50 Array<float> db(N_FFT/2+1); // A buffer storing the calculated log spectrum
mladjo1993 7:23b60827582d 51 // Two-dimensional array that stores color data corresponding to the size of spectrum
MikamiUitOpen 6:b3885567877c 52 Matrix<uint32_t> spectra(W0/H_BAR, H0+1, GuiBase::ENUM_BACK);
MikamiUitOpen 0:9470a174c910 53 FftAnalyzer fftAnalyzer(N_FFT+1, N_FFT);
MikamiUitOpen 0:9470a174c910 54
mladjo1993 7:23b60827582d 55 // Initialization of variable used in loop
MikamiUitOpen 6:b3885567877c 56 int stop = 0; // 0: run, 1: stop
MikamiUitOpen 3:6a2c8ff46f73 57
mladjo1993 7:23b60827582d 58 while(!runStop.Touched(0)) {} // Wait till you touch "RUN"
mladjo1993 7:23b60827582d 59 // Start reading data
MikamiUitOpen 0:9470a174c910 60 mySai.RecordIn();
MikamiUitOpen 6:b3885567877c 61 while(!mySai.IsCaptured()) {}
MikamiUitOpen 6:b3885567877c 62
MikamiUitOpen 0:9470a174c910 63 while (true)
MikamiUitOpen 0:9470a174c910 64 {
MikamiUitOpen 0:9470a174c910 65 runStop.GetTouchedNumber(stop);
MikamiUitOpen 0:9470a174c910 66 if (stop == 0)
MikamiUitOpen 0:9470a174c910 67 {
MikamiUitOpen 6:b3885567877c 68 clearButton.Inactivate();
MikamiUitOpen 0:9470a174c910 69 if (mySai.IsCaptured())
MikamiUitOpen 0:9470a174c910 70 {
mladjo1993 7:23b60827582d 71 // Input of signal for one frame
MikamiUitOpen 0:9470a174c910 72 for (int n=0; n<mySai.GetLength(); n++)
MikamiUitOpen 0:9470a174c910 73 {
MikamiUitOpen 0:9470a174c910 74 int16_t xL, xR;
MikamiUitOpen 0:9470a174c910 75 mySai.Input(xL, xR);
MikamiUitOpen 0:9470a174c910 76 sn[n] = (float)xL;
MikamiUitOpen 0:9470a174c910 77 }
MikamiUitOpen 0:9470a174c910 78
mladjo1993 7:23b60827582d 79 // Spectrum update
MikamiUitOpen 0:9470a174c910 80 SpectrumUpdate(spectra, fftAnalyzer, sn, db);
mladjo1993 7:23b60827582d 81 // Display spectrum
MikamiUitOpen 0:9470a174c910 82 DisplaySpectrum(spectra, X0, Y0, H_BAR, lcd);
MikamiUitOpen 0:9470a174c910 83 }
MikamiUitOpen 0:9470a174c910 84 }
MikamiUitOpen 0:9470a174c910 85 else
MikamiUitOpen 0:9470a174c910 86 {
MikamiUitOpen 6:b3885567877c 87 clearButton.Activate();
MikamiUitOpen 6:b3885567877c 88 if (clearButton.Touched())
MikamiUitOpen 0:9470a174c910 89 {
mladjo1993 7:23b60827582d 90 spectra.Fill(GuiBase::ENUM_BACK); // Process for clearing the spectrum display
mladjo1993 7:23b60827582d 91 DisplaySpectrum(spectra, X0, Y0, H_BAR, lcd); // Clear spectrum display
MikamiUitOpen 6:b3885567877c 92 clearButton.Draw();
MikamiUitOpen 0:9470a174c910 93 }
MikamiUitOpen 0:9470a174c910 94 }
MikamiUitOpen 0:9470a174c910 95 }
MikamiUitOpen 0:9470a174c910 96 }
MikamiUitOpen 6:b3885567877c 97