Natra
Natra

Reputation: 13

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

Upvotes: 0

Views: 917

Answers (2)

Erysol
Erysol

Reputation: 594

Depending on your use case you might want to consider a dynamic time wraping distance (Matlab has a build in function for that) as similarity metric. One problem with using correlation as a metric is that it compares always the same timestep of the signals. So two identical signals, where one is time delayed, could lead to low correlation. The DTW distance adresses this by comparing to values of adjacent timesteps.

The downside of the dtw distance is that the distance it self can't be interpretet on its only only relative to other distances. So you can tell that two signals A & B with a distance of 150 are more similar than A & C with a distance of 250. But the distance of 150 on its own doesn't tell you a lot.

Upvotes: 1

Hadi
Hadi

Reputation: 1213

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.

Upvotes: 0

Related Questions