From: David Cubero on
The problem i see is that i want to compare a square signal "y" with an output signal of an AM demodulation "x" ( it will be corrupted by noise so it will not be equal to the pattern even it will be similar)
I wan to know in which index "y" is more similar to"x" to know when the square pulse stars.

My idea is that i should use the function xcorr and look for the maximum? this point should be the end of the square signal?
From: us on
"David Cubero" <sirdivi(a)hotmail.com> wrote in message <i0t7mo$fv$1(a)fred.mathworks.com>...
> The problem i see is that i want to compare a square signal "y" with an output signal of an AM demodulation "x" ( it will be corrupted by noise so it will not be equal to the pattern even it will be similar)
> I wan to know in which index "y" is more similar to"x" to know when the square pulse stars.
>
> My idea is that i should use the function xcorr and look for the maximum? this point should be the end of the square signal?

well... YES(!)... you are talking too much: just TRY it...
also, carefully look at the various options that come with XCORR...

us
From: ImageAnalyst on
On Jul 5, 2:19 pm, "David Cubero" <sird...(a)hotmail.com> wrote:
> My idea is that i should use the function xcorr and look for the maximum? this point should be the end of the square signal?
--------------------------------------
David, that won't work, at least not robustly or in general. Here's
proof:

bigArray = [ 1 1 1 2 3 4 5 6 10 20 30 1 1 1];
searchPattern = [3 4];
% Pattern will match at a shift of 5, which is actually at 18 because
it's zero padded.
result = xcorr(bigArray, searchPattern)
% Note c = xcorr(x,y) returns the cross-correlation sequence in a
length
% 2*N-1 vector, where x and y are length N vectors (N>1).
% If x and y are not the same length, the shorter vector is zero-
padded
% to the length of the longer vector.
[maxValue, index] = max(result)
% Note that the peak xcorr value will be 180, not 3*3+4*4, so you
% will not find the patching location that you are hoping for.

You can see that where the pattern matches is NOT where the max in the
cross correlation is. You'd expect to find the max at results(18),
but it's actually at results(23) because the bigArray is larger up
there.

Now whether it will work with your particular signals, I don't know.
It will depend on what your two signals look like.

From: David Cubero on
Hi, thank you very much, i have been thinking about how to solve it but i can not, i hope morepeople could help me

I think that correlation is the best option to solve the problem but i do not realize how.
From: Walter Roberson on
David Cubero wrote:
> Hi, thank you very much, i have been thinking about how to solve it but
> i can not, i hope morepeople could help me
>
> I think that correlation is the best option to solve the problem but i
> do not realize how.

if D is the demodulated signal consisting of noisy repeated copies of Y
without breaks, then

YC = Y(:);
YL = length(YC);
L = floor(length(D) / YL);
A = mean( reshape(D(1:L),YL,[]), 2);
fiterr = nan(YL,1);
for K = 1 : YL
fiterr(K) = sum((circshift(A,K-1) - YC) .^ 2);
end
[minerr, bestshift] = min(fiterr);

and the pattern is then well aligned starting at D(bestshift)


This presumes that the noise is independent of time and uncorrelated from
sample to sample. Statistical independence over time is probably reasonable
but in practice the noise probably _would_ be correlated over a series of
samples -- e.g., it takes time for a truck to move through the signal path.


By the way, don't be surprised if the corners on your square wave are rounded...