diff --git a/Project.toml b/Project.toml index 93c06d3..b731e11 100644 --- a/Project.toml +++ b/Project.toml @@ -1,20 +1,22 @@ name = "EasyFFTs" uuid = "08be435b-48e7-4090-a646-9e3615ae1968" authors = ["KronosTheLate"] -version = "0.3.0" +version = "0.4.0" [deps] +DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2" FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" Term = "22787eb5-b846-44ae-b979-8e399b8463ab" [compat] +DSP = "0.6, 0.7" FFTW = "1" RecipesBase = "1" +Reexport = "1" Term = "1" julia = "1.6" -Reexport = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/src/EasyFFTs.jl b/src/EasyFFTs.jl index 892e286..f9bc923 100644 --- a/src/EasyFFTs.jl +++ b/src/EasyFFTs.jl @@ -3,6 +3,7 @@ module EasyFFTs using Reexport @reexport using FFTW +@reexport using DSP include("EasyFFT_type.jl") include("plotting.jl") @@ -18,8 +19,8 @@ input vector `s`, scaling by `1/length(s)` by default. This function uses FFTW.rfft if `s` has real elements, and FFTW.fft otherwise. -Note that if `s` has real elements, the one-side spectrum -is returned. This means that the amplitude of the frequencies +Note that if `s` has real elements, the one-side spectrum +is returned. This means that the amplitude of the frequencies is doubled, excluding the frequency=0 component. To get the full symmetric spectrum for real signals, use [`easymirror`](@ref), or change the element type of the signal by something like `easyfft(signal.|>ComplexF64)`. The output is an `EasyFFT` object, with fields `freq` and `resp` containing the frequences and @@ -40,26 +41,30 @@ julia> s = sin.(2π * 2 * timestamps); # sine of frequency = 2 Hz julia> easyfft(s, fs) EasyFFT with 51 samples. -Dominant component(s): - Frequency │ Magnitude +Dominant component(s): + Frequency │ Magnitude ╺━━━━━━━━━━━━━┿━━━━━━━━━━━━━╸ - 1.9802 │ 0.98461 + 1.9802 │ 0.98461 julia> easyfft(s) # `fs` defaults to 1 EasyFFT with 51 samples. -Dominant component(s): - Frequency │ Magnitude +Dominant component(s): + Frequency │ Magnitude ╺━━━━━━━━━━━━━┿━━━━━━━━━━━━━╸ - 0.019802 │ 0.98461 - ╵ + 0.019802 │ 0.98461 + ╵ ``` """ function easyfft end export easyfft -function easyfft(s::AbstractVector, fs::Real=1.0; scalebylength=true) - resp = FFTW.fft(s) +function easyfft( + s::AbstractVector, fs::Real=1.0; scalebylength=true, window=DSP.Windows.hanning +) + w = window(length(s)) + + resp = length(s)/sum(w)*FFTW.fft(w .* s) if scalebylength resp ./= length(s) end @@ -69,8 +74,12 @@ function easyfft(s::AbstractVector, fs::Real=1.0; scalebylength=true) return EasyFFT(freq, resp) end -function easyfft(s::AbstractVector{<:Real}, fs::Real=1.0; scalebylength=true) - resp = FFTW.rfft(s) +function easyfft( + s::AbstractVector{<:Real}, fs::Real=1.0; scalebylength=true, window=DSP.Windows.hanning +) + w = window(length(s)) + + resp = length(s)/sum(w)*FFTW.rfft(w .* s) resp[1] /= 2 resp .*= 2 if scalebylength diff --git a/test/manualtests.jl b/test/manualtests.jl index f0fb1ce..f426792 100644 --- a/test/manualtests.jl +++ b/test/manualtests.jl @@ -14,7 +14,7 @@ ef = easyfft(s, fs) Pkg.offline(true) -try +try using MakieCore catch e Pkg.add("MakieCore")