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

add window option to easyffts #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
name = "EasyFFTs"
uuid = "08be435b-48e7-4090-a646-9e3615ae1968"
authors = ["KronosTheLate"]
version = "0.3.0"
version = "0.4.0"
Copy link
Owner

Choose a reason for hiding this comment

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

I am rather sure that for versions 0.X.Y, incrementing X means that the change is breaking. It is only after the 1.X release that you increment the X for non-breaking changes. So this should be 3.1

Copy link
Owner

Choose a reason for hiding this comment

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

Before changing this, please see my comment on the larger picture.


[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"
Expand Down
35 changes: 22 additions & 13 deletions src/EasyFFTs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module EasyFFTs

using Reexport
@reexport using FFTW
@reexport using DSP

include("EasyFFT_type.jl")
include("plotting.jl")
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/manualtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ef = easyfft(s, fs)


Pkg.offline(true)
try
try
using MakieCore
catch e
Pkg.add("MakieCore")
Expand Down
Loading