Advertisement
Guest User

code leds jack

a guest
Jun 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.53 KB | None | 0 0
  1.  
  2.  
  3.  
  4. #define  RCV_TIM_LIMIT  10000000
  5. #define MY_LEDV  34
  6. #define MY_LEDJ  35
  7. #define GIL_SW2  11
  8. #define GIL_SW1  12
  9. #define GIL_BUTTON 10
  10.  
  11.  
  12.  
  13.  
  14. #define BUF_SIZE  1000     // size of the buffer containing the input samples. Must be greater than the order of the filters + K
  15.  
  16. #define KP 50             // number of samples for the estimation of the signal power
  17. #define HMIO  109         // order of the first filter
  18. #define HSOLO  244         // order of the second filter
  19.  
  20. #define S 50000           // Threshold applied to the signal power  for the signal detection        
  21.  
  22.  
  23. unsigned int  Ts=200;     // Sampling step in microseconds (sampling frequency = 5 KHz)
  24.  
  25. float buf[BUF_SIZE];      // Buffer containing the input samples
  26. float pIn;                // input power
  27. float y;                  // filtered signal
  28. float pOut;
  29. float g;                  // computed pOut/pIn ratio
  30. float Sg = 0.1;           // Threshod applied to g for the frequency detection
  31.  
  32. int compteur = 0;
  33. int detect;
  34.  
  35. unsigned long current_time = 0;
  36. unsigned long next_sample_time;
  37.  
  38.  
  39. // Filters :
  40. float HMI[HMIO] = {-0.03689,0.01497,0.01038,0.00644,0.00388,0.00312,0.00408,0.00627,0.00885,0.01087,0.01150,0.01021,0.00696,0.00218,-0.00332,-0.00845,-0.01212,-0.01347,-0.01207,-0.00797,-0.00183,0.00530,0.01207,0.01714,0.01945,0.01835,0.01386,0.00658,-0.00228,-0.01119,-0.01855,-0.02292,-0.02338,-0.01968,-0.01230,-0.00241,0.00831,0.01796,0.02478,0.02746,0.02538,0.01878,0.00871,-0.00316,-0.01473,-0.02400,-0.02917,-0.02937,-0.02444,-0.01515,-0.00314,0.00950,0.02058,0.02815,0.03084,0.02815,0.02058,0.00950,-0.00314,-0.01515,-0.02444,-0.02937,-0.02917,-0.02400,-0.01473,-0.00316,0.00871,0.01878,0.02538,0.02746,0.02478,0.01796,0.00831,-0.00241,-0.01230,-0.01968,-0.02338,-0.02292,-0.01855,-0.01119,-0.00228,0.00658,0.01386,0.01835,0.01945,0.01714,0.01207,0.00530,-0.00183,-0.00797,-0.01207,-0.01347,-0.01212,-0.00845,-0.00332,0.00218,0.00696,0.01021,0.01150,0.01087,0.00885,0.00627,0.00408,0.00312,0.00388,0.00644,0.01038,0.01497,-0.03689};
  41.  
  42.  
  43. float HSOL[HSOLO] = {-0.00093,0.04121,0.00656,-0.00444,-0.01140,-0.00728,0.00490,0.01333,0.00885,-0.00524,-0.01530,-0.01053,0.00547,0.01723,0.01230,-0.00557,-0.01909,-0.01413,0.00553,0.02084,0.01597,-0.00534,-0.02243,-0.01780,0.00500,0.02383,0.01955,-0.00451,-0.02500,-0.02120,0.00390,0.02590,0.02273,-0.00318,-0.02651,-0.02406,0.00231,0.02684,0.02516,-0.00143,-0.02687,-0.02600,0.00049,0.02658,0.02658,0.00049,-0.02600,-0.02687,-0.00143,0.02516,0.02684,0.00231,-0.02406,-0.02651,-0.00318,0.02273,0.02590,0.00390,-0.02120,-0.02500,-0.00451,0.01955,0.02383,0.00500,-0.01780,-0.02243,-0.00534,0.01597,0.02084,0.00553,-0.01413,-0.01909,-0.00557,0.01230,0.01723,0.00547,-0.01053,-0.01530,-0.00524,0.00885,0.01333,0.00490,-0.00728,-0.01140,-0.00444,0.00656,0.04121,-0.00093};
  44.  
  45.  
  46.  
  47.  
  48.  
  49. void setup() {
  50.  
  51.   int i,n;
  52.  
  53.   // initialize  serial ports:
  54.  
  55.   Serial.begin(9600);
  56.   Serial1.begin(9600);
  57.   pinMode(MY_LEDJ, OUTPUT);
  58.   pinMode(MY_LEDV, OUTPUT);
  59.   pinMode(RED_LED, OUTPUT);
  60.   pinMode(GIL_SW1, INPUT_PULLUP);
  61.   pinMode(GIL_SW2, INPUT_PULLUP);
  62.   pinMode(GIL_BUTTON, INPUT_PULLUP);
  63.  
  64.  
  65.   // Switch on the LEDs
  66.   digitalWrite(MY_LEDJ, HIGH);
  67.   digitalWrite(MY_LEDV, HIGH);
  68.  
  69.   // Initialize buffer and power
  70.   for (i = 0; i < BUF_SIZE; i++)
  71.   {
  72.     buf[i]  = 0;
  73.   }
  74.  
  75.   pIn = 0;
  76.   pOut = 0;
  77.   detect = 0;
  78.   g = 0;
  79.   y = 0;
  80.   delay(1);
  81.  
  82.   // switch off LEDs  
  83.   digitalWrite(MY_LEDJ, LOW);
  84.   digitalWrite(MY_LEDV, LOW);
  85.  
  86.  
  87.   // Define the next smapling time
  88.   next_sample_time = micros() + (unsigned long)Ts;
  89.  
  90. }
  91.  
  92.  
  93. void loop() {
  94.  
  95.   int i,n;
  96.  
  97.  
  98.   // Read data : shift the previous samples in the buffer ...
  99.   for (i = BUF_SIZE - 2; i >= 0; i--)
  100.   {
  101.     buf[i + 1] = buf[i];
  102.   }
  103.   // ... and acquire the new sample
  104.   current_time = micros() ;
  105.   while (current_time < next_sample_time)
  106.   {
  107.     current_time = micros() ;
  108.   }
  109.   buf[0] = (float) analogRead(23) - 2048.0;
  110.  
  111.   // Define the next sampling time
  112.   next_sample_time += (unsigned long)Ts;
  113.  
  114.  
  115.   // Update "instantaneous" power
  116.   pIn = 0;
  117.   for (i = 0; i < KP; i++)
  118.   {
  119.     pIn = pIn + buf[i] * buf[i];
  120.   }
  121.   pIn = pIn / (float)KP;
  122.  
  123.  
  124.   // Test the amplitude of the instantaneous power
  125.   if (pIn > S)
  126.   {
  127.     detect++;
  128.     digitalWrite(MY_LEDJ, HIGH);   // Switch on the LED to indicate that an audio signal is detected
  129.  
  130.  
  131.     // Apply filter and compute output power
  132.     if (detect == 200)
  133.     {
  134.       detect  = 0;
  135.  
  136.       pOut = 0;
  137.  
  138.       for (i = 0; i < KP; i++)
  139.       {
  140.         y = 0;
  141.         // For the detection of a G
  142.         for (n = 0; n < HMIO; n++)   // Filter
  143.         {
  144.            y = y + (buf[i + n] * HMI[n]);
  145.         }
  146.  
  147.         // For the detection of a A
  148.    //     for (n = 0; n < HSOLO; n++)   // Filter
  149.     //    {
  150.      //     y = y + (buf[i + n] * HSOL[n]);
  151.       //  }
  152.  
  153.         pOut  = pOut + y * y;
  154.       }
  155.       pOut = pOut / (float)KP ;
  156.      
  157.       // Compute the ratio between the signal power and the filtered signal power
  158.       g = pOut / pIn;
  159.  
  160.      
  161.       // The tone is detected if pOut > Sg * Pin      
  162.       if (g > Sg)
  163.       {
  164.         digitalWrite(MY_LEDV, HIGH);    // Switch on the led to indicate that the tone has been detected
  165.         //Serial.println("Display power ratio:");
  166.         //Serial.println(g * 1000);
  167.       }
  168.       else
  169.       {
  170.         digitalWrite(MY_LEDV, LOW);    // Switch off the led to indicate that the tone is not still detected
  171.       }
  172.     }
  173.   }
  174.   else      //  No useful audio signal detected
  175.   {
  176.       digitalWrite(MY_LEDJ, LOW);
  177.       digitalWrite(MY_LEDV, LOW);
  178.   }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement