Re: correlation function

*To*: mathgroup at smc.vnet.net*Subject*: [mg128774] Re: correlation function*From*: Dana DeLouis <dana01 at me.com>*Date*: Sat, 24 Nov 2012 02:30:07 -0500 (EST)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*Delivered-to*: l-mathgroup@wolfram.com*Delivered-to*: mathgroup-newout@smc.vnet.net*Delivered-to*: mathgroup-newsend@smc.vnet.net

> ListConvolve and ListCorrelate should give same results when given right parameters. Hi. They are very similar, but they do work differently. It would help if you gave a 'small' example of what you are really trying to do. Some thoughts on why you are not getting results you expect could be: -Your FourierParameter is not set correctly. -You are cutting off your data too much with {1,1} -You are rotating the second vector, when you don't mean to. With you large data (ie 309 data points), I get the same results. v = << Your Data as posted >> v//Length 309 SetOptions[{Fourier,InverseFourier},FourierParameters->{1,-1}] ; ListCorrelate[v,v,{1,1}]; InverseFourier[Fourier[Reverse[v]]*Fourier[v]]//Reverse; %%==% True InverseFourier[Fourier[v]^2]; ListConvolve[v,v,{1,1}]; %%==% True With either way, the above are always equal. Again, it depends on what you are doing. >> ListConvolve and ListCorrelate should give same results They operate slightly differently. Here, we don't rotate v2, by using 0, to make it easier to see. v1=v2={a,b,c}; // Here, we keep v2 as is, and shift it right to left ListCorrelate[v1,v2,{1,1},0] {a^2+b^2+c^2,a b+b c,a c} // Here, we reverse v2, but shift it left to right ListConvolve[v1,v2,{1,1},0] {a^2,2 a b,b^2+2 a c} // So, they will always be slightly different. By reversing v2, it's the same as ListConvolve, except we are going left to right. // This is just the opposite of right to left. Add 'Reverse to make this equal to ListConvolve. ListConvolve[v1,v2//Reverse,{1,1},0] {a c,a b+b c,a^2+b^2+c^2} This is the same as ListCorrelate, only backwards. As a fun side note, when I've done InverseFourier[Fourier[v]^2 ] in other languages, it was to square a very large number. For example, to do slow school-method multiplication, one would not chop the data off with {1,1}, but use {1,-1}. Here's an example of 123 * 123 n=123; v=n//IntegerDigits {1,2,3} // Don't rotate (use 0), and have the last digit of v2 (ie 9), end when it gets to the last digit of v1 (use -1) ListConvolve[v,v,{1,-1},0] {1,4,10,12,9} // This is the correct answer in Base 10. To adjust, for the 12, we keep the 2, and carry the 1. 10+1 == 1, carry the 1, etc. // The function FromDigits with default base 10 does all this for us: FromDigits[%] 15129 // Same Answer: n*n 15129 Here's a technique to try to see what's going on. Experiment with ListCorrelate as well. // Here is what you have ListConvolve[{a,b,c},{d,e,f},{1,1}] //MatrixForm // This is what you are really doing. You're rotating {d,e,f} around. ListConvolve[{a,b,c},{d,e,f},{1,1},{d,e,f},Times,Plus] //MatrixForm // Same, but not adding. ListConvolve[{a,b,c},{d,e,f},{1,1},{d,e,f},Times,List] //MatrixForm // Different Start / End ListConvolve[{a,b,c},{d,e,f},{1,1},0,Times,List] //MatrixForm ListConvolve[{a,b,c},{d,e,f},{1,-1},0,Times,List] //MatrixForm Now that I've seen the above, I may have said things backwards with regards to v1,v2. However, the results are the same. Good Luck. = = = = = = = = = = HTH :>) Dana DeLouis Mac & Mathematica 8 = = = = = = = = = = On Friday, November 23, 2012 3:38:21 AM UTC-5, jure lapajne wrote: > Professor (physicist) said this function is called correlation = function. Than I talked to someone from mathematics department and he = said it's some sort of discrete convolution, so it really depends on = person, how he/she calls it. ListConvolve and ListCorrelate should give = same results when given right parameters. I think there is something = strange about my data (linked in previous post), because I also have = some other data and when I use them I get correct (same) results. I = tried all the code in above answers and with given data nothing works. I = think I'll just use fourier transform method because it returns = symmetric data (which is correct) in contrast to ListCorrelate and = ListConvolve which return asymmetric data. > > Thank you all.