Search code examples
fortrancallgoto

Rainflow algorithm - Fortran conversion to Matlab


I am trying to convert a Rainflow cycle counting algorithm which is in Fortran, which is a language I am not familiar with, into Matlab.

There is a ready made Rainflow I've downloaded for Matlab but that does not fit the requirements of my project so I'm trying to build one from scratch.

Here is the Fortran code:

INTEGER BUFFER (4096), INDEX, VALUE, RANGE, MEAN, X, Y
INDEX = 0
10 CONTINUE
   call 'get next peak/valley', VALUE
   INDEX = INDEX + 1
   BUFFER (INDEX) = VALUE
20 CONTINUE
   IF (INDEX.LT.3) THEN
           not enough points to form a cycle
           GOTO 10
   ELSE
           X = ABS (BUFFER(INDEX)  - BUFFER(INDEX - 1))
           Y = ABS (BUFFER(INDEX - 1)  - BUFFER(INDEX - 2))
           IF (X.GE.Y) THEN
c -- cycle has been closed
                RANGE = Y
                MEAN = (BUFFER(INDEX-1) + BUFFER(INDEX-2))/2
c -- remove the cycle
                INDEX = INDEX - 2
                BUFFER(INDEX) = BUFFER(INDEX+2)
c -- see if this value closes any more cycles
                GOTO 20
           ELSE
                GOTO 10
           END IF
END IF

I had downloaded f2matlab (a Fortran to Matlab converter) but it requires a Fortran compiler which I do not have.

The bits I don't really understand how I can convert are:

  1. The call 'get next… line (is this an input()?)
  2. The BUFFER(4096) etc (is this a bit large to be a matrix in matlab?)
  3. The GOTO/CONTINUE structure.

What do they mean, in English (or Matlab)?

I have seen How to translate fortran goto state to matlab and translating loop from Fortran to MATLAB but they do not help me very much.


Solution

  • This

     call 'get next peak/valley', VALUE
    

    isn't (currently) syntactically valid Fortran and I'm not sure whether any compiler of yore would have understood it either. I guess that it means get a VALUE for use in the following bits of code.

    INTEGER BUFFER (4096)
    

    is a simple declaration that BUFFER is a vector of 4096 integers, nothing to scare Matlab in that volume of data.

    Finally, GOTO is an unconditional jump and the number following it is the label of the line to jump to, so GOTO 10 means execute the line with label 10 next. It was fairly common in FORTRAN of the vintage you are showing us to jump to a CONTINUE statement which is, in this context, a no-operation, execution continues to the next line.

    In another context, with DO loops CONTINUE would have marked the end of the block of code inside the scope of the loop and would have a subtly different effect.