Skip to content

Constant-Q in au4 spectrogram - #10142

Merged
saintmatthieu merged 1 commit into
audacity:masterfrom
kgramhans:constq-spectrogram
Jan 15, 2026
Merged

saintmatthieu merged 1 commit into
audacity:masterfrom
kgramhans:constq-spectrogram

Conversation

@kgramhans

@kgramhans kgramhans commented Jan 4, 2026 •

Copy link
Copy Markdown
Contributor

Resolves: #9509

Constant-Q Spectrogram into Audacity 4

The current Pull request deals with optimised calculation of Constant-Q spectrogram which was already added to Audacity 3 code base

  • The bulk part of the code is hidden away in a third-party lib, thus interference with audacity code should be minimal
  • Changes to GUI (Spectrogram settings) have not been settled
  • For demonstration purposes, choosing Window type "Gaussian (a=4.5)" will silently implement a Constant-Q spectrogram (Q=18). This is of course a temporary solution that can be easily reverted
  • I signed CLA
  • The title of the pull request describes an issue it addresses
  • If changes are extensive, then there is a sequence of easily reviewable commits
  • Each commit's message describes its purpose and effects
  • There are no behavior changes unnecessary for the stated purpose of the PR

Recommended:

  • Each commit compiles and runs on my machine without known undesirable changes of behavior

QA:

  • Autobot test cases have been run

@kgramhans

kgramhans commented Jan 4, 2026 •

Copy link
Copy Markdown
Contributor Author

@saintmatthieu
Happy New Year!
I have now progressed on implementing Constant-Q Spectrogram in Audacity 4. Please have a look.
The restructuring of code and the parallelisation has made it orders of magnitude faster

Do you think we are ready to discuss required GUI changes? This should probably be done in a separate issue. I could draft an issue - or how do you prefer to proceed?

Looking forward to your comments!

Regards, Klaus

@kgramhans
kgramhans marked this pull request as ready for review January 4, 2026 20:11
@saintmatthieu
saintmatthieu self-requested a review January 5, 2026 08:30
@saintmatthieu

Copy link
Copy Markdown
Contributor

Happy new year to you, @kgramhans !

I'm first taking a superficial look at this PR. A few early thoughts:

  1. I see that you have a repo for this code (https://github.com/kgramhans/tft). Did you consider not copying it in the audacity repo but using a cmake-fetch instead? You can look in lv2sdk/CMakeLists.txt for examples.
  2. Is it correct to say that a constant-Q spectrogram is a wavelet spectrogram with specific parametrisation?
  3. Minor: what IDE do you use? There a frequent typos, I use the Code Spell Checker extension in VSCode, it will squiggle these.

I just ran your branch. I did this analysis which I think illustrates well what the wavelet transform is about. I'll share it with the team - I've explained what it was about already but this will help I think. Performance-wise it feels totally useable now, too!! Will keep you updated on the outcome of the discussion.
image

@kgramhans

Copy link
Copy Markdown
Contributor Author

@saintmatthieu
In response to your latest comment:

  1. I will take a look. I expect to extend the repo I created with unit tests. It is still somehow sketchy and documentation is lagging
  2. The term "Wavelet" is nice and smooth. It has connotations to an orthogonal time-scale decomposition of a signal which the current Spectrogram is not. I therefore prefer the term "Constant-Q" Spectrogram which puts focus on time/frequency analysis and is conceptually more aligned with conventional Spectrogram. As a matter of fact, all window functions applicable to Short-time Fourier Transform could also be the foundation for a constant-Q Spectrogram although the Gaussian window has some compelling properties (no leaking/optimal simultaneous concentration in tf plane) and I see no real restrictions in making this the window function of choice
  3. I am on MAC, currently experimenting with Qt Creator. But also using Xcode. Both should have options for spell check. I did not focus on spell check, but will take a look

The square wave example is nice: When listening to the signal your ear also reveals both the pulse and sinusoidal natures

Looking forward to your feedback!

@saintmatthieu

Copy link
Copy Markdown
Contributor

I think I understand the difference, thanks for the clarification.
The fact that the analysis as implemented for Audacity isn't orthogonal comes from the fact that the center frequencies are aligned with the pixels on the y scale, correct? It's the case of the straightforward STFT ("Frequencies" algorithm) at least. I ambition to change this in some later work, decoupling the time-frequency grid from the pixel grid so that the analysis is transparent and offers a direct "grip" for spectral editing. This would allow you constant-Q transform to become a proper wavelet transform :) (if I understood you correctly).

Is the wavelet transform invertible? (Even with a Gaussian window? I know why cosine-based windows make STFTs invertible, not sure about Gaussian-based ones.) I'm asking because that is a necessary condition for proper spectral editing.

@kgramhans

Copy link
Copy Markdown
Contributor Author

@saintmatthieu
Well,
the orthogonality refers to a mathematical property that the individual wavelets in the transform form an orthogonal basis and therefore also has an inverse transform

When working with signal analysis I think it is more relevant to favour localisation of analysing functions in frequency and time. This then sacrifices a straightforward inverse transform since coefficients in the time/frequency plane are correlated (as are components in a STFT)

Both Wavelet and STFT can however allow for an inverse transform . In some cases (as for Hann window with special overlap) this can even be an exact inverse. Editing the transform is however not simple since there are correlations between points, for example the Hann window is not super localised in frequency. This means that you cannot just take out some blurp at low frequencies without affecting higher frequencies also

Instead, you could visualize a different approach similar to using an eraser: An interactive tool that would let you smoothly subtract a small amount of time/frequency localized phase-aligned signal: As a user you could gradually erase more and a forward transform would interactively show the effect. Get the picture?

I am working on fetching the TFT lib directly from the repo. Seems to work well - still fighting some CI issues that I expect to solve soon

@saintmatthieu

saintmatthieu commented Jan 6, 2026 •

Copy link
Copy Markdown
Contributor

this can even be an exact inverse

By "invertible" I meant indeed that inverse_transform(transform(x)) = x. I probably should have used "transparent".

It's understood that even the standard STFT must meet a certain window overlap to be transparent. My question is whether this can be the case for the wavelet transform, too, or, in other words, whether spectral editing can be implemented on the basis of the wavelet transform. Someone like @teetow manifested his interest in this, so that he wouldn't need to change the window size so often when spectral-editing events of contrasting time-frequency characteristics.

you cannot just take out some blurp at low frequencies without affecting higher frequencies also

With appropriate signal, window choice and care, spectral-edit artefacts can be reduced to something inaudible. It'll mainly be the responsibility of the user to make sure it's that way. Ours will only be to enforce that editing is only available for invertible transforms (and maybe provide some intelligence in selection tools to reduce the risk of a frequency lobe being cut in its middle and only half of it removed).

@saintmatthieu

saintmatthieu commented Jan 6, 2026 •

Copy link
Copy Markdown
Contributor

An interactive tool that would let you smoothly subtract a small amount of time/frequency localized phase-aligned signal

Yes, that'd be the part of the "intelligence in selection tools" I just mentioned. I'd probably start with the topic after the basic tools are implemented and user feedback is collected.

@kgramhans

Copy link
Copy Markdown
Contributor Author

@saintmatthieu

So-called "tight frames" do exist for wavelet transforms, ie a combination of a wavelet function and a grid in the time/scale plane on which to calculate values. They also provide for reconstructing the original signal from the transform. But at the expense of diverting to "scale" instead of "frequency" which makes time/frequency interpretation more of a challenge. Also, the grid would be a dyadic grid which is very coarse on the scale (frequency) axis (one division per octave) and thus not really useful for audio sounds

In the current implementation there are multiple divisions per octave. Due to constraints on discretisation (sampling is not continuous) it is not really practically feasible to perfectly re-construct the original signal. But by over-sampling in the time/frequency plane you can get pretty close - in theory. There are practical aspects related to the non-uniform areas related to sampling in the time/frequency plane - but my BELIEF is that this is should not be a practical obstacle

As already expressed, I more tend to see the constant-Q spectrogram as nice tool for visualisation along with standard stft - but then again I know so little about Audacity use cases :-). It would be nice to look into some practical examples of what users would like to achieve

@saintmatthieu

Copy link
Copy Markdown
Contributor

Ok, pity that there isn't a straightforward way. I didn't quite understand your explanation about over-sampling. I don't necessarily have to either for now, interesting is mainly how much more complex it would be to make it transparent. (When you say "pretty close", what kind of SNR are we talking about?) If it's awkward and/or doesn't provide sufficient quality, then never mind, the wavelet transform would go in the can't-do-spectral-edits-with-your-settings category and just stay a nice analysis tool :)

@kgramhans

Copy link
Copy Markdown
Contributor Author

@saintmatthieu
In my Thesis I have made some examples on the SNR that can be obtained. I enclose an example (SNR: rightmost column, number of frequency bands per octave: 1st column, decimation parameter: 2nd column)
PXL_20260106_132653882

Regarding overlap in time/frequency plane: Overlap is a pragmatic definition from my side. Considering the bandwidth and duration of the gaussian wavelets constituting the transform, an area in the time/frequency plane pertaining to the wavelets can be defined. By over-populating the plane, an overlap occurs between the wavelet areas

@saintmatthieu

Copy link
Copy Markdown
Contributor

Interesting, thanks for sharing!
Is the decimation parameter the hop size in samples?

@kgramhans

Copy link
Copy Markdown
Contributor Author

Interesting, thanks for sharing! Is the decimation parameter the hop size in samples?

Yes exactly

@saintmatthieu

Copy link
Copy Markdown
Contributor

@kgramhans hi!
I had a discussion with the team today. We will be happy to introduce this feature!
For this PR, I would do something like either not making it available at all (as dead code), or you could consider adding an extra entry to the window types enum that's maybe only available if a macro is defined.
And now there is a follow-up ticket: #10158
I would ask you to please comment on that ticket what current spectrogram settings aren't relevant to the wavelet transform (like window size) and what new settings would be needed. Then our designers can follow up and after that the ticket will be ready for a dev - could be you, me, someone else, we can talk about it again in due time.

@kgramhans

Copy link
Copy Markdown
Contributor Author

@kgramhans hi! I had a discussion with the team today. We will be happy to introduce this feature! For this PR, I would do something like either not making it available at all (as dead code), or you could consider adding an extra entry to the window types enum that's maybe only available if a macro is defined. And now there is a follow-up ticket: #10158 I would ask you to please comment on that ticket what current spectrogram settings aren't relevant to the wavelet transform (like window size) and what new settings would be needed. Then our designers can follow up and after that the ticket will be ready for a dev - could be you, me, someone else, we can talk about it again in due time.

@saintmatthieu Hi again!
From the options listed, I prefer not interfering with the windowType enumeration since since I cannot overlook the consequences throughout the application. Hence, I have simply made the Constant-Q code dead by always returning FALSE from the Au3SpectrogramSettings::isConstantQ() member
I will be commenting on the new issue

Also, as a follow-up action from our discussions, I will conduct a practical experiment on the feasibility of reconstructing the original signal from the Constant-Q transform. I will do that in the context of the tft project, I have created on Github by extending its functionality. I will revert on that whenever I have something to share

@saintmatthieu

Copy link
Copy Markdown
Contributor

@kgramhans I opened a PR on your TFT repository that I'd like you to consider before we proceed with this PR: kgramhans/tft#1
The description of that PR explains why.
Thank you!

@saintmatthieu saintmatthieu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm finished with this review iteration.

@kgramhans

Copy link
Copy Markdown
Contributor Author

@kgramhans I opened a PR on your TFT repository that I'd like you to consider before we proceed with this PR: kgramhans/tft#1 The description of that PR explains why. Thank you!

Thx for the input. I was working on re-structuring the code myself while introducing unit-testing on the project. Therefore I will silently close your PR and incorporate it into those changes

@kgramhans

Copy link
Copy Markdown
Contributor Author

@saintmatthieu
This check fails and I do not understand why. It seems unrelated to my changes. Do you have any idea why?

@saintmatthieu saintmatthieu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM - thank you!

@saintmatthieu

Copy link
Copy Markdown
Contributor

About the failing check: I don't see why. Please try to rebase on latest master (cleaning your commit history in the doing) and let's hope it just goes away.
Then I'll proceed with merging your PR.

LINUX compile issues fixed?

Uncrustify && LINUX build errors

Fixing unittest compile issue

Using FetchContent for building tft lib

This may be the reason for a failing MAC build?

Working on Win compile

Making Constant-Q Spectrogram dead code for now as suggested during review

Code style issue

Review comment: Simplifying project files

Review comments

Fixing checks

Review
@kgramhans

Copy link
Copy Markdown
Contributor Author

About the failing check: I don't see why. Please try to rebase on latest master (cleaning your commit history in the doing) and let's hope it just goes away. Then I'll proceed with merging your PR.

Seems like it cured it. I have then nothing more on the to-do list :-)

@saintmatthieu
saintmatthieu merged commit b7188e3 into audacity:master Jan 15, 2026
@kgramhans
kgramhans deleted the constq-spectrogram branch January 16, 2026 12:51
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.

2 participants