Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define RCV_TIM_LIMIT 10000000
- #define MY_LEDV 34
- #define MY_LEDJ 35
- #define GIL_SW2 11
- #define GIL_SW1 12
- #define GIL_BUTTON 10
- #define BUF_SIZE 1000 // size of the buffer containing the input samples. Must be greater than the order of the filters + K
- #define KP 50 // number of samples for the estimation of the signal power
- #define HMIO 109 // order of the first filter
- #define HSOLO 244 // order of the second filter
- #define S 50000 // Threshold applied to the signal power for the signal detection
- unsigned int Ts=200; // Sampling step in microseconds (sampling frequency = 5 KHz)
- float buf[BUF_SIZE]; // Buffer containing the input samples
- float pIn; // input power
- float y; // filtered signal
- float pOut;
- float g; // computed pOut/pIn ratio
- float Sg = 0.1; // Threshod applied to g for the frequency detection
- int compteur = 0;
- int detect;
- unsigned long current_time = 0;
- unsigned long next_sample_time;
- // Filters :
- 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};
- 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};
- void setup() {
- int i,n;
- // initialize serial ports:
- Serial.begin(9600);
- Serial1.begin(9600);
- pinMode(MY_LEDJ, OUTPUT);
- pinMode(MY_LEDV, OUTPUT);
- pinMode(RED_LED, OUTPUT);
- pinMode(GIL_SW1, INPUT_PULLUP);
- pinMode(GIL_SW2, INPUT_PULLUP);
- pinMode(GIL_BUTTON, INPUT_PULLUP);
- // Switch on the LEDs
- digitalWrite(MY_LEDJ, HIGH);
- digitalWrite(MY_LEDV, HIGH);
- // Initialize buffer and power
- for (i = 0; i < BUF_SIZE; i++)
- {
- buf[i] = 0;
- }
- pIn = 0;
- pOut = 0;
- detect = 0;
- g = 0;
- y = 0;
- delay(1);
- // switch off LEDs
- digitalWrite(MY_LEDJ, LOW);
- digitalWrite(MY_LEDV, LOW);
- // Define the next smapling time
- next_sample_time = micros() + (unsigned long)Ts;
- }
- void loop() {
- int i,n;
- // Read data : shift the previous samples in the buffer ...
- for (i = BUF_SIZE - 2; i >= 0; i--)
- {
- buf[i + 1] = buf[i];
- }
- // ... and acquire the new sample
- current_time = micros() ;
- while (current_time < next_sample_time)
- {
- current_time = micros() ;
- }
- buf[0] = (float) analogRead(23) - 2048.0;
- // Define the next sampling time
- next_sample_time += (unsigned long)Ts;
- // Update "instantaneous" power
- pIn = 0;
- for (i = 0; i < KP; i++)
- {
- pIn = pIn + buf[i] * buf[i];
- }
- pIn = pIn / (float)KP;
- // Test the amplitude of the instantaneous power
- if (pIn > S)
- {
- detect++;
- digitalWrite(MY_LEDJ, HIGH); // Switch on the LED to indicate that an audio signal is detected
- // Apply filter and compute output power
- if (detect == 200)
- {
- detect = 0;
- pOut = 0;
- for (i = 0; i < KP; i++)
- {
- y = 0;
- // For the detection of a G
- for (n = 0; n < HMIO; n++) // Filter
- {
- y = y + (buf[i + n] * HMI[n]);
- }
- // For the detection of a A
- // for (n = 0; n < HSOLO; n++) // Filter
- // {
- // y = y + (buf[i + n] * HSOL[n]);
- // }
- pOut = pOut + y * y;
- }
- pOut = pOut / (float)KP ;
- // Compute the ratio between the signal power and the filtered signal power
- g = pOut / pIn;
- // The tone is detected if pOut > Sg * Pin
- if (g > Sg)
- {
- digitalWrite(MY_LEDV, HIGH); // Switch on the led to indicate that the tone has been detected
- //Serial.println("Display power ratio:");
- //Serial.println(g * 1000);
- }
- else
- {
- digitalWrite(MY_LEDV, LOW); // Switch off the led to indicate that the tone is not still detected
- }
- }
- }
- else // No useful audio signal detected
- {
- digitalWrite(MY_LEDJ, LOW);
- digitalWrite(MY_LEDV, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement