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

Implement Short-time Fourier transform and its inverse #587

Merged
merged 13 commits into from
Jul 4, 2024

Conversation

pxl-th
Copy link
Member

@pxl-th pxl-th commented May 26, 2024

  • Implement Short-time Fourier transform (STFT) and its inverse (iSTFT), modelled after torch.stft and torch.istft respectively.
  • Add window functions (hann, hamming).
  • Add Mel-scale filterbanks.
  • Add tests.
  • Add test/Project.toml file and install GPU backends only when testing them.
    Currrently latest CUDA does not support LLVM 7, but AMDGPU uses only it. Without this fix, we can't install all packages needed for testing.
  • Add spectrogram function spectrogram and conversion from power to decibel (power_to_db) units and vice versa (db_to_power).

Spectrogram Example:

julia> x, sr = load("jfk.flac")
julia> spec = spectrogram(reshape(Float32.(x), :);
    n_fft=1024, hop_length=128, window=hann_window(1024))
julia> spec_db = power_to_db(spec)
julia> Makie.heatmap(transpose(spec_db))

image

PR Checklist

  • Tests are added
  • Documentation, if applicable

@pxl-th pxl-th requested a review from darsnack May 27, 2024 08:11
@pxl-th
Copy link
Member Author

pxl-th commented Jun 5, 2024

I'm not sure if NNlib.jl is the right place for audio stuff. But I figured since torch has it, we can too, for convenience

Copy link
Member

@darsnack darsnack left a comment

Choose a reason for hiding this comment

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

Looks pretty good to me. The only thing might be adding some basic gradient tests. The bulk of gradient testing is already in AbstractFFTs.

src/audio/spectrogram.jl Show resolved Hide resolved
src/audio/spectrogram.jl Outdated Show resolved Hide resolved
src/audio/spectrogram.jl Outdated Show resolved Hide resolved
src/audio/spectrogram.jl Outdated Show resolved Hide resolved

julia> spec_db = NNlib.power_to_db(spec);

julia> Makie.heatmap(spec_db[:, :, 1])
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a UnicodePlot with output would be nicer here (and fit other docstrings in NNlib)

Copy link
Member Author

Choose a reason for hiding this comment

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

See output for UnicodePlot for heatmap: https://fluxml.ai/NNlib.jl/previews/PR587/audio/#Spectrogram

Not sure how I feel about it. Maybe it is worth switching to Plots.jl?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah it's not ideal, but my inclination towards UnicodePlots is so that docstrings render completely in the REPL as well. I'm happy to use Makie or Plots instead though.

Let's solicit opinions from @mcabbott or @ToucheSir.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated to use Makie plots, here's UnicodePlots for reference:

UnicodePlots Makie
image image

src/audio/stft.jl Outdated Show resolved Hide resolved
src/audio/stft.jl Outdated Show resolved Hide resolved
src/audio/stft.jl Outdated Show resolved Hide resolved
src/audio/stft.jl Outdated Show resolved Hide resolved
src/audio/stft.jl Outdated Show resolved Hide resolved
- Convert examples to doctests or evaluate during build time
-More tests
@pxl-th
Copy link
Member Author

pxl-th commented Jun 8, 2024

I've addressed comments. I think we also need higher level API in future, like Spectrogram (or MelSpectrogram), that will hold parameters like window, filter banks on the specified device and other parameters. They can be added in Flux.jl though

@pxl-th pxl-th requested a review from darsnack June 15, 2024 13:19
@pxl-th
Copy link
Member Author

pxl-th commented Jun 15, 2024

@darsnack, kind bump

Project.toml Outdated
@@ -6,6 +6,7 @@ version = "0.9.17"
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Atomix = "a9b6321e-bd34-4604-b9c9-b65b8de01458"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
Copy link
Member

Choose a reason for hiding this comment

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

Can we make this a weak dependence and move any function relying on FFTW under an extension?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@CarloLucibello CarloLucibello merged commit 4159154 into master Jul 4, 2024
13 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants