From a561d68843ad114d66bffd32166224c9e2312c8d Mon Sep 17 00:00:00 2001 From: zymon Date: Mon, 25 Apr 2022 15:05:22 +0200 Subject: [PATCH] `STFT.analysis` added. A function for signal analysis using STFT is added. --- src/STFT.jl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/STFT.jl b/src/STFT.jl index 0508906..1614b97 100644 --- a/src/STFT.jl +++ b/src/STFT.jl @@ -3,5 +3,29 @@ module STFT using FFTW +_fft(x::AbstractMatrix{<:Real}, d) = rfft(x, d) +_fft(x::AbstractMatrix{<:Complex}, d) = fft(x, d) + + + +function analysis( + x::AbstractVector{T}, + w::AbstractVector{T}, + L::Integer = 0, + N::Integer = length(w); +)::AbstractMatrix{<:Complex} where {T<:Number} + X = length(x) # Length of the signal in samples + W = length(w) # Length of the window in samples + H = W - L # Hop + S = (X-L) ÷ H # Number of segments + N = N < W ? W : N # DFT size + sc = zeros(T, N, S) # Allocate container for signal segments + + for s ∈ 1:S, n ∈ 1:W # Slice the signal + sc[n, s] = w[n] * x[(s-1)*H+n] + end + _fft(sc, 1) # Convert segments to frequency-domain +end + end # module