# What algorithm can I use to compare two signals' similarity?

Suppose I have two time series (measuring the gyroscope data from two body sensors — A and B).

I somehow need to evaluate whether the signal B is "following" the signal A — that is, how similar it is.

Is there any measure that is used in signal processing for this sort of task?

For instance, I'm thinking about some kind of comparison algorithm that can indicate whether a signal follows another one or something along those lines...

Thank you!

• do you mean "correlation"? Aug 9, 2021 at 20:28

Instead of using the raw gyroscope measurements, I would recommend processing/converting the raw measurements into roll, pitch, and yaw angles. Conversion helps with the interpretation. You can try eyeballing the results and check if $$A \rightarrow B$$.

If you're looking for a mathematically rigorous approach to test if there is a causal relationship between the two sensors, then try Granger causality. Since these are time-series data, you can compute Granger causality for roll, pitch, and yaw, separately, or compute multivariate Granger causality.

You'd need to examine the cross-correlation between the two signals, say $$x_{A}[k]$$, $$x_{B}[k]$$.

From the cross-correlation sequence $$R_{_{AB}}$$ given by the expression

$$R_{_{AB}}[n] \triangleq \sum\limits_{k=-\infty}^{+\infty} x_{A}[n]x_{B}[k+n]$$

you can determine the amount of $$n$$ you need to shift $$x_{B}$$ in the time axis to get the maximum $$R_{_{AB}}$$ which is where it matches with $$x_{A}$$ "the most".

You can use scipy.signal.correlate in Python or if you're using MATLAB, the command xcorr to calculate the correlation sequence. Both documentations provide excellent examples for what you're trying to achieve.

You can use normalized cross correlation to find similarity between two signal

Assuming you have pre-processed the signals to get time alignment and filled in missing samples etc., Spearman rank correlation would be a choice: https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient

It's standard in Python numpy/scipy.

Qualitatively, it will help you track if X and Y fall/rise together.

Of course, good old Pearson correlation (as described in the answers above) would work as well.