A guitar Tuner project made by: Guarino Yuri Pota Giuseppe Sito Leonardo It is made up of a circuit of signal amplification(using LM386 OP-AMP), and a circuit of many LEDs that display if the guitar is tuned or not.

Dependencies:   mbed mbed-dsp

Committer:
gpota
Date:
Sun Dec 09 16:04:49 2018 +0000
Revision:
1:45d485b8966f
Parent:
0:2529b7b37e9a
A guitar Tuner project made by: Guarino Yuri Pota Giuseppe Sito Leonardo It is made up of a circuit of signal amplification(using LM386 OP-AMP), and a circuit of many LEDs that display if the guitar is tuned or not.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gpota 0:2529b7b37e9a 1 #include "mbed.h"
gpota 0:2529b7b37e9a 2 #include "mbed.h"
gpota 0:2529b7b37e9a 3 #include "arm_math.h"
gpota 0:2529b7b37e9a 4 #include "arm_const_structs.h"
gpota 0:2529b7b37e9a 5 #define SAMP_FREQ 2000
gpota 0:2529b7b37e9a 6
gpota 0:2529b7b37e9a 7 InterruptIn button(PC_13);
gpota 0:2529b7b37e9a 8 DigitalOut El(D2);
gpota 0:2529b7b37e9a 9 DigitalOut A(D3);
gpota 0:2529b7b37e9a 10 DigitalOut D(D4);
gpota 0:2529b7b37e9a 11 DigitalOut G(D5);
gpota 0:2529b7b37e9a 12 DigitalOut B(D6);
gpota 0:2529b7b37e9a 13 DigitalOut Eh(D7);
gpota 0:2529b7b37e9a 14 DigitalOut Led_Ok(D9);
gpota 0:2529b7b37e9a 15 DigitalOut Led_Down(D10);
gpota 0:2529b7b37e9a 16 DigitalOut Led_Up(D8);
gpota 0:2529b7b37e9a 17 Serial pc (SERIAL_TX,SERIAL_RX);
gpota 0:2529b7b37e9a 18 AnalogIn analog_value(A0);
gpota 0:2529b7b37e9a 19 const int FFT_LEN = 1024;
gpota 0:2529b7b37e9a 20 const int bins = 1000;
gpota 0:2529b7b37e9a 21
gpota 0:2529b7b37e9a 22 float Ing[FFT_LEN];
gpota 0:2529b7b37e9a 23
gpota 0:2529b7b37e9a 24 const static arm_cfft_instance_f32 *S;
gpota 0:2529b7b37e9a 25
gpota 0:2529b7b37e9a 26 float samples[FFT_LEN*2];
gpota 0:2529b7b37e9a 27 float magnitudes[FFT_LEN];
gpota 0:2529b7b37e9a 28 float freq_window[bins];
gpota 0:2529b7b37e9a 29
gpota 0:2529b7b37e9a 30 int c =0;
gpota 0:2529b7b37e9a 31 void selectNote() {
gpota 0:2529b7b37e9a 32 c++;
gpota 0:2529b7b37e9a 33 if(c==1)
gpota 0:2529b7b37e9a 34 {
gpota 0:2529b7b37e9a 35 El=1;
gpota 0:2529b7b37e9a 36 A=0;
gpota 0:2529b7b37e9a 37 D=0;
gpota 0:2529b7b37e9a 38 G=0;
gpota 0:2529b7b37e9a 39 B=0;
gpota 0:2529b7b37e9a 40 Eh=0;
gpota 0:2529b7b37e9a 41 }
gpota 0:2529b7b37e9a 42 else if(c==2)
gpota 0:2529b7b37e9a 43 {
gpota 0:2529b7b37e9a 44 El=0;
gpota 0:2529b7b37e9a 45 A=1;
gpota 0:2529b7b37e9a 46 D=0;
gpota 0:2529b7b37e9a 47 G=0;
gpota 0:2529b7b37e9a 48 B=0;
gpota 0:2529b7b37e9a 49 Eh=0;
gpota 0:2529b7b37e9a 50
gpota 0:2529b7b37e9a 51 }
gpota 0:2529b7b37e9a 52 else if(c==3)
gpota 0:2529b7b37e9a 53 {
gpota 0:2529b7b37e9a 54 El=0;
gpota 0:2529b7b37e9a 55 A=0;
gpota 0:2529b7b37e9a 56 D=1;
gpota 0:2529b7b37e9a 57 G=0;
gpota 0:2529b7b37e9a 58 B=0;
gpota 0:2529b7b37e9a 59 Eh=0;
gpota 0:2529b7b37e9a 60
gpota 0:2529b7b37e9a 61 }
gpota 0:2529b7b37e9a 62
gpota 0:2529b7b37e9a 63 else if(c==4)
gpota 0:2529b7b37e9a 64 {
gpota 0:2529b7b37e9a 65 El=0;
gpota 0:2529b7b37e9a 66 A=0;
gpota 0:2529b7b37e9a 67 D=0;
gpota 0:2529b7b37e9a 68 G=1;
gpota 0:2529b7b37e9a 69 B=0;
gpota 0:2529b7b37e9a 70 Eh=0;
gpota 0:2529b7b37e9a 71
gpota 0:2529b7b37e9a 72 }
gpota 0:2529b7b37e9a 73
gpota 0:2529b7b37e9a 74 else if(c==5)
gpota 0:2529b7b37e9a 75 {
gpota 0:2529b7b37e9a 76 El=0;
gpota 0:2529b7b37e9a 77 A=0;
gpota 0:2529b7b37e9a 78 D=0;
gpota 0:2529b7b37e9a 79 G=0;
gpota 0:2529b7b37e9a 80 B=1;
gpota 0:2529b7b37e9a 81 Eh=0;
gpota 0:2529b7b37e9a 82
gpota 0:2529b7b37e9a 83 }
gpota 0:2529b7b37e9a 84
gpota 0:2529b7b37e9a 85 else if(c==6)
gpota 0:2529b7b37e9a 86 {
gpota 0:2529b7b37e9a 87 El=0;
gpota 0:2529b7b37e9a 88 A=0;
gpota 0:2529b7b37e9a 89 D=0;
gpota 0:2529b7b37e9a 90 G=0;
gpota 0:2529b7b37e9a 91 B=0;
gpota 0:2529b7b37e9a 92 Eh=1;
gpota 0:2529b7b37e9a 93
gpota 0:2529b7b37e9a 94
gpota 0:2529b7b37e9a 95 }
gpota 0:2529b7b37e9a 96 else if(c==7)
gpota 0:2529b7b37e9a 97 c=0;
gpota 0:2529b7b37e9a 98
gpota 0:2529b7b37e9a 99 }
gpota 0:2529b7b37e9a 100 int main() {
gpota 0:2529b7b37e9a 101 El=0;
gpota 0:2529b7b37e9a 102 A=0;
gpota 0:2529b7b37e9a 103 D=0;
gpota 0:2529b7b37e9a 104 G=0;
gpota 0:2529b7b37e9a 105 B=0;
gpota 0:2529b7b37e9a 106 Eh=0;
gpota 0:2529b7b37e9a 107
gpota 0:2529b7b37e9a 108 El=1;wait(0.1);El=0;
gpota 0:2529b7b37e9a 109 A=1;wait(0.1);A=0;
gpota 0:2529b7b37e9a 110 D=1;wait(0.1);D=0;
gpota 0:2529b7b37e9a 111 G=1;wait(0.1);G=0;
gpota 0:2529b7b37e9a 112 B=1;wait(0.1);B=0;
gpota 0:2529b7b37e9a 113 Eh=1;wait(0.1);Eh=0;
gpota 0:2529b7b37e9a 114 El=1;A=1;D=1;G=1;B=1;Eh=1;wait(0.2);
gpota 0:2529b7b37e9a 115 El=0;A=0;D=0;G=0;B=0;Eh=0;
gpota 0:2529b7b37e9a 116 Led_Down=1;
gpota 0:2529b7b37e9a 117 Led_Up=0;
gpota 0:2529b7b37e9a 118 Led_Ok=0;
gpota 0:2529b7b37e9a 119
gpota 0:2529b7b37e9a 120
gpota 0:2529b7b37e9a 121
gpota 0:2529b7b37e9a 122 int32_t i = 0;
gpota 0:2529b7b37e9a 123 pc.printf("\r\n\r\nFFT test program!\r\n");
gpota 0:2529b7b37e9a 124
gpota 0:2529b7b37e9a 125
gpota 0:2529b7b37e9a 126 // Init arm_ccft_32
gpota 0:2529b7b37e9a 127 switch (FFT_LEN)
gpota 0:2529b7b37e9a 128 {
gpota 0:2529b7b37e9a 129 case 16:
gpota 0:2529b7b37e9a 130 S = & arm_cfft_sR_f32_len16;
gpota 0:2529b7b37e9a 131 break;
gpota 0:2529b7b37e9a 132 case 32:
gpota 0:2529b7b37e9a 133 S = & arm_cfft_sR_f32_len32;
gpota 0:2529b7b37e9a 134 break;
gpota 0:2529b7b37e9a 135 case 64:
gpota 0:2529b7b37e9a 136 S = & arm_cfft_sR_f32_len64;
gpota 0:2529b7b37e9a 137 break;
gpota 0:2529b7b37e9a 138 case 128:
gpota 0:2529b7b37e9a 139 S = & arm_cfft_sR_f32_len128;
gpota 0:2529b7b37e9a 140 break;
gpota 0:2529b7b37e9a 141 case 256:
gpota 0:2529b7b37e9a 142 S = & arm_cfft_sR_f32_len256;
gpota 0:2529b7b37e9a 143 break;
gpota 0:2529b7b37e9a 144 case 512:
gpota 0:2529b7b37e9a 145 S = & arm_cfft_sR_f32_len512;
gpota 0:2529b7b37e9a 146 break;
gpota 0:2529b7b37e9a 147 case 1024:
gpota 0:2529b7b37e9a 148 S = & arm_cfft_sR_f32_len1024;
gpota 0:2529b7b37e9a 149 break;
gpota 0:2529b7b37e9a 150 case 2048:
gpota 0:2529b7b37e9a 151 S = & arm_cfft_sR_f32_len2048;
gpota 0:2529b7b37e9a 152 break;
gpota 0:2529b7b37e9a 153 case 4096:
gpota 0:2529b7b37e9a 154 S = & arm_cfft_sR_f32_len4096;
gpota 0:2529b7b37e9a 155 break;
gpota 0:2529b7b37e9a 156 }
gpota 0:2529b7b37e9a 157 while(1) {
gpota 0:2529b7b37e9a 158 button.rise(&selectNote);
gpota 0:2529b7b37e9a 159 for (int k = 0; k < 1024; k++) {
gpota 1:45d485b8966f 160 Ing[k] = (short) (analog_value.read_u16() - 0x8000);
gpota 0:2529b7b37e9a 161 wait_us(1e6/SAMP_FREQ);
gpota 0:2529b7b37e9a 162 }
gpota 0:2529b7b37e9a 163 //Riempiamo il vettore per la FFT
gpota 0:2529b7b37e9a 164 for(i = 0; i< FFT_LEN*2; i+=2)
gpota 0:2529b7b37e9a 165 {
gpota 0:2529b7b37e9a 166
gpota 0:2529b7b37e9a 167 samples[i] = Ing[i];
gpota 0:2529b7b37e9a 168 samples[i+1] = 0;
gpota 0:2529b7b37e9a 169 }
gpota 0:2529b7b37e9a 170 arm_cfft_f32(S, samples, 0, 1);
gpota 1:45d485b8966f 171 // Calcola la FFT
gpota 0:2529b7b37e9a 172 arm_cmplx_mag_f32(samples, magnitudes, FFT_LEN);
gpota 0:2529b7b37e9a 173
gpota 0:2529b7b37e9a 174 if(c==1){
gpota 0:2529b7b37e9a 175 int max=3;
gpota 0:2529b7b37e9a 176 int j=2;
gpota 0:2529b7b37e9a 177 do{
gpota 0:2529b7b37e9a 178 if (magnitudes[j]>magnitudes[max]&&(j*1000/1024<100)&&(j*1000/1024>30))//restringo la banda a [30Hz, 100Hz]
gpota 0:2529b7b37e9a 179 max=j;
gpota 0:2529b7b37e9a 180 j++;
gpota 0:2529b7b37e9a 181
gpota 0:2529b7b37e9a 182 }while(j<1024/2);
gpota 0:2529b7b37e9a 183
gpota 0:2529b7b37e9a 184 float maxx=(float)max*1000/1024;
gpota 1:45d485b8966f 185
gpota 0:2529b7b37e9a 186 if(maxx<82){
gpota 0:2529b7b37e9a 187 Led_Down=1;
gpota 0:2529b7b37e9a 188 Led_Up=0;
gpota 0:2529b7b37e9a 189 Led_Ok=0;
gpota 0:2529b7b37e9a 190 }
gpota 0:2529b7b37e9a 191 if(maxx>84){
gpota 0:2529b7b37e9a 192 Led_Down=0;
gpota 0:2529b7b37e9a 193 Led_Up=1;
gpota 0:2529b7b37e9a 194 Led_Ok=0;
gpota 0:2529b7b37e9a 195 }
gpota 0:2529b7b37e9a 196 else if(maxx>82.3 &&maxx<=84){
gpota 0:2529b7b37e9a 197 Led_Down=0;
gpota 0:2529b7b37e9a 198 Led_Up=0;
gpota 0:2529b7b37e9a 199 Led_Ok=1;
gpota 0:2529b7b37e9a 200 }
gpota 0:2529b7b37e9a 201 }
gpota 0:2529b7b37e9a 202 //--------------A STRING------------------
gpota 0:2529b7b37e9a 203 if(c==2){
gpota 0:2529b7b37e9a 204 int max=3;
gpota 0:2529b7b37e9a 205 int j=2;
gpota 0:2529b7b37e9a 206 do{
gpota 0:2529b7b37e9a 207
gpota 0:2529b7b37e9a 208 if (magnitudes[j]>magnitudes[max]&&(j*1000/1024<130)&&(j*1000/1024>90))//restringo la banda a [90Hz, 130Hz]
gpota 0:2529b7b37e9a 209 max=j;
gpota 0:2529b7b37e9a 210 j++;
gpota 0:2529b7b37e9a 211
gpota 0:2529b7b37e9a 212 }while(j<1024/2);
gpota 0:2529b7b37e9a 213
gpota 0:2529b7b37e9a 214 float maxx=(float)max*1000/1024;
gpota 0:2529b7b37e9a 215 //pc.printf("Il massimo in ampiezza è alla frequenza %f Hertz \n",(float)max*1000/1024);
gpota 0:2529b7b37e9a 216 if(maxx<110){
gpota 0:2529b7b37e9a 217 Led_Down=1;
gpota 0:2529b7b37e9a 218 Led_Up=0;
gpota 0:2529b7b37e9a 219 Led_Ok=0;
gpota 0:2529b7b37e9a 220 }
gpota 0:2529b7b37e9a 221 if(maxx>112){
gpota 0:2529b7b37e9a 222 Led_Down=0;
gpota 0:2529b7b37e9a 223 Led_Up=1;
gpota 0:2529b7b37e9a 224 Led_Ok=0;
gpota 0:2529b7b37e9a 225 }
gpota 0:2529b7b37e9a 226 else if(maxx>110 &&maxx<113){
gpota 0:2529b7b37e9a 227 Led_Down=0;
gpota 0:2529b7b37e9a 228 Led_Up=0;
gpota 0:2529b7b37e9a 229 Led_Ok=1;
gpota 0:2529b7b37e9a 230 }
gpota 0:2529b7b37e9a 231 }
gpota 0:2529b7b37e9a 232 //--------------D STRING------------------
gpota 0:2529b7b37e9a 233 if(c==3){
gpota 0:2529b7b37e9a 234 int max=3;
gpota 0:2529b7b37e9a 235 int j=2;
gpota 0:2529b7b37e9a 236 do{
gpota 0:2529b7b37e9a 237
gpota 1:45d485b8966f 238 if (magnitudes[j]>magnitudes[max]&&(j*1000/1024<160)&&(j*1000/1024>130))//restringo la banda a [130Hz, 160Hz]
gpota 0:2529b7b37e9a 239 max=j;
gpota 0:2529b7b37e9a 240 j++;
gpota 0:2529b7b37e9a 241
gpota 0:2529b7b37e9a 242 }while(j<1024/2);
gpota 0:2529b7b37e9a 243
gpota 0:2529b7b37e9a 244 float maxx=(float)max*1000/1024;
gpota 0:2529b7b37e9a 245 //pc.printf("Il massimo in ampiezza è alla frequenza %f Hertz \n",(float)max*1000/1024);
gpota 0:2529b7b37e9a 246 if(maxx<147){
gpota 0:2529b7b37e9a 247 Led_Down=1;
gpota 0:2529b7b37e9a 248 Led_Up=0;
gpota 0:2529b7b37e9a 249 Led_Ok=0;
gpota 0:2529b7b37e9a 250 }
gpota 0:2529b7b37e9a 251 if(maxx>149){
gpota 0:2529b7b37e9a 252 Led_Down=0;
gpota 0:2529b7b37e9a 253 Led_Up=1;
gpota 0:2529b7b37e9a 254 Led_Ok=0;
gpota 0:2529b7b37e9a 255 }
gpota 0:2529b7b37e9a 256 else if(maxx>147 &&maxx<149){
gpota 0:2529b7b37e9a 257 Led_Down=0;
gpota 0:2529b7b37e9a 258 Led_Up=0;
gpota 0:2529b7b37e9a 259 Led_Ok=1;
gpota 0:2529b7b37e9a 260 }
gpota 0:2529b7b37e9a 261 }
gpota 0:2529b7b37e9a 262 //--------------G STRING------------------
gpota 0:2529b7b37e9a 263 if(c==4){
gpota 0:2529b7b37e9a 264 int max=3;
gpota 0:2529b7b37e9a 265 int j=2;
gpota 0:2529b7b37e9a 266 do{
gpota 0:2529b7b37e9a 267
gpota 1:45d485b8966f 268 if (magnitudes[j]>magnitudes[max]&&(j*1000/1024<210)&&(j*1000/1024>170))//restringo la banda a [170Hz, 210Hz]
gpota 0:2529b7b37e9a 269 max=j;
gpota 0:2529b7b37e9a 270 j++;
gpota 0:2529b7b37e9a 271
gpota 0:2529b7b37e9a 272 }while(j<1024/2);
gpota 0:2529b7b37e9a 273
gpota 0:2529b7b37e9a 274 float maxx=(float)max*1000/1024;
gpota 0:2529b7b37e9a 275 //pc.printf("Il massimo in ampiezza è alla frequenza %f Hertz \n",(float)max*1000/1024);
gpota 0:2529b7b37e9a 276 if(maxx<196){
gpota 0:2529b7b37e9a 277 Led_Down=1;
gpota 0:2529b7b37e9a 278 Led_Up=0;
gpota 0:2529b7b37e9a 279 Led_Ok=0;
gpota 0:2529b7b37e9a 280 }
gpota 0:2529b7b37e9a 281 if(maxx>203){
gpota 0:2529b7b37e9a 282 Led_Down=0;
gpota 0:2529b7b37e9a 283 Led_Up=1;
gpota 0:2529b7b37e9a 284 Led_Ok=0;
gpota 0:2529b7b37e9a 285 }
gpota 0:2529b7b37e9a 286 else if(maxx>=196 &&maxx<=197.5){
gpota 0:2529b7b37e9a 287 Led_Down=0;
gpota 0:2529b7b37e9a 288 Led_Up=0;
gpota 0:2529b7b37e9a 289 Led_Ok=1;
gpota 0:2529b7b37e9a 290 }
gpota 0:2529b7b37e9a 291 }
gpota 0:2529b7b37e9a 292 //--------------B STRING------------------
gpota 0:2529b7b37e9a 293 if(c==5){
gpota 0:2529b7b37e9a 294 int max=3;
gpota 0:2529b7b37e9a 295 int j=2;
gpota 0:2529b7b37e9a 296 do{
gpota 0:2529b7b37e9a 297
gpota 1:45d485b8966f 298 if (magnitudes[j]>magnitudes[max]&&(j*1000/1024<260)&&(j*1000/1024>230))//restringo la banda a [230Hz, 260Hz]
gpota 0:2529b7b37e9a 299 max=j;
gpota 0:2529b7b37e9a 300 j++;
gpota 0:2529b7b37e9a 301
gpota 0:2529b7b37e9a 302 }while(j<1024/2);
gpota 0:2529b7b37e9a 303
gpota 0:2529b7b37e9a 304 float maxx=(float)max*1000/1024;
gpota 0:2529b7b37e9a 305 //pc.printf("Il massimo in ampiezza è alla frequenza %f Hertz \n",(float)max*1000/1024);
gpota 0:2529b7b37e9a 306 if(maxx<244){
gpota 0:2529b7b37e9a 307 Led_Down=1;
gpota 0:2529b7b37e9a 308 Led_Up=0;
gpota 0:2529b7b37e9a 309 Led_Ok=0;
gpota 0:2529b7b37e9a 310 }
gpota 0:2529b7b37e9a 311 if(maxx>250){
gpota 0:2529b7b37e9a 312 Led_Down=0;
gpota 0:2529b7b37e9a 313 Led_Up=1;
gpota 0:2529b7b37e9a 314 Led_Ok=0;
gpota 0:2529b7b37e9a 315 }
gpota 0:2529b7b37e9a 316 else if(maxx>246 &&maxx<250){
gpota 0:2529b7b37e9a 317 Led_Down=0;
gpota 0:2529b7b37e9a 318 Led_Up=0;
gpota 0:2529b7b37e9a 319 Led_Ok=1;
gpota 0:2529b7b37e9a 320 }
gpota 0:2529b7b37e9a 321 }
gpota 0:2529b7b37e9a 322 //--------------Eh STRING------------------
gpota 0:2529b7b37e9a 323 if(c==6){
gpota 0:2529b7b37e9a 324 int max=3;
gpota 0:2529b7b37e9a 325 int j=2;
gpota 0:2529b7b37e9a 326 do{
gpota 0:2529b7b37e9a 327
gpota 1:45d485b8966f 328 if (magnitudes[j]>magnitudes[max]&&(j*1000/1024<340)&&(j*1000/1024>310))//restringo la banda a [340Hz, 310Hz]
gpota 0:2529b7b37e9a 329 max=j;
gpota 0:2529b7b37e9a 330 j++;
gpota 0:2529b7b37e9a 331
gpota 0:2529b7b37e9a 332 }while(j<1024/2);
gpota 0:2529b7b37e9a 333
gpota 0:2529b7b37e9a 334 float maxx=(float)max*1000/1024;
gpota 0:2529b7b37e9a 335 //pc.printf("Il massimo in ampiezza è alla frequenza %f Hertz \n",(float)max*1000/1024);
gpota 0:2529b7b37e9a 336 if(maxx<327){
gpota 0:2529b7b37e9a 337 Led_Down=1;
gpota 0:2529b7b37e9a 338 Led_Up=0;
gpota 0:2529b7b37e9a 339 Led_Ok=0;
gpota 0:2529b7b37e9a 340 }
gpota 0:2529b7b37e9a 341 if(maxx>334){
gpota 0:2529b7b37e9a 342 Led_Down=0;
gpota 0:2529b7b37e9a 343 Led_Up=1;
gpota 0:2529b7b37e9a 344 Led_Ok=0;
gpota 0:2529b7b37e9a 345 }
gpota 0:2529b7b37e9a 346 else if(maxx>330.3 &&maxx<333.5){
gpota 0:2529b7b37e9a 347 Led_Down=0;
gpota 0:2529b7b37e9a 348 Led_Up=0;
gpota 0:2529b7b37e9a 349 Led_Ok=1;
gpota 0:2529b7b37e9a 350 }
gpota 0:2529b7b37e9a 351 }
gpota 0:2529b7b37e9a 352
gpota 0:2529b7b37e9a 353
gpota 0:2529b7b37e9a 354 }
gpota 0:2529b7b37e9a 355 }
gpota 0:2529b7b37e9a 356