hi,i am new into the programming. i am struggling to perform the cross correlation of analogread of two

sound sensor data.

could somebody help me write the code

hi,i am new into the programming. i am struggling to perform the cross correlation of analogread of two

sound sensor data.

could somebody help me write the code

,i am new into the programming

Cross correlation is not a thing that a beginner should be doing.

What exactly are you trying to do? Is it to do with trying to spot if a sound is the same as a pre-recorded sound? If so cross correlation is only part of the technique you need to use. It is a very complex problem.

i am trying to find the location of unknown sound source ( the sound can be clap or speech ).So, i am going to

use CC in order to find a time delay between the microphone.

See:- Acoustic direction finding using Microphone - Project Guidance - Arduino Forum

But what you really want is one of these:-https://uk.farnell.com/matrix-labs/matrix-voice-esp/dev-kit-iot-voice-recognition/dp/2846913

This is a review of one. Matrix Voice review — The MagPi magazine

If you can set up a microphone so that only claps are detected, just start a timer running when the first microphone registers, and stop it when the second microphone registers.

makibra:

i am trying to find the location of unknown sound source ( the sound can be clap or speech ).So, i am going touse CC in order to find a time delay between the microphone.

Will it make a big difference to your technique if the samples from the different sources are more than 100us apart?

no ii won't make since i am taking at most one sample every 112 µs. and for alternating between the

channels,on each channel i get one sample every 224 µs.

i understand the CC in theory but the implement in term of code is what i am struggling . i have done research

the initial part of the code which consists reading the value via by analogread and store them into array

the second part is to perform the cc but i couldn't do it that part

could someone help me ?

the implement in term of code is what i am struggling

Post the code you have and explain the problems. See the "How to use this forum" post for instructions.

A web search on "cross correlation c source code" gives this (among other stuff): Cross Correlation

This particular implementation is biased, so it won't do well if the delay range of interest is large relative to the number of samples. That probably not an issue for this application as I understand it.

The problem with using cross correlation for detecting the angle of approach of a sound is that the result is ambiguous.

Imagine if you get that the angle is 45 degrees, you don’t know if that is 45 degrees to the left or right.

the code i have .there is not cross correlation in it .

to tell whether it is the angle right or left, we can say if the cross correlation is positive then the angle upward

to the right or if the cc is negative ,to the left. By using if statement i think we can do it

To find the location of a sound, isn't it easier to use 3 microphones at the top of a triangle ? A correlation function

could localize the sound generation inside the triangle.

you can use 3 microphones in triangular shape but i think the angle will be more 180 degree

Grumpy_Mike:

The problem with using cross correlation for detecting the angle of approach of a sound is that the result is ambiguous.Imagine if you get that the angle is 45 degrees, you don’t know if that is 45 degrees to the left or right.

Two receive elements resolve to a hyperbola (approximately a cone at range, e.g. 45 degrees) rotated around the axis along which the receive elements are located. In a two dimensional horizontal plane geometry this is a front/back ambiguity. If the microphones are directional, one might be able to rule out the back ambiguity in practice.

makibra:

the code i have .there is not cross correlation in it .

Huh?

ard_newbie:

To find the location of a sound, isn’t it easier to use 3 microphones at the top of a triangle ? A correlation function

could localize the sound generation inside the triangle.

I don’t know about “easier”, but 3 microphones so arranged can give the direction of arrival in 3 dimensions, albeit still with a front/back ambiguity.

MrMark:

I don’t know about “easier”, but 3 microphones so arranged can give the direction of arrival in 3 dimensions, albeit still with a front/back ambiguity.

Are you sure of that ?

Are you sure of that ?

Very common approach. Look up trilateration.

The ambiguity is with respect to the mirror plane containing the three microphones.

```
function [lags,ck,cc,td] = xcorrTD(x,y)
%
% TIME-DOMAIN CROSS-CORRELATION FUNCTION
%
% Description: xcorrTD takes two discrete time signals as input and
% calculates cross-correlation values, cross-correlation correlation
% coefficients and delay between two signals. The computation is
% performed in the time domain. The results of xcorrTD is validated
% against the MatLAB's xcorr function.
%
% For cross-correlation in frequency domain see xcorrFD.
%
% Syntax:
% [lags,ck,cc,td] = xcorrTD(x,y)
%
% Input:
% x = input signal 1 (must be a Nx1 or 1xN vector)
%
% y = input signal 2 (must be a Nx1 or 1xN vector)
%
% Output:
%
% lags = a vector of lags with a length of 2xN-1 (N = number of
% data points in signal x or y)
%
% ck = cross-correlation values (MatLAB xcorr gives them as
% output)
%
% cc = correlation coefficients
%
% td = delay (i.e., number of lags) between two signals
%
% Reference: Gubbins, D. (2004). Time Series Analysis and Inverse Theory
% for Geophysicists, Cambridge University Press, 255 p.
%
% Example: Input acceleration signals are base and roof motions from a
% building during earthquake shaking. Let us compute the delay between
% two signals, which can be used to compute average shear-wave velocity
% in the building.
%
% data = importdata('input.txt');
% x = data(:,2); y = data(:,3);
% [lag,ck,cc,td] = xcorrTD(x,y);
% plot(lag,ck,'r'); grid on;
% xlabel('Lag'); ylabel('Cross correlation values');
% title('Cross-correlation in time domain by xcorrTD');
% print('-dpng','-painters','-r600','xcorrTD.png')
%
% THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
% NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY DIRECT, INDIRECT,
% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
% OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
% ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
% TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
% USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
% DAMAGE.
%
% Written by Dr. Erol Kalkan, P.E. (ekalkan@usgs.gov)
% $Revision: 1.0 $ $Date: 2017/06/14 14:03:00 $
ndata = length(x);
sx = sum(x.^2); sy = sum(y.^2);
maxlag = ndata-1;
cnt = 0;
for lag = -maxlag:maxlag
cnt = cnt + 1;
sxy = 0;
for i=1:ndata
j = i + lag;
if j>0 && j<=ndata
sxy = sxy + x(i) * y(j);
end
end
cc(cnt) = sxy / sqrt(sx*sy); % correlation coefficient
ck(cnt) = sxy; % cross-correlation value
lags(cnt) = lag;
end
[~,i] = max(cc);
td = i - ndata;
```

Hi,

this is my code but i am really struggling the cross correlation.please help me to convert the matlab

code into arduino.

```
#define LED_left 2
#define LED_right 4
#define left_mic A0
#define right_mic A2
const unsigned int numReadings = 100;
unsigned int analogVals1[numReadings];
unsigned int analogVals2[numReadings];
unsigned int i = 0;
void setup() {
pinMode(LED_left, OUTPUT);
digitalWrite(LED_left, LOW);
pinMode(LED_right, OUTPUT);
digitalWrite(LED_right, LOW);
Serial.begin(115200);
}
void loop() {
int left = analogRead(left_mic);
Serial.println(String(left));
int right = analogRead(right_mic);
Serial.println(String(right));
static uint32_t tStart = millis(); // ms; start time
const uint32_t DESIRED_PERIOD = 1000; // ms
uint32_t tNow = millis(); // ms; time now
if (tNow - tStart >= DESIRED_PERIOD)
{
tStart += DESIRED_PERIOD; // update start time to ensure consistent and near-exact period
Serial.println("taking sample");
analogVals1[i] = analogRead(left_mic);
analogVals2[i] = analogRead(right_mic);
i++;
if (i>=numReadings)
{
i = 0; //reset to beginning of array, so you don't try to save readings outside of the bounds of the array
}
}
}
```