# Evaluation of autocorrelation function [closed]

I am attempting to evaluate this double integral:

Integrate[
Integrate[
Exp[-R (t1 - t2)] s[t1] s[t2], {t2, 0, t1}
], {t1, 0, t}
]


where s(t) is a piecewise-constant function. The final t value will be within a portion of the domain of s(t) is constant. The s(t) function is determined first by generating an array, lArray, that contains the domain for the piecewise-function, and ModArray, that contains the function values for those domains. I then define the following identifying function that returns the value of ModArray when fed an arbitrary l value:

IdenS[lArray_, ModArray_, Nlen_, l_] :=
For[i = 1, i < Nlen + 1, i++,
TempSum = Sum[lArray[[1, j]], {j, 1, i}];
TotalSum = Sum[lArray[[1, j]], {j, 1, Nlen}];
Which[
((TempSum > l) && (l < TotalSum) && (l > 0)),
(Return[ModArray[[1, i]]];
Break;),
l < 0,
(Return;
Break;),
l > TotalSum,
(Return;
Break;
)
]
]


When trying to evaluate the integral using the IdenS function defined above, the integral returns something in terms of Null values. The IdenS function is having a problem with accepting the t1 and t2 values before evaluation. Is there some workaround that will result in an analytic result in terms of t and R for t values greater than the sum of all the t values in lArray? Perhaps some usage of Hold, Evaluate, and Assumptions?

Example result:

Nlen = 5;
lArray = ConstantArray[1, {1, Nlen}];
ModArray = ConstantArray[0, {1, Nlen}];
For[j = 1, j < Nlen + 1, j++,
ModArray[[1, j]] = (-1)^(j + 1);
]

Integrate[
Integrate[
Exp[-R (t1 - t2)] IdenS[lArray, ModArray, Nlen, t1] IdenS[lArray,
ModArray, Nlen, t2], {t2, 0, t1}
], {t1, 0, 1}
]

(* Result: *)
(E^-R Null^2 (1 + E^R (-1 + R)))/R^2

• Shouldn't s(t1) and s(t2) actually be s[t1] and s[t2] instead? Also, you have shown how you are trying to implement your solution. But could you comment on what exactly you are trying to accomplish? It is not that clear to me what the underlying problem is. – MarcoB Jan 30 '17 at 16:43
• Sorry, it should be s[t1] and s[t2], but I never actually execute that code, so that shouldn't be a problem. Thanks for pointing that out. My goal is generally to calculate something that looks like an autocorrelation of the s(t) function convolved with an exponential decay. The general problem is that the IdenS function is having a difficult time dealing with the symbolic t1 and t2 values. – Aaron Ross Jan 30 '17 at 16:45
• Aaron, I wonder if IdenS is correct. That function returns 1 throughout the $[0,1[$ domain, except at 1, where its value is -1. Is that correct? See Plot[IdenS[lArray, ModArray, Nlen, t], {t, 0, 1}]. – MarcoB Jan 30 '17 at 16:55
• Hi MarcoB: this is correct! The domain of the s(t) function here is intended to be [0,5], oscillating back and forth between 1 and -1, and 0 outside of those bounds. – Aaron Ross Jan 30 '17 at 17:02
• In your example, the IdenS function generates a square wave. Is that always the case, or is it peculiar to the example? If it is always the case, then you can replace it with the built-in function SquareWave. – m_goldberg Jan 31 '17 at 8:13

I believe your IdenS can be replaced with a function defined by much simpler code.

f[lArray_, ModArray_, l_] :=
Which[
l <= 0, ModArray[],
l >= Total[lArray], ModArray[[-1]],
True,
Module[{val = Pick[ModArray, l <= # & /@ Accumulate[lArray]][]},
If[NumericQ[val], val, 1, 1]]]


Using the example parameters

n = 5;
lArray = ConstantArray[1, n];
modArray = Table[(-1)^(j + 1), {j, n}];


f generates a square wave.

Plot[f[lArray, modArray, t] , {t, 0, 5}] Evaluating your double integral with f replacing IdesS

Integrate[
Exp[-R (t1 - t2)] f[lArray, modArray, t1]  f[lArray, modArray, t2],
{t1, 0, 1}, {t2, 0, t1}]


gives The above integral is equivalent to

Integrate[
Exp[-R (t1 - t2)] SquareWave[t1/2]  SquareWave[t2/2], {t1, 0, 1}, {t2, 0, t1}]

• this is perfect! Thanks for working through this. I am not entirely sure that I understand the syntax in the Module portion of the function though. I need to dig into the # % /@ usage a bit. The f function is necessary for me to be able to input more complicated modulation functions other than the square wave. – Aaron Ross Jan 31 '17 at 15:36