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:
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.