localization

i’m having trouble using with sparkfun electret microphone.is there a possible way that a microphone would only receive a input when i say something and not taking to much pointless noise.is there a way to do this.?when it receive an input it will correlate and find angle.how do i do this?is there an improvement that i should do in my code.?to pointing in my code would be a great help.thank you and this is my source of help https://www2.informatik.uni-hamburg.de/ … _ulm04.pdf

#include "mbed.h"
#include "math.h"

#define SAMPLE_PERIOD 60
#define SAMPLE 1024
#define PI  3.1416
#define freq 44100
#define sos 384 //m/s
#define WINDOWSIZE 128

Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);
AnalogIn  rightmic(p16);
AnalogIn  leftmic(p19);
BusOut    unused(p15,p17,p18,p20); 
Timer t;

double max1,min1; 
double max,min;
float left_results[SAMPLE];
float right_results[SAMPLE];
float timesampling;
int peakoffset;
int count;

int correlate(float left_results[] , float right_results[])
{
    float peakCorrelation = 0;
    int peakoffset = 0;
    float correlation;
    int firstLeftSample = SAMPLE/ 2 - WINDOWSIZE/2; // the first sample in the left data such that our window will be centered in the middle.
    int timeoffset = -firstLeftSample ;//minimum time test
    while ( (timeoffset + firstLeftSample  + WINDOWSIZE ) < SAMPLE) 
    {
        correlation = 0;
        for (int i = 0; i<WINDOWSIZE ; i++) 
        {
        correlation += left_results[firstLeftSample  + i] * right_results[firstLeftSample + timeoffset +i];
        }
        if (correlation  > peakCorrelation) // look for the peak..
        { 
        peakCorrelation = correlation;
        peakoffset = timeoffset;
        timeoffset++;
        }
        pc.printf("lag=%d",peakoffset);
    }
  return peakoffset;
}
int angle(int peakoffset)
{
    float c=0.15;//distance between two mics...
    float distance=(peakoffset*timesampling)*sos;//distance of unknown which is the speaker or length of 'a'..
    int theta=asin(distance/c)*180/PI;    //phase of the speaker   
    return theta;
}
int main() 
{   
    max = 0.0;
    min = 3.3;
    t.start();
    for (int i=0; i<SAMPLE; i++)
    {
        while(t.read_ms()<i*SAMPLE_PERIOD)// wait until the next sample time....
        {
            left_results[i]=leftmic.read();
            right_results[i] = rightmic.read();
        
            if (right_results[i] > max) 
                max = right_results[i];
            if (right_results[i] < min) 
                min = right_results[i];
            if (left_results[i] > max) 
                max = left_results[i];
            if (left_results[i] < min) 
                min = left_results[i];        
        }
    
    t.reset();
    max=max*3.3;
    min=min*3.3;
    pc.printf(" max=%0.3f, min=%0.3f\n\r",max,min);
    max=0.0;
    min=3.3;
    } 
    correlate(left_results , right_results);   
}

“Pointless noise” is a fact of life. Welcome to the real world. In some cases it is possible to filter out some of the noise, but in such cases, detailed knowledge of the expected signal is required.

You have chosen a challenging project.

Remington is right. Noise cancellation is a tough subject. Search this forum and others (search term “localization”) for many, many threads.

To do localization, you need more than one mic, 3 is best.