-
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Short-time Fourier transform and its inverse (#587)
* Initial stft implementation * Finish STFT * Cleanup * Bump AMDGPU compat * Install GPU backends only when testing them * Add spectrogram * Move audio documentation to its own page - Convert examples to doctests or evaluate during build time -More tests * Fixes * Use Makie for spectrogram plots * Add mel-scale filterbanks * Run doctests when building documentation instead of a separate CI stage * Minor fix * Move FFTW-dependent functions to extension
- Loading branch information
Showing
19 changed files
with
808 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
[deps] | ||
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" | ||
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
FLAC = "abae9e3b-a9a0-4778-b5c6-ca109b507d99" | ||
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" | ||
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" | ||
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd" | ||
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228" | ||
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Reference | ||
|
||
!!! note | ||
Spectral functions require importing `FFTW` package to enable them. | ||
|
||
## Window functions | ||
|
||
```@docs | ||
hann_window | ||
hamming_window | ||
``` | ||
|
||
## Spectral | ||
|
||
```@docs | ||
stft | ||
istft | ||
NNlib.power_to_db | ||
NNlib.db_to_power | ||
``` | ||
|
||
## Spectrogram | ||
|
||
```@docs | ||
melscale_filterbanks | ||
spectrogram | ||
``` | ||
|
||
Example: | ||
|
||
```@example 1 | ||
using FFTW # <- required for STFT support. | ||
using NNlib | ||
using FileIO | ||
using Makie, CairoMakie | ||
CairoMakie.activate!() | ||
waveform, sampling_rate = load("./assets/jfk.flac") | ||
fig = lines(reshape(waveform, :)) | ||
save("waveform.png", fig) | ||
# Spectrogram. | ||
n_fft = 1024 | ||
spec = spectrogram(waveform; n_fft, hop_length=n_fft ÷ 4, window=hann_window(n_fft)) | ||
fig = heatmap(transpose(NNlib.power_to_db(spec)[:, :, 1])) | ||
save("spectrogram.png", fig) | ||
# Mel-scale spectrogram. | ||
n_freqs = n_fft ÷ 2 + 1 | ||
fb = melscale_filterbanks(; n_freqs, n_mels=128, sample_rate=Int(sampling_rate)) | ||
mel_spec = permutedims(spec, (2, 1, 3)) ⊠ fb # (time, n_mels) | ||
fig = heatmap(NNlib.power_to_db(mel_spec)[:, :, 1]) | ||
save("mel-spectrogram.png", fig) | ||
nothing # hide | ||
``` | ||
|
||
|Waveform|Spectrogram|Mel Spectrogram| | ||
|:---:|:---:|:---:| | ||
|||| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module NNlibFFTWExt | ||
|
||
using FFTW | ||
using NNlib | ||
using KernelAbstractions | ||
|
||
include("stft.jl") | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
function NNlib.stft(x; | ||
n_fft::Int, hop_length::Int = n_fft ÷ 4, window = nothing, | ||
center::Bool = true, normalized::Bool = false, | ||
) | ||
kab = get_backend(x) | ||
use_window = !isnothing(window) | ||
|
||
use_window && kab != get_backend(window) && throw(ArgumentError( | ||
"`window` must be on the same device as stft input `x` ($kab), \ | ||
instead: `$(get_backend(window))`.")) | ||
use_window && !(0 < length(window) ≤ n_fft) && throw(ArgumentError( | ||
"Expected `0 < length(window) ≤ n_fft=$n_fft`, \ | ||
but got `length(window)=$(length(window))`.")) | ||
hop_length < 0 && throw(ArgumentError( | ||
"Expected `hop_length > 0`, but got `hop_length=$hop_length`.")) | ||
|
||
# Pad window on both sides with `0` to `n_fft` length if needed. | ||
if use_window && length(window) < n_fft | ||
left = ((n_fft - length(window)) ÷ 2) + 1 | ||
tmp = KernelAbstractions.zeros(kab, eltype(window), n_fft) | ||
tmp[left:left + length(window) - 1] .= window | ||
window = tmp | ||
end | ||
|
||
if center | ||
pad_amount = n_fft ÷ 2 | ||
x = pad_reflect(x, pad_amount; dims=1) | ||
end | ||
|
||
n = size(x, 1) | ||
(0 < n_fft ≤ n) || throw(ArgumentError( | ||
"Expected `0 < n_fft ≤ size(x, 1)=$n`, but got `n_fft=$n_fft`.")) | ||
|
||
n_frames = 1 + (n - n_fft) ÷ hop_length | ||
|
||
# time2col. | ||
# Reshape `x` to (n_fft, n_frames, B) if needed. | ||
# Each row in `n_frames` is shifted by `hop_length`. | ||
if n_frames > 1 | ||
# TODO can be more efficient if we support something like torch.as_strided | ||
ids = [ | ||
row + hop_length * col | ||
for row in 1:n_fft, col in 0:(n_frames - 1)] | ||
x = x[ids, ntuple(_ -> Colon(), ndims(x) - 1)...] | ||
end | ||
|
||
region = 1 | ||
use_window && (x = x .* window;) | ||
y = eltype(x) <: Complex ? fft(x, region) : rfft(x, region) | ||
|
||
normalized && (y = y .* eltype(y)(n_fft^-0.5);) | ||
return y | ||
end | ||
|
||
function NNlib.istft(y; | ||
n_fft::Int, hop_length::Int = n_fft ÷ 4, window = nothing, | ||
center::Bool = true, normalized::Bool = false, | ||
return_complex::Bool = false, | ||
original_length::Union{Nothing, Int} = nothing, | ||
) | ||
kab = get_backend(y) | ||
use_window = !isnothing(window) | ||
|
||
use_window && kab != get_backend(window) && throw(ArgumentError( | ||
"`window` must be on the same device as istft input `y` ($kab), \ | ||
instead: `$(get_backend(window))`.")) | ||
use_window && !(0 < length(window) ≤ n_fft) && throw(ArgumentError( | ||
"Expected `0 < length(window) ≤ n_fft=$n_fft`, \ | ||
but got `length(window)=$(length(window))`.")) | ||
hop_length < 0 && throw(ArgumentError( | ||
"Expected `hop_length > 0`, but got `hop_length=$hop_length`.")) | ||
|
||
# TODO check `y` eltype is complex | ||
|
||
n_frames = size(y, 2) | ||
|
||
# Pad window on both sides with `0` to `n_fft` length if needed. | ||
if use_window && length(window) < n_fft | ||
left = ((n_fft - length(window)) ÷ 2) + 1 | ||
tmp = KernelAbstractions.zeros(kab, eltype(window), n_fft) | ||
tmp[left:left + length(window) - 1] .= window | ||
window = tmp | ||
end | ||
|
||
# Denormalize. | ||
normalized && (y = y .* eltype(y)(n_fft^0.5);) | ||
|
||
region = 1 | ||
x = return_complex ? ifft(y, region) : irfft(y, n_fft, region) | ||
|
||
# De-apply window. | ||
use_window && (x = x ./ window;) | ||
|
||
# col2time. | ||
expected_output_len = n_fft + hop_length * (n_frames - 1) | ||
|
||
ids = Vector{Int}(undef, expected_output_len) | ||
in_idx, out_idx = 0, 0 | ||
prev_e, v = 0, 0 | ||
|
||
for col in 0:(n_frames - 1) | ||
for row in 1:n_fft | ||
in_idx += 1 | ||
v = row + hop_length * col | ||
v > prev_e || continue | ||
|
||
out_idx += 1 | ||
ids[out_idx] = in_idx | ||
end | ||
prev_e = v | ||
end | ||
|
||
# In case of batched input, reshaped it (n_fft, n_frames, batch) -> (:, batch). | ||
nd = ntuple(_ -> Colon(), ndims(x) - 2) | ||
ndims(x) == 3 && (x = reshape(x, (:, size(x, 3)));) | ||
x = x[ids, nd...] | ||
|
||
# Trim padding. | ||
left = center ? (n_fft ÷ 2 + 1) : 1 | ||
right = if isnothing(original_length) | ||
center ? (size(x, 1) - n_fft ÷ 2) : expected_output_len | ||
else | ||
left + original_length - 1 | ||
end | ||
x = x[left:right, nd...] | ||
return x | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.