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

Accommodate AbstractFFTs.jl#26 fftfreq etc. #300

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions docs/src/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ end
unwrap
unwrap!
hilbert
fftfreq
rfftfreq
nextfastfft
pow2db
amp2db
Expand Down
4 changes: 4 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ Base.@deprecate_binding TFFilter PolynomialRatio
Base.@deprecate_binding BiquadFilter Biquad
Base.@deprecate_binding SOSFilter SecondOrderSections

Base.@deprecate Frequencies(nreal::Int, n::Int, multiplier::Float64) FFTW.Frequencies(nreal, n, multiplier)
Base.@deprecate fftfreq(n::Int, fs::Real=1) FFTW.fftfreq(n, fs)
Base.@deprecate rfftfreq(n::Int, fs::Real=1) FFTW.rfftfreq(n, fs)

@deprecate conv2 conv
7 changes: 4 additions & 3 deletions src/periodograms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using ..Util, ..Windows
export arraysplit, nextfastfft, periodogram, welch_pgram, mt_pgram,
spectrogram, power, freq, stft
using FFTW
import FFTW: Frequencies, fftfreq, rfftfreq
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import FFTW: Frequencies, fftfreq, rfftfreq

I believe this isn't necessary, since there's a using FFTW and FFTW reexports the symbols from AbstractFFTs. Same with the import from DSP in the tests.


## ARRAY SPLITTER

Expand Down Expand Up @@ -218,11 +219,11 @@ See also: [`fftfreq`](@ref), [`rfftfreq`](@ref)
freq(p::TFR) = p.freq
freq(p::Periodogram2) = (p.freq1, p.freq2)
FFTW.fftshift(p::Periodogram{T,F}) where {T,F<:Frequencies} =
Periodogram(p.freq.nreal == p.freq.n ? p.power : fftshift(p.power), fftshift(p.freq))
Periodogram(p.freq.n_nonnegative == p.freq.n ? p.power : fftshift(p.power), fftshift(p.freq))
FFTW.fftshift(p::Periodogram{T,F}) where {T,F<:AbstractRange} = p
# 2-d
FFTW.fftshift(p::Periodogram2{T,F1,F2}) where {T,F1<:Frequencies,F2<:Frequencies} =
Periodogram2(p.freq1.nreal == p.freq1.n ? fftshift(p.power,2) : fftshift(p.power), fftshift(p.freq1), fftshift(p.freq2))
Periodogram2(p.freq1.n_nonnegative == p.freq1.n ? fftshift(p.power,2) : fftshift(p.power), fftshift(p.freq1), fftshift(p.freq2))
FFTW.fftshift(p::Periodogram2{T,F1,F2}) where {T,F1<:AbstractRange,F2<:Frequencies} =
Periodogram2(fftshift(p.power,2), p.freq1, fftshift(p.freq2))
FFTW.fftshift(p::Periodogram2{T,F1,F2}) where {T,F1<:AbstractRange,F2<:AbstractRange} = p
Expand Down Expand Up @@ -442,7 +443,7 @@ struct Spectrogram{T,F<:Union{Frequencies,AbstractRange}} <: TFR{T}
time::FloatRange{Float64}
end
FFTW.fftshift(p::Spectrogram{T,F}) where {T,F<:Frequencies} =
Spectrogram(p.freq.nreal == p.freq.n ? p.power : fftshift(p.power, 1), fftshift(p.freq), p.time)
Spectrogram(p.freq.n_nonnegative == p.freq.n ? p.power : fftshift(p.power, 1), fftshift(p.freq), p.time)
FFTW.fftshift(p::Spectrogram{T,F}) where {T,F<:AbstractRange} = p

"""
Expand Down
47 changes: 0 additions & 47 deletions src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,53 +108,6 @@ fftabs2type(::Type{Complex{T}}) where {T<:FFTW.fftwReal} = T
fftabs2type(::Type{T}) where {T<:FFTW.fftwReal} = T
fftabs2type(::Type{T}) where {T<:Union{Real,Complex}} = Float64

## FREQUENCY VECTOR

struct Frequencies <: AbstractVector{Float64}
nreal::Int
n::Int
multiplier::Float64
end

unsafe_getindex(x::Frequencies, i::Int) =
(i-1+ifelse(i <= x.nreal, 0, -x.n))*x.multiplier
function Base.getindex(x::Frequencies, i::Int)
(i >= 1 && i <= x.n) || throw(BoundsError())
unsafe_getindex(x, i)
end
if isdefined(Base, :iterate)
function Base.iterate(x::Frequencies, i::Int=1)
i > x.n ? nothing : (unsafe_getindex(x,i), i + 1)
end
else
Base.start(x::Frequencies) = 1
Base.next(x::Frequencies, i::Int) = (unsafe_getindex(x, i), i+1)
Base.done(x::Frequencies, i::Int) = i > x.n
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
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)
FFTW.fftshift(x::Frequencies) = (x.nreal-x.n:x.nreal-1)*x.multiplier

# Get next fast FFT size for a given signal length
const FAST_FFT_SIZES = [2, 3, 5, 7]
"""
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DSP, FFTW, Test
import DSP: Frequencies, fftfreq, rfftfreq

using Random: seed!

Expand Down
21 changes: 0 additions & 21 deletions test/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,6 @@ using Statistics: mean
end

@testset "fft helpers" begin
## FFTFREQ
@test fftfreq(1) ≈ [0.]
@test fftfreq(2) ≈ [0., -1/2]
@test fftfreq(2, 1/2) ≈ [0., -1/4]
@test fftfreq(3) ≈ [0., 1/3, -1/3]
@test fftfreq(3, 1/2) ≈ [0., 1/6, -1/6]
@test fftfreq(6) ≈ [0., 1/6, 1/3, -1/2, -1/3, -1/6]
@test fftfreq(7) ≈ [0., 1/7, 2/7, 3/7, -3/7, -2/7, -1/7]

@test rfftfreq(1) ≈ [0.]
@test rfftfreq(2) ≈ [0., 1/2]
@test rfftfreq(2, 1/2) ≈ [0., 1/4]
@test rfftfreq(3) ≈ [0., 1/3]
@test rfftfreq(3, 1/2) ≈ [0., 1/6]
@test rfftfreq(6) ≈ [0., 1/6, 1/3, 1/2]
@test rfftfreq(7) ≈ [0., 1/7, 2/7, 3/7]

for n = 1:7
@test fftshift(fftfreq(n)) ≈ fftshift([fftfreq(n);])
end

@test meanfreq(sin.(2*π*10*(0:1e-3:10*π)),1e3) ≈ 10.0 rtol=1e-3

# nextfastfft
Expand Down