5
$\begingroup$

I have the following two discrete signals

Spktrain1 = {{12.853, 1}, {124.719, 1}, {236.349, 1}, {347.979, 1}};
Spktrain2 = {{1.502, 1}, {113.556, 1}, {225.186, 1}, {336.816, 1}};

I would like first to convolve them with a kernel function (that in my case is a Gaussian function) in order to make the signals continuous. Once I have them, I would like to cross-correlate them obtaining a function of the lag time, which I would like to plot.

$\endgroup$
2
  • 3
    $\begingroup$ Convolution is a linear operation, so you'd be better off writing out your formulas explicitly and then cross-correlating the kernel functions directly, instead of convolving first and then cross-correlating the result. In essence, interchange the order of sums and integrals to make things simpler. $\endgroup$ – Roman Mar 1 at 18:19
  • $\begingroup$ Thanks for your answer. Unfortunately I need both of them, but I do not know how to do it in Mathematica. Could you please show me an example? $\endgroup$ – VDF Mar 1 at 18:24
5
$\begingroup$

Define a Gaussian kernel. The width is specified by s

kernel = #2 Exp[-(t - #1)^2/(2 s^2)] &;

Define a function to compute the correlation in terms of the convolution

correlate[x_, y_, t, T_] := Convolve[x, y /. t -> -t, t, T]

Test this by correlating two kernels at different times

correlate[kernel[t1, 1], kernel[t2, 1], t, T]
(* (E^(-((T - t1 + t2)^2/(4 s^2))) Sqrt[π])/Sqrt[1/s^2] *)

As expected the location of the peak is given by t1-t2.

Use a value of s=10 to spread the specified points and correlate.

correlation = 
 Block[{s = 10}, 
  correlate[Total[kernel @@@ Spktrain1], Total[kernel @@@ Spktrain2], 
   t, T]]
(* 1.98775*10^-113 E^((-1.61981 - 0.0025 T) T) + 
 1.98531*10^-48 E^((-1.06167 - 0.0025 T) T) + 
 2.55025*10^-48 E^((-1.06049 - 0.0025 T) T) + 
 1.7299*10^-10 E^((-0.503515 - 0.0025 T) T) + 
 1.94791*10^-10 E^((-0.502335 - 0.0025 T) T) + 
 1.94791*10^-10 E^((-0.502335 - 0.0025 T) T) + 
 12.9801 E^((0.055815 - 0.0025 T) T) + 
 25.9602 E^((0.055815 - 0.0025 T) T) + 
 12.8435 E^((0.056755 - 0.0025 T) T) + 
 7.54594*10^-16 E^((0.613965 - 0.0025 T) T) + 
 7.54594*10^-16 E^((0.613965 - 0.0025 T) T) + 
 5.81383*10^-16 E^((0.616085 - 0.0025 T) T) + 
 3.82712*10^-59 E^((1.17211 - 0.0025 T) T) + 
 2.32725*10^-59 E^((1.17424 - 0.0025 T) T) + 
 8.12734*10^-130 E^((1.73239 - 0.0025 T) T) *)

Plot the results

Plot[correlation, {T, -500, 500}, PlotRange -> All]

The peak of the correlation gives the delay between the two sets of samples

FindMaximum[correlation, {T, 0}]
(* {70.897, {T -> 11.21}} *)
$\endgroup$
4
  • $\begingroup$ mikado thank you very much for your answer. So if I have understood correctly you convolve the two signals with the Kernel function in order to obtain a continuous function. but at the end do you calculate also they cross-correlation? $\endgroup$ – VDF Mar 1 at 21:57
  • 1
    $\begingroup$ You are correct - I've convolved the smoothed functions rather than correlating them. Of course, correlation is convolution with one of the signals reversed. Perhaps you can work it out from here. $\endgroup$ – mikado Mar 1 at 21:58
  • $\begingroup$ Mikado thank you again for further explanations. Yes I agree I have to reverse the second term in order to obtain the cross-correlation. I have simply applied Reverse[Total[kernel @@@ Spktrain2]], which I think it works $\endgroup$ – VDF Mar 1 at 22:45
  • $\begingroup$ I have done same trials, and unfortunately I did not get the right results. Can you help me to solve it? I am confused about convolution and cross-correlation in terms of code. $\endgroup$ – VDF Mar 2 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.