Skip to content

Commit eeaee23

Browse files
committed
add fftfreq function
1 parent 14549b1 commit eeaee23

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

src/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ SRCF90 = \
6060
fftpack_qct.f90\
6161
fftpack_iqct.f90\
6262
fftpack_dct.f90\
63-
rk.f90
63+
rk.f90\
64+
fftpack_utils.f90
6465

6566
OBJF := $(SRCF:.f90=.o)
6667
OBJF90 := $(SRCF90:.f90=.o)
@@ -86,6 +87,7 @@ fftpack_iqct.o: fftpack.o rk.o
8687
fftpack_dct.o: fftpack.o rk.o
8788
fftpack_fftshift.o: fftpack.o rk.o
8889
fftpack_ifftshift.o: fftpack.o rk.o
90+
fftpack_utils.o: fftpack.o
8991

9092
zfftb.f90: rk.o
9193
cfftb1.f90: rk.o

src/fftpack.f90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module fftpack
77
public :: zffti, zfftf, zfftb
88
public :: fft, ifft
99
public :: fftshift, ifftshift
10+
public :: fftfreq
1011

1112
public :: dffti, dfftf, dfftb
1213
public :: rfft, irfft
@@ -175,6 +176,11 @@ pure subroutine dcost(n, x, wsave)
175176
real(kind=rk), intent(in) :: wsave(*)
176177
end subroutine dcost
177178

179+
pure module function fftfreq(n) result(out)
180+
integer, intent(in) :: n
181+
integer, dimension(n) :: out
182+
end function fftfreq
183+
178184
end interface
179185

180186
!> Version: experimental

src/fftpack_utils.f90

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
submodule(fftpack) fftpack_utils
2+
3+
contains
4+
5+
!> Returns an integer array with the frequency values involved in the
6+
!> performed FFT, ordered in the standard way (zero first, then positive
7+
!> frequencies, then the negative ones).
8+
pure module function fftfreq(n) result(out)
9+
integer, intent(in) :: n
10+
integer, dimension(n) :: out
11+
integer :: i
12+
13+
out(1) = 0
14+
if (n == 1) return
15+
16+
if (mod(n, 2) == 0) then !> n even, smallest n = 2
17+
do i = 2, n/2
18+
out(i) = i-1
19+
end do
20+
out(n/2+1) = -n/2
21+
do i = n/2+2, n !> only enters if n/2+2 <= n
22+
out(i) = out(i-1) + 1
23+
end do
24+
else !> n odd, smallest n = 3
25+
do i = 2, n/2+1
26+
out(i) = i-1
27+
end do
28+
out(n/2+2) = -out(n/2+1)
29+
do i = n/2+3, n !> only enters if n/2+3 <= n
30+
out(i) = out(i-1) + 1
31+
end do
32+
end if
33+
end function fftfreq
34+
35+
end submodule fftpack_utils

0 commit comments

Comments
 (0)