# [SOLVED]Samples by samples cross-correlation(Xcorr)

Status
Not open for further replies.

#### electricalpeople

##### Newbie level 6
Hi, I am using the xcorr function for identifying the similarity of the signals. the following is the code,
r1 = max(abs(xcorr(S1, shat1,'coeff')));
r2 = max(abs(xcorr(S1,shat2,'coeff')));
if r1>r2
dn=shat2;
else
dn=shat1;
end
But the problem is the signals are having 40,000 samples each. Practically I do get a lot delay. I have to send bunch of samples (like 250samples)into the xcorr for getting rid of the delay. But how do I do that? I know that I have to use a for loop, but found difficult in doing that. Can some one suggest me how do I do that.

---------- Post added at 15:12 ---------- Previous post was at 15:04 ----------

I tried something like this
for i=1:250:40000
r1 = max(abs(xcorr(S1,i), shat1,i),'coeff'))); but totally lost. Someone suggest something please....

#### zorro

Hi electricalpeople,

Is the problem the time it takes for the calculation?
You can accelerate the time not performing the correlation over all possible lags but only a part, but you must have an idea of the possible time delay that makes S1 similar to one of the other signals.
Alternatively, you can truncate S1 or the other two, but the estimation will be not so good.
Can you tell where the signals come from?
Regards

Z

#### electricalpeople

##### Newbie level 6
yes, I am bothered about the time it takes for processing the entire 40,000 samples at a time. I am working on the blind source separation. I have an ouput signal which is
shat(which is 2x40000). shat(1, is the first signal and shat(2, is the other signal. I have the input signals S1 and S2 which I am mixing them to get X1 and X1 which are fed into the BSS. The output of the BSS are the estimations of the S1 and S1. Now one of the shat is similar to the input signal S1 and I found out this using the cross-correlation(matlab function) But since they 40,000 samples each this occurs a delay in practical implementation but not in simulation. So I need to feed some 250 samples into the xcorr function using a for loop rather than just using the xcorr function directly. Just like sending a sample into a box and retrieving the sample rather
than sending the entire signal at a time.

#### zorro

Hi,

If I understand, you don't need to calculate cross-correlation at all the 79999 time lags, but only at time 0.
For this, the first two lines of your code could be modified like this:

r1 = abs(xcorr(S1,shat1,0,'coeff'));
r2 = abs(xcorr(S1,shat2,0,'coeff'));

but this is even faster:

r1 = abs(S1*shat1');
r2 = abs(S1*shat2');

The normalization given by the argument 'coeff' if the xcorr function is not here, but maybe you don't need it. Otherwise, it can be calculated.
Is it still necessary to split the sequence in blocks if this way works fast enough?
Regards

Z

electricalpeople

### electricalpeople

Points: 2

#### JoannesPaulus

If you want to use a for loop and do the correlation every 250 samples, you need to index the array of your signals as in:
Code:
for p=1:floor(40000/250)
r1 = max(abs(xcorr(S1(:,250*(p-1)+1:250*p), shat1(:,250*(p-1)+1:250*p),'coeff')));
...
end

electricalpeople

### electricalpeople

Points: 2

#### electricalpeople

##### Newbie level 6
Hi Z,

I am not sure how fast it works in the real time since right now I am working only on matlab in which there is no delay. But when I showed the code I have written to my instructor he said in the real time this would create some delay by sending all the samples at a time. So he asked me to send block of samples. Anyways thanks for the idea and will show this to my instructor.

---------- Post added at 10:47 ---------- Previous post was at 10:46 ----------

Hi Joannes, thank you very much.

#### zorro

Ok. You want to start calculation before the whole data are ready.
So imagine you have a function getsamples(N) that returns a part (N samples) of S1, shat1 and shat2, defined in this way:

function [S1_part, shat1_part, shat2_part] = getsamples(N)
...code...
return

Then your code can be like this:

r1 = 0;
r2 = 0;
for p=1:floor(40000/250)
[S1_part, shat1_part, shat2_part] = getsamples(250);
r1 = r1 + S1_part*shat1_part';
r2 = r2 + S1_part*shat2_part';
end
r1A = abs(r1);
r2A = abs(r2);

This calculates only lag 0 of the correlation, that is what I suppose you need.
Regards

Z

electricalpeople

Points: 2