# Similarity between two Signals: cycle by cycle

I need to find the similarity between two signals, i.e., measured signal and the reference signal(e.g. a simple sinusoidal signal). What I mean by similarity is, if there is any decentralized glitch or any change in freq for one particular cycle, the similarity measure should give 'False' or a value in 0 - 1 scale for that cycle. Phase difference also should be considered. Could this be done using correlation? Or is it possible to use wavelets? Or any other method?

Many Thanks...

• Assuming the periods are the same, you can use the correlation coefficient, which will return a number between [-1,1] – Emre Nov 15 '14 at 3:41

so i'll throw the cross-correlation and/or matched-filter thing at you. a little bit similar to Karthik but maybe a little more rigor.

so you have a reference signal, $y(t)$, and a measured signal $x(t)$. suppose they have something in common and some differences and may one of the differences is a misalignment in time. but if you investigate what these two signals have in common (or what they don't) for a variety of different offsets in time, that dissimilarity due to time misalignment can be made non-salient (or maybe you want to know the time offset).

so first consider measuring the dissimilarity (at a variety of different offsets or "lags") as:

$$Q_{xy}(\tau) \triangleq \int\limits_{-\infty}^{+\infty} \left( x(t) - y(t+\tau) \right)^2 dt$$

so you're looking at how much $x(t)$ and $y(t+\tau)$ are different, for different lags, $\tau$, squaring that difference or "error" so that it's always positive (this is analytically easier than if you use the absolute value), adding up the total squared-difference over all of time and you have an overall measure in how dissimilar $x(t)$ and $y(t+\tau)$ are. then picking the lag $\tau$ so that $Q_{xy}(\tau) \ge 0$ is minimum might be considered to be a measure on how dissimilar $x(t)$ and $y(t+\tau)$ are.

the smaller this measure (which is always non-negative), the more similar $x(t)$ and $y(t+\tau)$ are. this is the Average Squared Difference Function (ASDF) and has a similar motivation in its definition as does the more well-known and historical AMDF function.

now the cool thing you get that you don't if it's AMDF is

\begin{align} Q_{xy}(\tau) & = \int\limits_{-\infty}^{+\infty} \left( x(t) - y(t+\tau) \right)^2 \ dt \\ & = \int\limits_{-\infty}^{+\infty} x^2(t) + y^2(t+\tau) - 2x(t)y(t+\tau) \ dt \\ & = \ \int\limits_{-\infty}^{+\infty} x^2(t) dt \ + \ \int\limits_{-\infty}^{+\infty}y^2(t+\tau) dt \ - \ 2\int\limits_{-\infty}^{+\infty}x(t)y(t+\tau) dt \\ & = \ \int\limits_{-\infty}^{+\infty} x^2(t) dt \ + \ \int\limits_{-\infty}^{+\infty}y^2(t) dt \ - \ 2\int\limits_{-\infty}^{+\infty}x(t)y(t+\tau) dt \\ & = \ E_x \ + \ E_y \ - \ 2\int\limits_{-\infty}^{+\infty}x(t)y(t+\tau) dt \\ \end{align}

$E_x$ and $E_y$ are the energies of $x(t)$ and $y(t)$ and not functions of $\tau$. so $Q_{xy}(\tau)$ will be minimum with a $\tau$ that makes the integral on the right maximum. we'll give that integral a name:

$$R_{xy}(\tau) \triangleq \int\limits_{-\infty}^{+\infty}x(t)y(t+\tau) dt$$

and we see that

$$Q_{xy}(\tau) = E_x + E_y - 2 R_{xy}(\tau)$$

so so $Q_{xy}(\tau)$ is minimum when $R_{xy}(\tau)$ is maximum. in fact, with a scaling constant of $2$ tossed in there $Q_{xy}(\tau)$ and $R_{xy}(\tau)$ are upside-down mirror images of each other.

we call $R_{xy}(\tau)$, the cross-correlation function of $x(t)$ and $y(t)$ and is the inner product (if we're pretending we're living in a Hilbert space) of $x(t)$ and $y(t+\tau)$. if we're dealing with complex $x(t)$ and $y(t)$ (we're not, i hope) then $y(t)$ should be complex-conjugated. where $R_{xy}(\tau)$ is maximum is the time offset that $x(t)$ and $y(t)$ are most similar to each other.

the above can be rearranged:

$$R_{xy}(\tau) = \frac{1}{2} \left( E_x + E_y - Q_{xy}(\tau) \right)$$

or

$$\frac{R_{xy}(\tau)}{\frac{1}{2}(E_x + E_y)} = 1 - \frac{Q_{xy}(\tau)}{E_x + E_y}$$

so the cross-correlation is getting normalized by the average energy of $x(t)$ and $y(t)$. we're always subtracting a non-negative number from 1 so that normalized cross-correlation is never larger than 1

and pick the $\tau_0 \ne 0$ where $R_{xy}(\tau_0)>R_{xy}(\tau)$ for all other $\tau$ and calculate that normalized cross-correlation and you have a measure of similarity with

$$-1 \le \frac{R_{xy}(\tau_0)}{\frac{1}{2}(E_x + E_y)} \le 1$$

if it's -1, they are identical but with opposite polarity. if it's 1, they're totally similar (at that offset of $\tau_0$).

Since you already have a reference signal, have you thought of computing the absolute error between the reference and the measured signal.

i.e. if $x(t)$ is the reference signal and $y(t)$ is the measured signal, one of the approaches to find the difference is to evaluate the function

$$d(t) = \cases{0 \quad |x(t) - y(t)| < T \\ 1 \quad |x(t) - y(t)| > T }$$

where $T$ is a threshold set by you.

• thanks for your response. Do you mean the point-by-point difference of the two signals? i need to find the difference in each cycle, on real time signals. – IVG Oct 15 '14 at 12:19
• I meant the point by point difference. To do this on a cycle by cycle basis, since you have a reference signal, you will be knowing the time period of the measured signal. You can compute the point-by-point difference freshly in each cycle i.e. If $\tau$ is the time period of the signal, calculate $|x(t) - y(t)|$ for every interval $[0,tau]$. If the signal exceeded the threshold within this period, $d(t) = 1$. Else, it is zero. – Karthik Upadhya Oct 15 '14 at 13:59

Wavelet cross correlation, wavelet cross coherence, and cross wavelet phase analysis may help.

A short answer is YES, you can use correlation to measure the similarity between two signals. A long answer maybe NO, because what correlation coefficients can reflect is the strength of linear relationship between a test signal $X$ and a reference signal $Y$. If the similarity between $X$ and $Y$ in your mind is different from this, you'd better not use it. Therefore, you should first sit down and think about how to define your interested similarity measure. For example, how about rank correlation?

If you also have difficulties to define such a similarity metric, you probably should think of the generic machine learning approach. In this approach, you will implicitly define such a metric by providing the so-called training data, namely a pairwise data $(X_i,Y_i)$ and its reference label $s_i\in\{\rm{True},False\}$. Once you prepare these training samples, many existing tools can build you a classifier $C(X,Y)$ and predict you the corresponding similarity label $s$. However, the major difficulty of this approach is `training data', which may be not easy to obtain.