[Fortran] Filon's method Fourier Transform

Click For Summary

Discussion Overview

The discussion revolves around implementing Filon's method for calculating the Fourier cosine transform in Fortran. Participants are seeking guidance on how to pass a function to the subroutine and how to properly implement the code for the transform.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant requests an example of how to include a function in the subroutine for Filon's method.
  • Another participant suggests calculating a series of values at intervals defined by DT and storing them in an array to be passed to the subroutine.
  • There is confusion about how to pass the array as an argument, with multiple participants expressing uncertainty.
  • A participant shares their auxiliary function and main program code but seeks further guidance on executing the Fourier transform.
  • Errors in the code are discussed, including an unclassifiable statement related to the calculation of CHAT in the subroutine.
  • One participant notes they fixed an error related to an extra bracket in their formula, indicating progress in their implementation.

Areas of Agreement / Disagreement

Participants generally agree on the need for effort in understanding how to pass arrays to subroutines, but there is no consensus on the best approach to implement the Fourier transform using Filon's method.

Contextual Notes

Some participants express uncertainty about the proper syntax for passing arrays in Fortran, and there are unresolved issues related to specific coding errors encountered during implementation.

Galizius
Messages
14
Reaction score
0
I was told to do a Fourier transform of function by using a Filon's method. I have found the code but I don't know how to include any function to the subroutine. I would be grateful for any example of how to use this code.

Fortran:
   SUBROUTINE FILONC ( DT, DOM, NMAX, C, CHAT )

C    *******************************************************************
C    ** CALCULATES THE FOURIER COSINE TRANSFORM BY FILON'S METHOD     **
C    **                                                               **
C    ** A CORRELATION FUNCTION, C(T), IN THE TIME DOMAIN, IS          **
C    ** TRANSFORMED TO A SPECTRUM CHAT(OMEGA) IN THE FREQUENCY DOMAIN.**
C    **                                                               **
C    ** REFERENCE:                                                    **
C    **                                                               **
C    ** FILON, PROC ROY SOC EDIN, 49 38, 1928.                        **
C    **                                                               **
C    ** PRINCIPAL VARIABLES:                                          **
C    **                                                               **
C    ** REAL    C(NMAX)            THE CORRELATION FUNCTION.          **
C    ** REAL    CHAT(NMAX)         THE 1-D COSINE TRANSFORM.          **
C    ** REAL    DT                 TIME INTERVAL BETWEEN POINTS IN C. **
C    ** REAL    DOM                FREQUENCY INTERVAL FOR CHAT.       **
C    ** INTEGER NMAX               NO. OF INTERVALS ON THE TIME AXIS  **
C    ** REAL    OMEGA              THE FREQUENCY                      **
C    ** REAL    TMAX               MAXIMUM TIME IN CORRL. FUNCTION    **
C    ** REAL    ALPHA, BETA, GAMMA FILON PARAMETERS                   **
C    ** INTEGER TAU                TIME INDEX                         **
C    ** INTEGER NU                 FREQUENCY INDEX                    **
C    **                                                               **
C    ** USAGE:                                                        **
C    **                                                               **
C    ** THE ROUTINE REQUIRES THAT THE NUMBER OF INTERVALS, NMAX, IS   **
C    ** EVEN AND CHECKS FOR THIS CONDITION. THE FIRST VALUE OF C(T)   **
C    ** IS AT T=0. THE MAXIMUM TIME FOR THE CORRELATION FUNCTION IS   **
C    ** TMAX=DT*NMAX. FOR AN ACCURATE TRANSFORM C(TMAX)=0.            **
C    *******************************************************************

        INTEGER    NMAX
        REAL       DT, DOM, C(0:NMAX), CHAT(0:NMAX)

        REAL       TMAX, OMEGA, THETA, SINTH, COSTH, CE, CO
        REAL       SINSQ, COSSQ, THSQ, THCUB, ALPHA, BETA, GAMMA
        INTEGER    TAU, NU

C    *******************************************************************

C    ** CHECKS NMAX IS EVEN **

        IF ( MOD ( NMAX, 2 ) .NE. 0 ) THEN

           STOP ' NMAX SHOULD BE EVEN '

        ENDIF

        TMAX = REAL ( NMAX ) * DT

C    ** LOOP OVER OMEGA **

        DO 30 NU = 0, NMAX

           OMEGA = REAL ( NU ) * DOM
           THETA = OMEGA * DT

C       ** CALCULATE THE FILON PARAMETERS **

           SINTH = SIN ( THETA )
           COSTH = COS ( THETA )
           SINSQ = SINTH * SINTH
           COSSQ = COSTH * COSTH
           THSQ  = THETA * THETA
           THCUB = THSQ * THETA

           IF ( THETA. EQ. 0.0 ) THEN

              ALPHA = 0.0
              BETA  = 2.0 / 3.0
              GAMMA = 4.0 / 3.0

            ELSE

              ALPHA = ( 1.0 / THCUB )
     :                * ( THSQ + THETA * SINTH * COSTH - 2.0 * SINSQ )
              BETA  = ( 2.0 / THCUB )
     :                * ( THETA * ( 1.0 + COSSQ ) -2.0 * SINTH * COSTH )
              GAMMA = ( 4.0 / THCUB ) * ( SINTH - THETA * COSTH )

           ENDIF

C       ** DO THE SUM OVER THE EVEN ORDINATES **

           CE = 0.0

           DO 10 TAU = 0, NMAX, 2

              CE = CE + C(TAU) * COS ( THETA * REAL ( TAU ) )

10         CONTINUE

C       ** SUBTRACT HALF THE FIRST AND LAST TERMS **

           CE = CE - 0.5 * ( C(0) + C(NMAX) * COS ( OMEGA * TMAX ) )

C       ** DO THE SUM OVER THE ODD ORDINATES **

           CO = 0.0

           DO 20 TAU = 1, NMAX - 1, 2

              CO = CO + C(TAU) * COS ( THETA * REAL ( TAU ) )

20         CONTINUE

C       ** FACTOR OF TWO FOR THE REAL COSINE TRANSFORM **

           CHAT(NU) = 2.0 * ( ALPHA * C(NMAX) * SIN ( OMEGA * TMAX )
     :                       + BETA * CE + GAMMA * CO ) * DT

30      CONTINUE

        RETURN
        END
 
Last edited by a moderator:
Technology news on Phys.org
Before calling this function, use your function to calculate a series of values at points spaced by some amount DT, and store those values in an array. Pass that array as the fourth argument to this function.
 
jtbell said:
Before calling this function, use your function to calculate a series of values at points spaced by some amount DT, and store those values in an array. Pass that array as the fourth argument to this function.

I do not really know how I should pass that array as the fourth argument. Could you help me?
 
Galizius said:
I do not really know how I should pass that array as the fourth argument. Could you help me?
Then it looks like you're stuck, at least until you can figure out how to pass an array to a subroutine in Fortran.

According to the rules of this forum we are not going to simply do your work for you with no effort shown on your part.
 
Mark44 said:
Then it looks like you're stuck, at least until you can figure out how to pass an array to a subroutine in Fortran.

According to the rules of this forum we are not going to simply do your work for you with no effort shown on your part.
Yes, I know and I strongly support such a position.
I have done following code for counting values of my function. I do not really know what to do further. How to force a program to count the transform of that function? I would be really grateful for any further suggestions.

The code is :
Auxiliary function
Fortran:
real(10) function g(z)
implicit none
real(10) :: z, infinity, s,b, I
real(10) :: y
y=huge(1.d0)
infinity = y+y
s=2
b=-1
I=3

if (z>=0 .and. z<s) then
  g=infinity
elseif (z>=s .and. z<I) then
  g=b
elseif (z>=I) then
  g=0
endif
end function g
And the main code:
Fortran:
program function1
implicit none
real(10) :: g, a, x, dt
integer, parameter :: nmax=128
integer :: k
real(kind=10), dimension(nmax) :: f
dt=0.1
a=1
open(1, file='wykresik.dat', action='write', status='replace')

do k=1,nmax
  x=float(k)*dt
  if (x>0) then
  f(k)=exp((-a*g(x))-1)
  else
  f(k)=0
  end if
write(1,*)x,f(k)
enddo
end program function1
include 'g.f95'

Edit:
Okay, I know that I exchange the C as f in the subroutine... But now I am getting following error

Fortran:
  Included at function1.f95:26:

           CHAT(NU) = 2.0 * ( ALPHA * D(NMAX) * SIN ( OMEGA * TMAX ))+ BETA * C
           1
Error: Unclassifiable statement at (1)
 
Last edited:
Please show us the code that calls the FILON subroutine.

Also, the code that is shown in the error message --
Fortran:
CHAT(NU) = 2.0 * ( ALPHA * D(NMAX) * SIN ( OMEGA * TMAX ))+ BETA * C
-- is different from the code you showed in post #1.
Maybe it was just cut off.

Galizius said:
Fortran:
CHAT(NU) = 2.0 * ( ALPHA * C(NMAX) * SIN ( OMEGA * TMAX ) + BETA * CE + GAMMA * CO ) * DT
 
Mark44 said:
Please show us the code that calls the FILON subroutine.

Also, the code that is shown in the error message --
Fortran:
CHAT(NU) = 2.0 * ( ALPHA * D(NMAX) * SIN ( OMEGA * TMAX ))+ BETA * C
-- is different from the code you showed in post #1.
Maybe it was just cut off.

I have already fixed that. I just had one additional bracket in this formula. Thank you for help. It works properly now
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
6K