Search code examples
matlabcorrelationsimilarity

MATLAB: How to compute the similarity of two signals and get the correct consistency or coherence metric


I was wondering about the consistency metric. Generally, it allows us to deduce the parity or similarity between two signals, right? If so, if the probability is higher (from 0.5 to 1), does it means that there is a strong similarity of the signals? If the margin is less than (0.1-0.43), can this predict the poor coherence between the signals (or poor similarity, the probability the signals are different)? So, if we got the metric <0, is this approved the signal is totally different? Because I'm getting negative numbers. Is this hypothesis possible?

Can I have a clear understanding of the consistency metric of the signal? Here is my small code and figure. Thanks in advance.

s1 = signal3
s2 = signal4     

if  s1 ~= s2
    [C1] = xcorr(s1);        
    [C2] = xcorr(s2);
    signal_mix = C1.*C2   %mixing vector
    signal_mix1 = signal_mix
else
    s1(1,:) == s2(1,:)
    s3 = s1
    s3= s2
    signal_mix = s2
end

n =2;
   
for i = length(signal_mix1)
    signal_mix1(i) = min(C1(i),C2(i))/ max(C1(i),C2(i)) % consistency score
    signal_mix2 = sum(signal_mix1(i))
end

enter image description here


Solution

  • first of all, you could use xcorrfunction to calculate cross-correlation between two signals. from Matlab help:

    r = xcorr(x,y) returns the cross-correlation of two discrete-time sequences. Cross-correlation measures the similarity between a vector x and shifted (lagged) copies of a vector y as a function of the lag. If x and y have different lengths, the function appends zeros to the end of the shorter vector so it has the same length as the other.

    additionally you could use xcov:

    xcov computes the mean of its inputs, subtracts the mean, and then calls xcorr.

    The result of xcov can be interpreted as an estimate of the covariance between two random sequences or as the deterministic covariance between two deterministic signals.

    in case of your example you are using xcorr with one signal so it computes auto-correlation between the signal itself and its lagged signal.

    update: based on the comment, it seems you need linear correlation, it can be calculated by corr function:

    p=corr(x,y)
    

    the value of p is 1 when x , y behave exactly like each other, and is -1 when x and y behave quite the opposite of each other. when p is 0 it means there is no correlation between two signals.