Skip to content

Commit 9d87210

Browse files
committed
Moved Frequencies, fftfreq, rfftfreq from DSP.jl to AbstractFFTs.jl
1 parent 162c162 commit 9d87210

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/AbstractFFTs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module AbstractFFTs
33
export fft, ifft, bfft, fft!, ifft!, bfft!,
44
plan_fft, plan_ifft, plan_bfft, plan_fft!, plan_ifft!, plan_bfft!,
55
rfft, irfft, brfft, plan_rfft, plan_irfft, plan_brfft,
6-
fftshift, ifftshift
6+
fftshift, ifftshift, Frequencies, fftfreq, rfftfreq
77

88
include("definitions.jl")
99

10-
end # module
10+
end # module

src/definitions.jl

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using LinearAlgebra
44
using LinearAlgebra: BlasReal
55
import Base: show, summary, size, ndims, length, eltype,
6-
*, inv, \
6+
*, inv, \, size, step, getindex, iterate
77

88
# DFT plan where the inputs are an array of eltype T
99
abstract type Plan{T} end
@@ -396,6 +396,49 @@ function ifftshift(x,dim)
396396
circshift(x, s)
397397
end
398398

399+
##############################################################################
400+
401+
402+
struct Frequencies{T<:Real} <: AbstractVector{T}
403+
nreal::Int
404+
n::Int
405+
multiplier::T
406+
end
407+
408+
unsafe_getindex(x::Frequencies, i::Int) =
409+
(i-1+ifelse(i <= x.nreal, 0, -x.n))*x.multiplier
410+
function Base.getindex(x::Frequencies, i::Int)
411+
(i >= 1 && i <= x.n) || throw(BoundsError())
412+
unsafe_getindex(x, i)
413+
end
414+
415+
function Base.iterate(x::Frequencies, i::Int=1)
416+
i > x.n ? nothing : (unsafe_getindex(x,i), i + 1)
417+
end
418+
Base.size(x::Frequencies) = (x.n,)
419+
Base.step(x::Frequencies) = x.multiplier
420+
421+
"""
422+
fftfreq(n, fs=1)
423+
Return discrete fourier transform sample frequencies. The returned
424+
Frequencies object is an AbstractVector containing the frequency
425+
bin centers at every sample point. `fs` is the sample rate of the
426+
input signal.
427+
"""
428+
fftfreq(n::Int, fs::Real=1) = Frequencies(((n-1) >> 1)+1, n, fs/n)
429+
430+
"""
431+
rfftfreq(n, fs=1)
432+
Return discrete fourier transform sample frequencies for use with
433+
`rfft`. The returned Frequencies object is an AbstractVector
434+
containing the frequency bin centers at every sample point. `fs`
435+
is the sample rate of the input signal.
436+
"""
437+
rfftfreq(n::Int, fs::Real=1) = Frequencies((n >> 1)+1, (n >> 1)+1, fs/n)
438+
439+
fftshift(x::Frequencies) = (x.nreal-x.n:x.nreal-1)*x.multiplier
440+
441+
399442
##############################################################################
400443

401444
"""

0 commit comments

Comments
 (0)