diff --git a/src/bases.jl b/src/bases.jl index 2dbeffc9..fa8770b5 100644 --- a/src/bases.jl +++ b/src/bases.jl @@ -28,13 +28,18 @@ n400(; sfreq = 100) = -hanning(0.4, 0.4, sfreq) """ generate a hanning window -duration: in s -offset: in s, defines hanning peak +width: in s +offset: in s, defines hanning peak, must be > round(width/2) sfreq: sampling rate in Hz """ -function DSP.hanning(duration, offset, sfreq) - signal = hanning(Int(round(duration * sfreq))) - return pad_array(signal, -Int(round(offset * sfreq / 2)), 0) +function DSP.hanning(width, offset, sfreq) + width = width * sfreq + offset = offset * sfreq + signal = hanning(Int(round(width))) + pad_by = Int(round(offset - length(signal) / 2)) + + pad_by < 0 ? error("offset has to be > round(width/2)") : "" + return pad_array(signal, -pad_by, 0) end ## pupil diff --git a/test/bases.jl b/test/bases.jl index 1ac947a2..6b0ae558 100644 --- a/test/bases.jl +++ b/test/bases.jl @@ -1,7 +1,15 @@ using UnfoldSim @testset "hanning" begin - @test UnfoldSim.hanning(0.021, 0.04, 1000)[40] == 1.0 + @test UnfoldSim.hanning(0.021, 0.04, 1000)[41] == 1.0 # why 41 not 40? beacuse round(0.5) = 0 and round(1.5) = 2 -- and we are living on the edge! @test UnfoldSim.hanning(0.011, 0.04, 1000)[40] == 1.0 @test UnfoldSim.hanning(0.011, 0.02, 1000)[20] == 1.0 @test_throws Exception UnfoldSim.hanning(0.011, 0.0, 1000) +end + +@testset "p100,N170,p300,n400" begin + sfreq = 1000 + @test argmax(p100(; sfreq)) == 0.1 * sfreq + @test argmin(n170(; sfreq)) == 0.17 * sfreq + @test argmax(p300(; sfreq)) == 0.3 * sfreq + @test argmin(n400(; sfreq)) == 0.4 * sfreq end \ No newline at end of file