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-10-26
Revision:
1:2ef356b500f2
Parent:
0:c35b8a23a863
Child:
2:4e34945b5c00

File content as of revision 1:2ef356b500f2:

//-----------------------------------------------------------
//  Demo waveform and spectrum display
//
//  2015/10/26, 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"

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 MAX_DB_ = 60;     // Maximum dB

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;
    lcd_.Clear(backColor);

    Button *buttons[5];
    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_, MAX_DB_, FS_);
    bool dataOk = false;
    int vowel = -1;
    while (true)
    {
        for (int k=0; k<N_VOWEL_; k++)
        {
            if (buttons[k]->Touched())
            {
                for (int n=0; n<N_DATA_; n++) sn[n] = sn_[k][n];
                WaveformDisplay(lcd_, 50, 40, sn, N_DATA_, backColor);
                
                buttons[k]->DrawButton(0xFF0000B0);
                if ((vowel != -1) && (k != vowel))
                    buttons[vowel]->DrawButton(LCD_COLOR_BLUE);
                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);
            }
        }
        
        if (fftButtonBar.Touched() && dataOk)
            disp.BarChart(db1, backColor);

        if (fftButtonLine.Touched() && dataOk)
            disp.LineChart(db1, backColor);

        if (lpButton.Touched() && dataOk)
            disp.LineChart(db2, backColor);

        wait(0.1);
    }
}