|
3 | 3 | using LinearAlgebra
|
4 | 4 | using LinearAlgebra: BlasReal
|
5 | 5 | import Base: show, summary, size, ndims, length, eltype,
|
6 |
| - *, inv, \ |
| 6 | + *, inv, \, size, step, getindex, iterate |
7 | 7 |
|
8 | 8 | # DFT plan where the inputs are an array of eltype T
|
9 | 9 | abstract type Plan{T} end
|
@@ -396,6 +396,49 @@ function ifftshift(x,dim)
|
396 | 396 | circshift(x, s)
|
397 | 397 | end
|
398 | 398 |
|
| 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 | + |
399 | 442 | ##############################################################################
|
400 | 443 |
|
401 | 444 | """
|
|
0 commit comments