2

I have a simple problem. I'm programming my own script to calculate the cross-correlation between two signals in Fortran (each signal can be represented as 1-D array). As any cross-correlation, I have to delay one signal with respect to the other, and compare only the intersecting elements between them. This means that for each lag, one of the signals will change its size because the intersecting elements will reduce.

The problem is the resizing of the array inside the code. I show a very simple example to show the situation, where the second signal is delayed with respect to the other. I only would need to know how to define s1 and s2 to keep this working, because what I put with allocatable statement doesn't work:

program test_corr

   ! Defining variables:
   integer :: i, max_delay
   real, dimension(5) :: signal_1, signal_2
   real, allocatable :: s1(:), s2(:)

   ! Signals to correlate:
   signal_1 = (/ 1, 2, 3, 4, 5 /)  
   signal_2 = (/ 6, 7, 8, 9, 10 /)

   ! Loop along delays:
   max_delay = 3
   do i=1,max_delay
      ! Applying delay to the second signal:
      s1 = signal_1(1:size(signal_1)-1)
      s2 = signal_2(i+1:size(signal_2))
   end do

end program

For example, if I add a print *, s2 statement inside the loop, it doesn't show anything. I would expect an output for the second signal like: 7 8 9 10 and then 8 9 10 for the first two iterations.

I am using ifort 15 and I compile like

ifort test_corr.f90 -o test_corr
francescalus
  • 30,576
  • 16
  • 61
  • 96
Carlos Herrera
  • 165
  • 1
  • 1
  • 7
  • 1
    Could you please explain your problem more? Do you get an error message when compiling/running, or do you just get results you don't expect? – francescalus Jan 17 '16 at 20:21
  • @francescalus the code I put in my question can be compiled with no errors. But it doesn't give me the results that I want. For example, if I add a `print *, s2` statement inside the loop, it doesn't show anything. I would expect an output for the signal 2 like: `7 8 9 10` and then `8 9 10` for the first two iterations. – Carlos Herrera Jan 17 '16 at 20:28
  • You are relying, perhaps, on a Fortran 2003 feature of _automatic allocation_ on (intrinsic) assignment. Which compiler, and version, are you using and which compiler flags? [In particular, ifort does not currently support this standard behaviour without a special flag.] – francescalus Jan 17 '16 at 20:31
  • 1
    @francescalus I'm using ifort. The compiler version is 15.0.3 20150407. This is what I write in the console when I compile the code: `ifort test_corr.f90 -o test_corr` – Carlos Herrera Jan 17 '16 at 20:40
  • @francescalus it seems the answer is in the link you posted, because with gfortran it works. I must say that I'm not a expert in Fortran yet. Can you please tell me how would be syntax to compile this code using the option `-assume realloc-lhs`? I would appreciate that. – Carlos Herrera Jan 17 '16 at 21:14
  • 1
    Where you have `ifort test_corr.f90 -o test_corr` add that option, so something like `ifort test_corr.f90 -assume realloc-lhs -o test_corr`. But I'm a fan of having that in the ifort.cfg file (site-wide configuration) and even using `-standard-semantics` (which implies that option) and only deviating from that when I really care about performance and so on. – francescalus Jan 17 '16 at 21:19

0 Answers0