Please edit your posts to add code tags ("</>" button), as described in the "How to use this forum" post.
i edited my post sir
please help me to convert the matlab code into arduino
Why not use the code linked in reply #9?
The MATLAB code does not subtract the mean values of the input arrays, so it may not even work with your data.
i used that code but i don't get any results
#define left_mic A0
#define right_mic A2
const unsigned int numReadings = 100;
unsigned int analogVals1[numReadings];
unsigned int analogVals2[numReadings];
int i,j;
int lags,maxdelay;
double mx,my,sx,sy,sxy,denom,r;
void setup()
{
Serial.begin(115200);
}
void loop()
{
int left = analogRead(left_mic);
Serial.println(String(left));
int right = analogRead(right_mic);
Serial.println(String(right));
Serial.print(r);
for(int i = 0; i < numReadings; i++)
{
analogVals1[i] = analogRead(left_mic);
analogVals2[i] = analogRead(right_mic);
}
}
void cross_correlation()
{
/*Calculate the mean of the two series analogVals1[], analogVals2[]*/
mx = 0;
my = 0;
for(i=0;i< numReadings;i++)
{
mx += analogVals1[i];
my += analogVals2[i];
}
mx /= numReadings;
my /= numReadings;
/* Calculate the denominator */
sx = 0;
sy = 0;
for(i=0;i< numReadings;i++)
{
sx += (analogVals1[i]-mx)*(analogVals1[i]-mx);
sy += (analogVals2[i]-my)*(analogVals2[i]-my);
}
denom = sqrt(sx*sy);
/* Calculate the correlation */
maxdelay = numReadings-1;
for (lags=-maxdelay;lags<maxdelay;lags++)
{
sxy = 0;
for(i=0;i<numReadings;i++)
{
j = i+lags;
if( j<0 || j<=numReadings)
sxy += (analogVals1[i]-mx)*(-my);
else
sxy += (analogVals1[i]-mx)*(analogVals2[j]-my);
}
}
r = sxy / denom;
}
The variable "r" is the correlation coefficient corresponding to each offset in the variable "lags". Print them.
Most people use Serial.print() to print results. But then, your program does not call cross_correlation(), so there are no results to print.
These two lines do nothing:
analogVals1[i];
analogVals1[i];
You forgot to include these two lines from the original source code:
mx /= n;
my /= n;
There are probably lots of other errors, because you are not paying attention.
i changed my post and i include the result
makibra:
i changed my post and i include the result
Please don't so that again - it makes nonsense of the replies.
Your revised program still does not call the function cross_correlation(). But, that function won't work properly anyway, because it contains several errors. Compare your code very carefully to that posted in the link (reply *9).
Note, however, that the code posted is neither a complete program nor a complete function and won't compile as is.
Minor points:
What is "String" doing for you here?
int left = analogRead(left_mic);
Serial.println(String(left));
Why print "r" before reading in the data?
Serial.print(r);
for (int i = 0; i < numReadings; i++)
{
analogVals1[i] = analogRead(left_mic);
analogVals2[i] = analogRead(right_mic);
}
