CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   Reagrding autocorrelation (https://www.cfd-online.com/Forums/main/230694-reagrding-autocorrelation.html)

ari003 October 1, 2020 09:53

Reagrding autocorrelation
 
1 Attachment(s)
To have a proper understanding I started with a short dataframe where the 2nd column represensts velocity at different time-step collected from a probe (for example). And I m trying to plot the correlation function with time lag.
PHP Code:

time    U
1    10.22
2    10.54
3    10.15
4    10.87
5    10.24
6    10.35
7    10.45
8    10.75
9    10.58
10    10.4 


r(s)=⟨U(t)U(t+S)⟩/⟨⋃(t)^2 ⟩

And the above formula is my auto-correlation function where τ is my time lag.
Now as far my my knowledge it is calculated as follows (explaining with this example).
1. The denominator is just when τ =0. So
Quote:

<u(1)*u(1)+u(2)*u(2)…….u(10)*u(10)>
In this way we found the mean <u2>.

2. The τ = 0 to 10
Quote:

For r(0)= <u2>/<u2> =1
For r(1)= <u(1)u(2)+u(2)u(3)……+u(8)u(9)+u(9)u(10)>/ <u2>.
Similarly it will be on till 10 where finally r(10)=0

Now my questions are:-
1. Does this procedure really happen like this?
2. If so then I tried it in MATLAB and it gave results with negative( whereas my U are all positive). How is that possible?

I ve attached a word doc for better understanding of my doubt. Will be really helpful if someone helps.

flotus1 October 1, 2020 15:29

Not sure which matlab function you used here. Or what the default behavior of matlab is with that function.
But there is some degree of ambiguity when it comes to evaluating autocorrelation. Just look at all the options gnu octave provides: https://octave.sourceforge.io/signal...ion/xcorr.html
On top of those, one could also remove the mean value before computing an autocorrelation. Maybe that's what your matlab call was doing, and why it gave negative values.
Among all those options (including your normalization with the value for tau=0), there is no definitive "right" or "wrong" way of computing an autocorrelation. Each of the options just highlights different properties of the signal. Which one is right for you mostly depends on what you want to do with the autocorrelation values.

gnwt4a October 2, 2020 04:36

for calculating the correlation u need to subtract out the mean. so negative values are possible and sane. negative correlation means that for certain separation in time/space, the product of values above (or below) the average with those below (or above) the average are outsumming the other combination.
--

ari003 October 2, 2020 04:44

Quote:

Originally Posted by gnwt4a (Post 784274)
for calculating the correlation u need to subtract out the mean. so negative values are possible and sane. negative correlation means that for certain separation in time/space, the product of values above (or below) the average with those below (or above) the average are outsumming the other combination.
--

Thank you for your response. Then the value of negetive makes sense if it is substracted out of mean but in the formula of turbulence autocorrelation where should I use that mean tern to substract?
Quote:

r(s)=⟨U(t)U(t+S)⟩/⟨⋃(t)^2 ⟩
I mean here in this equation. The mean is just divided not substracted.

flotus1 October 2, 2020 05:02

Not like that. You are dividing by the sum of squares. Removing the mean in this context: calculate the mean value of your signal, subtract that from each value of your signal, then go on to calculate autocorrelations with the result.

ari003 October 2, 2020 05:25

Quote:

Originally Posted by flotus1 (Post 784280)
Not like that. You are dividing by the sum of squares. Removing the mean in this context: calculate the mean value of your signal, subtract that from each value of your signal, then go on to calculate autocorrelations with the result.


Lets say
Quote:


U
0 10.22
1 10.54
2 10.15
3 10.87
4 10.24
5 10.35
6 10.45
7 10.75
8 10.58
9 10.4





U
0 -0.23499999999999943
1 0.08499999999999908
2 -0.3049999999999997
3 0.41499999999999915
4 -0.21499999999999986
5 -0.10500000000000043
6 -0.005000000000000782
7 0.29499999999999993
8 0.125
9 -0.054999999999999716
If the 2nd series represents U(t)-U(mean) then we should proceed with the autocorrelation with the 2nd series right?
But I just plotted the thing in python just now both gave the same autocorr plot.:)

gnwt4a October 2, 2020 05:31

that was fast.


1) find the mean of the u values.


2) from every u value subtract out the above mean. you will find negative values appearing.


3) calculate the autocorrelation which should be the same as the matlab one.



warning: i have not used matlab. it is very likely that matlab assumes periodicity in the data so that its calculation is 'wrapped' round.
--

ari003 October 2, 2020 05:33

Quote:

Originally Posted by gnwt4a (Post 784283)
that was fast.


1) find the mean of the u values.


2) from every u value subtract out the above mean. you will find negative values appearing.


3) calculate the autocorrelation which should be the same as the matlab one.



warning: i have not used matlab. it is very likely that matlab assumes periodicity in the data so that its calculation is 'wrapped' round.
--

Yaa that s what I did and shared in my previous comment. Thanks a lot for your valuable time.

flotus1 October 2, 2020 06:01

Quote:

Originally Posted by ari003 (Post 784282)
But I just plotted the thing in python just now both gave the same autocorr plot.:)

Both... which ones are you referring to exactly?
If you plugged both your original series and the series with zero mean into your matlab call, then we have confirmation that whatever you used in matlab to get autocorrelations, does indeed remove mean values first. Maybe it's time to read the documentation now. Or share which call you used in matlab.

ari003 October 2, 2020 07:15

Quote:

Originally Posted by flotus1 (Post 784288)
Both... which ones are you referring to exactly?
If you plugged both your original series and the series with zero mean into your matlab call, then we have confirmation that whatever you used in matlab to get autocorrelations, does indeed remove mean values first. Maybe it's time to read the documentation now. Or share which call you used in matlab.


This one is my original series :
Quote:


U
0 10.22
1 10.54
2 10.15
3 10.87
4 10.24
5 10.35
6 10.45
7 10.75
8 10.58
9 10.4

This one is after deducting the mean value which is 10.445
Quote:


U
0 -0.24000
1 0.07999
2 -0.31000
3 0.40999
4 -0.22000
5 -0.11000
6 -0.01000
7 0.28999
8 0.11999
9 -0.06000

I m using both MATLAB and Python.
When I plot both in Python I got the same result.
For Python

Quote:

a=pd.DataFrame({'u':[10.22,10.54,10.15,10.87,10.24,10.35,10.45,10.75,10 .58,10.40]})
b=np.mean(a)
c=a-b.round(2)


z=plot_acf(c,lags=9)
pyplot.show()

Thanks again for your patience and eagerness.:)

flotus1 October 2, 2020 07:32

So mystery solved I guess? Your matlab call with default settings removes the mean values before calculating autocorrelations. That's why you get the same result with the original series, and the series where you manually removed the mean value before plugging it into the autocorrelation function.


All times are GMT -4. The time now is 22:36.