声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2528|回复: 0

[Fortran] 快速傅里叶变换FFT

[复制链接]
发表于 2015-11-3 17:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
本代码实现 Cooley-Tukey 蝶形算法计算复数域的一维快速傅里叶变换。非迭代方式,节约内存,执行速度快。要求数据个数为2的整幂次方,符合语法规范,可直接使用。
如下代码及示范数据,输出为:

(363.000000000000,0.000000000000000E+000)
(-52.9411231676211,-65.4558436870575)
(-15.0000000874228,2.00000000000000)
(14.9411242803314,14.5441577965562)
(31.0000000000000,0.000000000000000E+000)
(14.9411266645322,-14.5441563129425)
(-14.9999999125772,-2.00000000000000)
(-52.9411277772425,65.4558422034438)

(36.0000000000000,0.000000000000000E+000)
(21.0000007843665,-4.589695601353583E-007)
(33.0000000000000,0.000000000000000E+000)
(44.0000003562839,-6.556710155648600E-008)
(55.0000000000000,0.000000000000000E+000)
(62.9999992156335,4.589695601353583E-007)
(73.0000000000000,0.000000000000000E+000)
(37.9999996437161,6.556710155648600E-008)

  1. Module FFT_Mod
  2.   Implicit None
  3.   Integer , parameter :: DP = Selected_Real_Kind( 15 )
  4.   Integer , parameter :: FFT_Forward = 1
  5.   Integer , parameter :: FFT_Inverse = -1
  6. contains

  7.   Subroutine fcFFT( x , forback )
  8.     !//Subroutine FFT , Cooley-Tukey , radix-2
  9.     !// www.fcode.cn
  10.     Real(Kind=DP) , parameter :: PI = 3.141592654_DP
  11.     Complex(Kind=DP) , Intent(INOUT) :: x(:)
  12.     Integer , Intent(IN) :: forback
  13.     Integer :: n
  14.     integer :: i , j , k , ncur , ntmp , itmp
  15.     real(Kind=DP) :: e
  16.     complex(Kind=DP) :: ctmp
  17.     n = size(x)
  18.     ncur = n
  19.     Do
  20.       ntmp = ncur
  21.       e = 2.0_DP * PI / ncur
  22.       ncur = ncur / 2
  23.       if ( ncur < 1 ) exit
  24.       Do j = 1 , ncur
  25.         Do i = j , n , ntmp
  26.           itmp = i + ncur
  27.           ctmp = x(i) - x(itmp)
  28.           x(i) = x(i) + x(itmp)
  29.           x(itmp) = ctmp * exp( forback * cmplx( 0.0_DP , e*(j-1) ) )
  30.         End Do   
  31.       End Do
  32.     End Do
  33.     j = 1
  34.     Do i = 1, n - 1
  35.       If ( i < j ) then
  36.         ctmp = x(j)
  37.         x(j) = x(i)
  38.         x(i) = ctmp
  39.       End If
  40.       k = n/2
  41.       Do while( k < j )
  42.         j = j - k
  43.         k = k / 2
  44.       End Do
  45.       j = j + k
  46.     End Do
  47.     Return
  48.   End Subroutine fcFFT

  49. End Module FFT_Mod
  50.   
  51. Program www_fcode_cn
  52.   use FFT_Mod
  53.   Implicit None
  54.   integer :: i
  55.   integer , parameter :: N = 8
  56.   complex(Kind=DP) :: x(N) = [36.d0,21.d0,33.d0,44.d0,55.d0,63.d0,73.d0,38.d0 ]
  57.   Call fcFFT( x , FFT_Forward )
  58.   Do i = 1 , N
  59.     Write (*, *) x(i)
  60.   End Do
  61.   Call fcFFT( x , FFT_Inverse )
  62.   Do i = 1 , N
  63.     Write (*, *) x(i)/N
  64.   End Do  
  65. End Program www_fcode_cn
复制代码
回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-12-27 21:02 , Processed in 0.091676 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表