Compare commits

..

No commits in common. "main" and "v1.0.1" have entirely different histories.
main ... v1.0.1

3 changed files with 24 additions and 62 deletions

View file

@ -1,13 +1,13 @@
name = "STFT" name = "STFT"
uuid = "58bb99bf-048b-48b7-93e7-1cbf3ee61509" uuid = "58bb99bf-048b-48b7-93e7-1cbf3ee61509"
authors = ["Szymon M. Woźniak <s@zymon.org>"] authors = ["Szymon M. Woźniak"]
version = "1.3.0" version = "1.0.1"
[deps] [deps]
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890"
[compat] [compat]
FFTW = "1.7"
LoopVectorization = "0.12.101" FFTW = "1.4"
julia = "1.6" julia = "1.6"

View file

@ -19,7 +19,7 @@ To install this package from General registry, use the following command in Juli
``` ```
Alternatively, directly via repository: Alternatively, directly via repository:
```julia ```julia
pkg> add https://codeberg.org/zymon/STFT.jl pkg> add https://git.sr.ht/~zymon/STFT.jl
``` ```

View file

@ -1,25 +1,12 @@
module STFT module STFT
using LoopVectorization
using FFTW using FFTW
export stft, istft export stft, istft
_fft(x::AbstractArray{<:Real}, d) = rfft(x, d) _fft(x::AbstractMatrix{<:Real}, d) = rfft(x, d)
_fft(x::AbstractArray{<:Complex}, d) = fft(x, d) _fft(x::AbstractMatrix{<:Complex}, d) = fft(x, d)
function malloc_stft(
x::M,
w::V,
L::I = zero(I),
N::I = length(w);
) where {T<:Number, I<:Integer, V<:AbstractVector{T}, M<:AbstractMatrix{T}}
X, K = size(x) # Length of the signal in samples
W = length(w) # Length of the window in samples
S = (X-L) ÷ (W - L) # Number of segments
N = N < W ? W : N # DFT size
zeros(T, (N, S, K)) # Allocate container for signal segments
end
doc_analysis = """ doc_analysis = """
@ -107,31 +94,24 @@ function analysis() end
stft(x, w, L=0, N=length(w)) = analysis(x, w, L, N) stft(x, w, L=0, N=length(w)) = analysis(x, w, L, N)
function analysis( function analysis(
x::M, x::AbstractVector{T},
w::V, w::AbstractVector{T},
L::I = zero(I), L::Integer = 0,
N::I = length(w); N::Integer = length(w);
) where {T<:Number, I<:Integer, V<:AbstractVector{T}, M<:AbstractMatrix{T}} )::AbstractMatrix{<:Complex} where {T<:Number}
sc = malloc_stft(x, w, L, N) X = length(x) # Length of the signal in samples
N, S, K = sc |> size W = length(w) # Length of the window in samples
W = w |> length 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
@turbo for k 1:K, s 1:S, n 1:W for s 1:S, n 1:W # Slice the signal
sc[n, s, k] = w[n] * x[(s-1)*(W-L)+n, k] sc[n, s] = w[n] * x[(s-1)*H+n]
end end
_fft(sc, 1) # Convert segments to frequency-domain _fft(sc, 1) # Convert segments to frequency-domain
end end
function analysis(
x::V,
w::V,
L::I = zero(I),
N::I = length(w);
)::Matrix{T |> complex} where {T<:Number, I<:Integer, V<:AbstractVector{T}}
xx = @view x[:,:]
@view analysis(xx, w, L, N)[:, :, 1]
end
function analysis( function analysis(
xs::AbstractArray{<:AbstractVector{T}}, xs::AbstractArray{<:AbstractVector{T}},
w::AbstractVector{T}, w::AbstractVector{T},
@ -249,32 +229,14 @@ function synthesis(
xs = irfft(X, N, 1) # Convert segments to time-domain xs = irfft(X, N, 1) # Convert segments to time-domain
@turbo for s 0:(S-1) for s 1:S
ss = s*H # Segment shift ss = (s-1)*H # Segment start
for n = 1:W for n = 1:W
xn[ss+n] += xs[n, s+1] * w[n] xn[ss+n] += xs[n, s] * w[n]
xd[ss+n] += [n] xd[ss+n] += [n]
end end
end end
xn ./ xd # Normalize xn ./ xd # Normalize
end end
"""
Real-value signal STFT with constant input size.
"""
function rSTFTm(A, w, L, N=length(w))
mem = STFT.malloc_stft(A, w, L, N)
N, S, K = mem |> size
W = w |> length
P = plan_rfft(mem, 1)
function f(x)
@turbo for k 1:K, s 1:S, n 1:W
mem[n, s, k] = w[n] * x[(s-1)*(W-L)+n, k]
end
P * mem
end
return f
end
end # module end # module