Skip to content

Commit c68231e

Browse files
committed
implementing comments
1 parent bf84d0d commit c68231e

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/definitions.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,20 @@ end
400400

401401

402402
struct Frequencies{T<:Number} <: AbstractVector{T}
403-
nreal::Int
403+
n_nonnegative::Int
404404
n::Int
405405
multiplier::T
406+
407+
Frequencies(n_nonnegative::Int, n::Int, multiplier::T) where {T<:Number} = begin
408+
1 n_nonnegative n || error("Condition 1 ≤ n_nonnegative ≤ n isn't satisfied.")
409+
return new{T}(n_nonnegative, n, multiplier)
410+
end
406411
end
407412

408413
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())
414+
(i-1+ifelse(i <= x.n_nonnegative, 0, -x.n))*x.multiplier
415+
@inline function Base.getindex(x::Frequencies, i::Int)
416+
@boundscheck Base.checkbounds(x, i)
412417
unsafe_getindex(x, i)
413418
end
414419

@@ -420,23 +425,23 @@ Base.step(x::Frequencies) = x.multiplier
420425

421426
"""
422427
fftfreq(n, fs=1)
423-
Return discrete fourier transform sample frequencies. The returned
424-
Frequencies object is an AbstractVector containing the frequency
428+
Return the discrete Fourier transform (DFT) sample frequencies for a DFT of length `n`. The returned
429+
`Frequencies` object is an `AbstractVector` containing the frequency
425430
bin centers at every sample point. `fs` is the sample rate of the
426431
input signal.
427432
"""
428-
fftfreq(n::Int, fs::Number=1) = Frequencies(((n-1) >> 1)+1, n, fs/n)
433+
fftfreq(n::Int, fs::Number=1) = Frequencies((n+1) >> 1, n, fs/n)
429434

430435
"""
431436
rfftfreq(n, fs=1)
432-
Return discrete fourier transform sample frequencies for use with
433-
`rfft`. The returned Frequencies object is an AbstractVector
437+
Return the discrete Fourier transform (DFT) sample frequencies for a real DFT of length `n`.
438+
The returned `Frequencies` object is an `AbstractVector`
434439
containing the frequency bin centers at every sample point. `fs`
435440
is the sample rate of the input signal.
436441
"""
437442
rfftfreq(n::Int, fs::Number=1) = Frequencies((n >> 1)+1, (n >> 1)+1, fs/n)
438443

439-
fftshift(x::Frequencies) = (x.nreal-x.n:x.nreal-1)*x.multiplier
444+
fftshift(x::Frequencies) = (x.n_nonnegative-x.n:x.n_nonnegative-1)*x.multiplier
440445

441446

442447
##############################################################################

0 commit comments

Comments
 (0)