Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Frequencies, fftfreq, rfftfreq from DSP.jl to AbstractFFTs.jl #26

Merged
merged 6 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/AbstractFFTs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module AbstractFFTs
export fft, ifft, bfft, fft!, ifft!, bfft!,
plan_fft, plan_ifft, plan_bfft, plan_fft!, plan_ifft!, plan_bfft!,
rfft, irfft, brfft, plan_rfft, plan_irfft, plan_brfft,
fftshift, ifftshift
fftshift, ifftshift, Frequencies, fftfreq, rfftfreq

include("definitions.jl")

end # module
end # module
carstenbauer marked this conversation as resolved.
Show resolved Hide resolved
45 changes: 44 additions & 1 deletion src/definitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using LinearAlgebra
using LinearAlgebra: BlasReal
import Base: show, summary, size, ndims, length, eltype,
*, inv, \
*, inv, \, size, step, getindex, iterate

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

##############################################################################


struct Frequencies{T<:Real} <: AbstractVector{T}
nreal::Int
n::Int
multiplier::T
carstenbauer marked this conversation as resolved.
Show resolved Hide resolved
end

unsafe_getindex(x::Frequencies, i::Int) =
(i-1+ifelse(i <= x.nreal, 0, -x.n))*x.multiplier
carstenbauer marked this conversation as resolved.
Show resolved Hide resolved
function Base.getindex(x::Frequencies, i::Int)
(i >= 1 && i <= x.n) || throw(BoundsError())
carstenbauer marked this conversation as resolved.
Show resolved Hide resolved
unsafe_getindex(x, i)
end

function Base.iterate(x::Frequencies, i::Int=1)
i > x.n ? nothing : (unsafe_getindex(x,i), i + 1)
end
Base.size(x::Frequencies) = (x.n,)
Base.step(x::Frequencies) = x.multiplier

"""
fftfreq(n, fs=1)
Return discrete fourier transform sample frequencies. The returned
carstenbauer marked this conversation as resolved.
Show resolved Hide resolved
Frequencies object is an AbstractVector containing the frequency
bin centers at every sample point. `fs` is the sample rate of the
input signal.
"""
fftfreq(n::Int, fs::Real=1) = Frequencies(((n-1) >> 1)+1, n, fs/n)

"""
rfftfreq(n, fs=1)
Return discrete fourier transform sample frequencies for use with
`rfft`. The returned Frequencies object is an AbstractVector
containing the frequency bin centers at every sample point. `fs`
is the sample rate of the input signal.
"""
rfftfreq(n::Int, fs::Real=1) = Frequencies((n >> 1)+1, (n >> 1)+1, fs/n)

fftshift(x::Frequencies) = (x.nreal-x.n:x.nreal-1)*x.multiplier


##############################################################################

"""
Expand Down