Spectrum analyzer using DISCO-F746NG. Spectrum is calculated by FFT or linear prediction. The vowel data is in "vowel_data.hpp"

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG UIT_FFT_Real mbed BUTTON_GROUP

main.cpp

Committer:
MikamiUitOpen
Date:
2015-11-03
Revision:
3:ce2e8481790c
Parent:
2:4e34945b5c00
Child:
4:3c5241f9fbf3

File content as of revision 3:ce2e8481790c:

//-----------------------------------------------------------
//  Demo waveform and spectrum display
//      Tap the screen to begin the spectrum analyzer 
//
//  2015/11/03, Copyright (c) 2015 MIKAMI, Naoki
//-----------------------------------------------------------

#include "vowel_data.hpp"
#include "button.hpp"
#include "waveform_display.hpp"
#include "FFT_Analysis.hpp"
#include "SpectrumDisplay.hpp"
#include "LPC_Analysis.hpp"
#include "PadalingImage.h"

using namespace Mikami;

const int N_FFT_ = 512;     // number of date for FFT
const int X0_ = 50;         // Origin for x axis
const int Y0_ = 236;        // Origin for y axis
const float DB1_ = 2.4f;    // Pixels for 1 dB
const int BIN_ = 1;         // Pixels per bin
const int W_DB = 60;        // Width in dB to be displayed

const int FS_ = 8000;       // Sampling frequency: 8 kHz

LCD_DISCO_F746NG lcd_;
TS_DISCO_F746NG ts_;

FftAnalyzer fft_(N_DATA_, N_FFT_);      // using FFT
LpcAnalyzer lpc_(N_DATA_, 10, N_FFT_);  // using linear prediction

int main()
{
    const char AIUEO[5] = {'a', 'i', 'u', 'e', 'o'};
    int16_t sn[N_DATA_];
    float sn_f[N_DATA_];
    float db1[N_FFT_/2+1];  // Log powerspectrum using FFT
    float db2[N_FFT_/2+1];  // Log powerspectrum using linear prediction

    uint32_t backColor = LCD_COLOR_LIGHTBLUE;

    // Initial image
    lcd_.DrawBitmap(0, 0, (uint8_t *)hattatsurei);

    bool ok = false;
    while (!ok)     // wait for tapped
    {
        TS_StateTypeDef state;
        ts_.GetState(&state);
        ok = state.touchDetected;
    }
    wait(0.5f);

    lcd_.Clear(backColor);

    Button *buttons[5];     // for select /a/, /i/, /u/, /e/, /o/
    for (int n=0; n<5; n++)
    {
        char strButton[8];
        sprintf(strButton, "/%c/", AIUEO[n]);
        buttons[n] = new Button(lcd_, ts_, 430, 15+50*n, 50, 40,
                                LCD_COLOR_BLUE, strButton, Font16);
    }
    Button fftButtonBar(lcd_, ts_, 340, 15, 80, 40, LCD_COLOR_MAGENTA, "FFT (Bar)", Font12);
    Button fftButtonLine(lcd_, ts_, 340, 65, 80, 40, LCD_COLOR_MAGENTA, "FFT (Line)", Font12);
    Button lpButton(lcd_, ts_, 340, 115, 80, 40, LCD_COLOR_MAGENTA, "LP", Font12);
    lcd_.SetBackColor(backColor);

    SpectrumDisplay disp(lcd_, N_FFT_, X0_, Y0_, DB1_, BIN_, W_DB, FS_);
    bool dataOk = false;
    int vowel = -1;
    while (true)
    {
        for (int k=0; k<N_VOWEL_; k++)
        {
            if (buttons[k]->Touched(0xFF0000B0))
            {
                for (int n=0; n<N_DATA_; n++) sn[n] = sn_[k][n];
                WaveformDisplay(lcd_, 50, 40, sn, N_DATA_, backColor);

                if ((vowel != -1) && (k != vowel))
                    buttons[vowel]->ReDraw();
                vowel = k;

                for (int n=0; n<N_DATA_; n++) sn_f[n] = sn[n];
                fft_.Execute(sn_f, db1);
                lpc_.Execute(sn_f, db2);
                dataOk = true;
                disp.Clear(backColor);

                fftButtonBar.Draw(LCD_COLOR_MAGENTA);
                fftButtonLine.Draw(LCD_COLOR_MAGENTA);
                lpButton.Draw(LCD_COLOR_MAGENTA);
            }
        }

        if (fftButtonBar.Touched() && dataOk)
        {
            fftButtonBar.Draw(0xFFD000D0);
            fftButtonLine.Draw(LCD_COLOR_MAGENTA);
            lpButton.Draw(LCD_COLOR_MAGENTA);
            disp.BarChart(db1, backColor);
        }

        if (fftButtonLine.Touched() && dataOk)
        {
            fftButtonBar.Draw(LCD_COLOR_MAGENTA);
            fftButtonLine.Draw(0xFFD000D0);
            lpButton.Draw(LCD_COLOR_MAGENTA);
            disp.LineChart(db1, backColor);
        }

        if (lpButton.Touched() && dataOk)
        {
            fftButtonBar.Draw(LCD_COLOR_MAGENTA);
            fftButtonLine.Draw(LCD_COLOR_MAGENTA);
            lpButton.Draw(0xFFD000D0);
            disp.LineChart(db2, backColor);
        }

        wait(0.1);
    }
}