![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mini projet de FFT
Dependencies: mbed Grove_LCD_RGB_Backlight UIT_FFT_Real
main.cpp@1:a806f3aacf1f, 2019-04-15 (annotated)
- Committer:
- SBACCARI
- Date:
- Mon Apr 15 10:08:19 2019 +0000
- Revision:
- 1:a806f3aacf1f
- Parent:
- 0:c7f159ed0bc3
- Child:
- 2:525fb567d140
add 1024 ADC comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SBACCARI | 0:c7f159ed0bc3 | 1 | #include "mbed.h" |
SBACCARI | 0:c7f159ed0bc3 | 2 | #include "Grove_LCD_RGB_Backlight.h" |
SBACCARI | 0:c7f159ed0bc3 | 3 | #include "fftReal.hpp" |
SBACCARI | 0:c7f159ed0bc3 | 4 | |
SBACCARI | 0:c7f159ed0bc3 | 5 | Grove_LCD_RGB_Backlight ecran(P0_27, P0_28); |
SBACCARI | 0:c7f159ed0bc3 | 6 | AnalogIn micro(A1); |
SBACCARI | 0:c7f159ed0bc3 | 7 | DigitalOut myled(LED1); |
SBACCARI | 0:c7f159ed0bc3 | 8 | |
SBACCARI | 0:c7f159ed0bc3 | 9 | Ticker sampler; |
SBACCARI | 0:c7f159ed0bc3 | 10 | const int FFT_len = 256; |
SBACCARI | 0:c7f159ed0bc3 | 11 | int sampling_freq = 44000; |
SBACCARI | 0:c7f159ed0bc3 | 12 | float samples[FFT_len*2]; |
SBACCARI | 0:c7f159ed0bc3 | 13 | int i = 0; |
SBACCARI | 0:c7f159ed0bc3 | 14 | |
SBACCARI | 0:c7f159ed0bc3 | 15 | void sampling_interrup() |
SBACCARI | 0:c7f159ed0bc3 | 16 | { |
SBACCARI | 1:a806f3aacf1f | 17 | samples[i] = 1024*micro.read(); // 1024 : ADC of the sound sensor |
SBACCARI | 0:c7f159ed0bc3 | 18 | i++; |
SBACCARI | 0:c7f159ed0bc3 | 19 | if (i >= FFT_len*2) { |
SBACCARI | 0:c7f159ed0bc3 | 20 | sampler.detach(); |
SBACCARI | 0:c7f159ed0bc3 | 21 | } |
SBACCARI | 0:c7f159ed0bc3 | 22 | } |
SBACCARI | 0:c7f159ed0bc3 | 23 | |
SBACCARI | 0:c7f159ed0bc3 | 24 | void samplingBegin() |
SBACCARI | 0:c7f159ed0bc3 | 25 | { |
SBACCARI | 0:c7f159ed0bc3 | 26 | // Reset sample buffer position and start callback at the sampling frequency |
SBACCARI | 0:c7f159ed0bc3 | 27 | i = 0; |
SBACCARI | 0:c7f159ed0bc3 | 28 | sampler.attach_us(&sampling_interrup, 1000000/sampling_freq); |
SBACCARI | 0:c7f159ed0bc3 | 29 | } |
SBACCARI | 0:c7f159ed0bc3 | 30 | |
SBACCARI | 0:c7f159ed0bc3 | 31 | bool samplingDone() |
SBACCARI | 0:c7f159ed0bc3 | 32 | { |
SBACCARI | 0:c7f159ed0bc3 | 33 | return i >= FFT_len*2; |
SBACCARI | 0:c7f159ed0bc3 | 34 | } |
SBACCARI | 0:c7f159ed0bc3 | 35 | |
SBACCARI | 0:c7f159ed0bc3 | 36 | |
SBACCARI | 0:c7f159ed0bc3 | 37 | int main() { |
SBACCARI | 0:c7f159ed0bc3 | 38 | char str[20]; |
SBACCARI | 0:c7f159ed0bc3 | 39 | |
SBACCARI | 0:c7f159ed0bc3 | 40 | // max 1 is set to the second FFT bin, because the first bin has the DC componenet of the signal. |
SBACCARI | 0:c7f159ed0bc3 | 41 | int max1 = 2; |
SBACCARI | 0:c7f159ed0bc3 | 42 | int max2 = 3; |
SBACCARI | 0:c7f159ed0bc3 | 43 | float val = 0; |
SBACCARI | 0:c7f159ed0bc3 | 44 | |
SBACCARI | 0:c7f159ed0bc3 | 45 | // CONFIG FFT |
SBACCARI | 0:c7f159ed0bc3 | 46 | Mikami::Complex fft_bins[FFT_len]; |
SBACCARI | 0:c7f159ed0bc3 | 47 | Mikami::FftReal fft((int16_t)FFT_len); |
SBACCARI | 0:c7f159ed0bc3 | 48 | |
SBACCARI | 0:c7f159ed0bc3 | 49 | // CONFIG ECRAN |
SBACCARI | 0:c7f159ed0bc3 | 50 | ecran.setRGB(0, 255, 0); |
SBACCARI | 0:c7f159ed0bc3 | 51 | ecran.clear(); |
SBACCARI | 0:c7f159ed0bc3 | 52 | ecran.locate(0, 1); |
SBACCARI | 0:c7f159ed0bc3 | 53 | |
SBACCARI | 0:c7f159ed0bc3 | 54 | samplingBegin(); |
SBACCARI | 0:c7f159ed0bc3 | 55 | |
SBACCARI | 0:c7f159ed0bc3 | 56 | // The following loop, will do FFT and display highest detected frequency on the screen every 2s |
SBACCARI | 0:c7f159ed0bc3 | 57 | // Commented sections, are debug mode. If uncommented, it will do FFT, display all the spectrum then restart, it will take about 10s + FFT_len/2 seconds |
SBACCARI | 0:c7f159ed0bc3 | 58 | |
SBACCARI | 0:c7f159ed0bc3 | 59 | while(1) { |
SBACCARI | 0:c7f159ed0bc3 | 60 | /*ecran.clear(); |
SBACCARI | 0:c7f159ed0bc3 | 61 | sprintf(str,"Sampling ..."); |
SBACCARI | 0:c7f159ed0bc3 | 62 | ecran.print(str); |
SBACCARI | 0:c7f159ed0bc3 | 63 | ecran.setRGB(255, 0, 0); |
SBACCARI | 0:c7f159ed0bc3 | 64 | wait(0.6); |
SBACCARI | 0:c7f159ed0bc3 | 65 | ecran.setRGB(0, 0, 255); |
SBACCARI | 0:c7f159ed0bc3 | 66 | wait(0.6); |
SBACCARI | 0:c7f159ed0bc3 | 67 | ecran.setRGB(0, 255, 0);*/ |
SBACCARI | 0:c7f159ed0bc3 | 68 | |
SBACCARI | 0:c7f159ed0bc3 | 69 | // DO FFT |
SBACCARI | 0:c7f159ed0bc3 | 70 | if (samplingDone()) |
SBACCARI | 0:c7f159ed0bc3 | 71 | { |
SBACCARI | 0:c7f159ed0bc3 | 72 | /*ecran.locate(0, 0); |
SBACCARI | 0:c7f159ed0bc3 | 73 | sprintf(str,"Sampling done"); |
SBACCARI | 0:c7f159ed0bc3 | 74 | ecran.print(str); |
SBACCARI | 0:c7f159ed0bc3 | 75 | wait(1);*/ |
SBACCARI | 0:c7f159ed0bc3 | 76 | |
SBACCARI | 0:c7f159ed0bc3 | 77 | fft.Execute(samples,fft_bins); |
SBACCARI | 0:c7f159ed0bc3 | 78 | wait(0.5); |
SBACCARI | 0:c7f159ed0bc3 | 79 | |
SBACCARI | 0:c7f159ed0bc3 | 80 | /*ecran.clear(); |
SBACCARI | 0:c7f159ed0bc3 | 81 | sprintf(str,"FFT Done"); |
SBACCARI | 0:c7f159ed0bc3 | 82 | ecran.print(str); |
SBACCARI | 0:c7f159ed0bc3 | 83 | wait(1);*/ |
SBACCARI | 0:c7f159ed0bc3 | 84 | |
SBACCARI | 0:c7f159ed0bc3 | 85 | // PRINT SPECTRUM |
SBACCARI | 0:c7f159ed0bc3 | 86 | for(int j = 0;j < FFT_len/2 ;++j){ |
SBACCARI | 0:c7f159ed0bc3 | 87 | ecran.clear(); |
SBACCARI | 0:c7f159ed0bc3 | 88 | float mod = std::abs(fft_bins[j]); |
SBACCARI | 0:c7f159ed0bc3 | 89 | float freq = j*sampling_freq/(FFT_len*1000); |
SBACCARI | 0:c7f159ed0bc3 | 90 | |
SBACCARI | 0:c7f159ed0bc3 | 91 | /*sprintf(str,"%d.Freq = %.2f kHz", j,freq ); |
SBACCARI | 0:c7f159ed0bc3 | 92 | ecran.locate(0,1); |
SBACCARI | 0:c7f159ed0bc3 | 93 | ecran.print(str); |
SBACCARI | 0:c7f159ed0bc3 | 94 | |
SBACCARI | 0:c7f159ed0bc3 | 95 | sprintf(str,"%d.Mag = %.2f", j, mod ); |
SBACCARI | 0:c7f159ed0bc3 | 96 | ecran.locate(0,0); |
SBACCARI | 0:c7f159ed0bc3 | 97 | ecran.print(str);*/ |
SBACCARI | 0:c7f159ed0bc3 | 98 | |
SBACCARI | 0:c7f159ed0bc3 | 99 | if( (j>5)and mod >= std::abs(fft_bins[max1]) ){ |
SBACCARI | 0:c7f159ed0bc3 | 100 | max1 = j; |
SBACCARI | 0:c7f159ed0bc3 | 101 | } |
SBACCARI | 0:c7f159ed0bc3 | 102 | /*else if ( (j>3) and (std::abs(fft_bins[j]) >= std::abs(fft_bins[max2])) and (std::abs(fft_bins[j]) < std::abs(fft_bins[max1]))){ |
SBACCARI | 0:c7f159ed0bc3 | 103 | max2 = j; |
SBACCARI | 0:c7f159ed0bc3 | 104 | } |
SBACCARI | 0:c7f159ed0bc3 | 105 | wait(1);*/ |
SBACCARI | 0:c7f159ed0bc3 | 106 | } |
SBACCARI | 0:c7f159ed0bc3 | 107 | |
SBACCARI | 0:c7f159ed0bc3 | 108 | val = max1*sampling_freq/(FFT_len*1000); |
SBACCARI | 0:c7f159ed0bc3 | 109 | float mod = std::abs(fft_bins[max1]); |
SBACCARI | 0:c7f159ed0bc3 | 110 | |
SBACCARI | 0:c7f159ed0bc3 | 111 | /*sprintf(str,"%max1= %.2f", mod ); |
SBACCARI | 0:c7f159ed0bc3 | 112 | ecran.locate(0,0); |
SBACCARI | 0:c7f159ed0bc3 | 113 | ecran.print(str);*/ |
SBACCARI | 0:c7f159ed0bc3 | 114 | |
SBACCARI | 0:c7f159ed0bc3 | 115 | ecran.clear(); |
SBACCARI | 0:c7f159ed0bc3 | 116 | sprintf(str,"max= %.2f kHz", val ); |
SBACCARI | 0:c7f159ed0bc3 | 117 | ecran.locate(0,0); |
SBACCARI | 0:c7f159ed0bc3 | 118 | ecran.print(str); |
SBACCARI | 0:c7f159ed0bc3 | 119 | wait(2); |
SBACCARI | 0:c7f159ed0bc3 | 120 | |
SBACCARI | 0:c7f159ed0bc3 | 121 | /*val = max2*sampling_freq/(FFT_len*1000); |
SBACCARI | 0:c7f159ed0bc3 | 122 | sprintf(str,"max 2 = %.2f kHz", val); |
SBACCARI | 0:c7f159ed0bc3 | 123 | ecran.locate(0,1); |
SBACCARI | 0:c7f159ed0bc3 | 124 | ecran.print(str); |
SBACCARI | 0:c7f159ed0bc3 | 125 | wait(2);*/ |
SBACCARI | 0:c7f159ed0bc3 | 126 | |
SBACCARI | 0:c7f159ed0bc3 | 127 | samplingBegin(); |
SBACCARI | 0:c7f159ed0bc3 | 128 | |
SBACCARI | 0:c7f159ed0bc3 | 129 | } |
SBACCARI | 0:c7f159ed0bc3 | 130 | |
SBACCARI | 0:c7f159ed0bc3 | 131 | /* // DEBUG SCREEN |
SBACCARI | 0:c7f159ed0bc3 | 132 | ecran.clear(); |
SBACCARI | 0:c7f159ed0bc3 | 133 | myled = !myled; |
SBACCARI | 0:c7f159ed0bc3 | 134 | sprintf(str,"Waiting ..."); |
SBACCARI | 0:c7f159ed0bc3 | 135 | ecran.print(str);*/ |
SBACCARI | 0:c7f159ed0bc3 | 136 | } |
SBACCARI | 0:c7f159ed0bc3 | 137 | } |