# Calculation of the correlation of two sinusoidals

First of all I apologize for the long question. I am working with a 3-axis accelerometer and a 3-axis gyroscope and I need to calculate the correlation between the different signals (acceleration in x,y,z and the quaternion x,y,z,w). In the end, I need to get 21 correlations, one for each pair. I was thinking of using the standard Pearson product moment correlation coefficient. Particularly the second formula in the "for a sample" section that uses the standard score, sample mean, and sample standard deviation.

Now, I am computing these correlations as features for a classifier. I am computing these features over a window size. I will now introduce some MATLAB code that will help illustrate what I am trying to do.

First we have a discrete sine and a cosine.

t = [0:1:600]; %time samples
f = 1/2; %input signal frequency
fs = 100; %sampling frequency
x = sin(2*pi*f/fs*t); %generate sine
figure(1);
stem(t,x,'r');
figure(2);
stem(t*1/fs*1000,x,'r');
hold on;
plot(t*1/fs*1000,x);
y = cos(2*pi*f/fs*t); %generate cosine
figure(3);
stem(t,y,'r');
figure(4);
stem(t*1/fs*1000,y,'r');
hold on;
plot(t*1/fs*1000,y);


Now I get Pearson's coefficient for a window of 50 measurements (notice this is equivalent to a quarter of a cycle) with the following code. This can easily be changed for windows of different sizes but I will leave at the moment in 50 for the sake of simplicity.

xMean = sum(x(1:50))/50;
yMean = sum(y(1:50))/50;
tmpX = x(1:50) - xMean;
tmpY = y(1:50) - yMean;
stdDevX = sqrt((sum(tmpX .* tmpX)) / 49);
stdDevY = sqrt((sum(tmpY .* tmpY)) / 49);
stdScoreX = tmpX ./ stdDevX;
stdScoreY = tmpY ./ stdDevY;
stdScores = stdScoreX .* stdScoreY;
r = sum(stdScores)/49


This gives an r(Pearson's coefficient) equivalent to -0.9183. This makes sense from the scatter plot of x vs y: I get the scatter plot with this:

figure(5);
scatter(x(1:50),y(1:50));


Different windows have different Pearson's coefficients, either a value close to 1 or a value close to -1, which makes sense from the scatter plots of x vs y in the other windows. The second window has an r = 0.9183. The third window will have a value again close to -1. This windows are with no overlap whatsoever.

Now, if I change the window size to 100 samples, things change. Same story with a window of size 200, but now it looks like a circle. Now it is visible (and calculations also confirmed it) that the correlation is 0 or a value very close to 0.

So my question after all this is… what is the correct way of calculating a correlation for this type of periodic signals? Or are both of these ways correct? Preferrably I would like to obtain a number close to -1, 0, or 1 (discrete) since these numbers will be input features in a classifier. I know that, for example, two sinusoidal functions that are in phase will have a correlation close to 1 (or 1, depending on noise but that is another issue). My main problem is the correlation in the example I showed. So, probably I should calculate over a window size that is the length of at least a period in order to get either a 0 or a 1, but I am not entirely sure.

Thanks!

• Please do not cross-post on the SE series of sites. It's considered poor form (see the SO comment, too!). Your post belongs here. – Peter K. Aug 15 '13 at 19:55
• Yes, sorry. I deleted my other post from SO. Thanks for letting me know :-) – strontivm Aug 15 '13 at 21:37
• Where did sinusoid come from? Why didn't you try correlation computation on your actual data? Can you repeat the above expt with your actual data and see if there is any trouble w.r.t window sizes? Why are you assuming that your data will be periodic? – user13107 Aug 16 '13 at 9:41
• I am measuring repetitive cyclic movements only, therefore my data are periodic and sinusoidal in shape. I have the same issues with the correlation on my data, for a specific window size I get one value and for a different window size I get a different value, depending on which parts of the cycle I cover. I hope I answered your questions. – strontivm Aug 16 '13 at 13:23
• thnks, please use the @ operator so that I get notified of your reply (@strontivm) – user13107 Aug 17 '13 at 11:52