3
$\begingroup$

I just want to understand, why is the result of my wavelet(?) transform so bad.

  • For $0\le i< k$, where I set $k$ to $10$, I split the signal in blocks of length $s_i:=2^{i+2}$, overlapping by $s/2$. I then take the absolute value of the dot product of the block with $[e^{i4\pi(k+0.5)/s_i}]_{0\le k<s_i}$ and store it in $F_{ij}$.
  • For the reconstruction, I choose random phases $p_i\in[0,2\pi)$ and then recreate a block by multiplying $F_{ij}$ with the window $[\sin((k+0.5)/s_i)^2]_{0\le k<s_i}$ and the oscillator $[\sin(4\pi(k+0.5)/2+p_i)]_{0<k\le s_i}$.

I'm aware that I loose some information in this process, but I wonder why the result sounds really bad, with high frequencies dominating the result.

Visually, I think it doesn't look too bad (blue is the original signal, orange is the transform). You can see that the phase is randomized, but the frequencies look OK to me.

Looking at single frequency bands, it also doesn't look too bad?

$\endgroup$
6
  • 1
    $\begingroup$ This is a long debugging problem (for which you've not shown enough detail) so here's what may help: 1, 2. $\endgroup$ Commented Mar 23, 2023 at 11:50
  • $\begingroup$ Thanks a lot for the comment! I now rewrote my algorithm, so that I start with the lowest frequency, then I sample the frequency, and then I subtract the inverse transform from the signal so that only the higher frequencies remain in the signal. Without removing the phase, the result is this. It looks and sounds better than my previous approach. Here is the code. $\endgroup$
    – fweth
    Commented Mar 23, 2023 at 13:59
  • $\begingroup$ This is also a nice plot, showing the convergence of the approximation. $\endgroup$
    – fweth
    Commented Mar 23, 2023 at 14:06
  • 1
    $\begingroup$ Neat. FYI, a welcomed expression of gratitude is to vote on what you found helpful. $\endgroup$ Commented Mar 23, 2023 at 15:18
  • 1
    $\begingroup$ Ah sorry, I usually do this but forgot today! But I think I can close the question now, or is there anything valuable for the community to let it remain here? $\endgroup$
    – fweth
    Commented Mar 23, 2023 at 15:22

2 Answers 2

3
$\begingroup$

Inverting CWT modulus

The problem is known as phase retrieval, and for |CWT|, the transform is proven invertible to within a global phase shift, $e^{j\omega_0}$, in (1), and an algorithm for recovery is provided in (2):

  1. Phase recovery for the Cauchy wavelet transform
  2. Phase retrieval for wavelet transforms -- code

This is excellent invertibility for a wide range of signals. However, it assumes unit stride, or hop_size=1, or maximum overlap: hop_size > 1 aliases the scalogram and loses information (FR 1). Hopping by half the support of the wavelet is large and will lose a lot.


Answer's algorithm

As written, it's neither DWT nor CWT, but rather CWT with variable stride - which is a valid feature engineering with CWT. With DWT, the answer is simple - recovery from modulus is as good as recovery from |DFT|, i.e. terrible. With multi-rate CWT, the hop size controls information loss (FR 1). And the Fourier phase, for most purposes, is more important than magnitude, so randomizing phase on per-coefficient basis isn't sound.

But there's other problems. The algorithm has both poor time and poor frequency resolution, per absence of windowing in time domain (or equivalently rectangular windowing). It then inverts with windowing for some reason. Information can also be lost in other ways; there's many ways to do a wavelet transform wrong (FR 2). Pay attention to padding and unpadding; if you're not padding, it's exacerbating the hop problem near edges.

I'm unsure what the algorithm's trying to accomplish, but I suggest first getting comfortable with CWT and its inversion with and without stride. A survey of DWT might also help.


Further reading (FR)

  1. Role of window length and overlap in uncertainty principle - gives demo of aliasing due to hop_size, and provides measure of information loss. hop_size > 1 is OK, but there's also "too much". Note STFT is just non-logscaled CWT (for the described purposes).
  2. How to validate a wavelet filterbank? - shows nearly all ways in which CWT can go wrong.
  3. How to test wavelet transforms? - useful test signals. I commented that OP's results are "luck" - that's because even the worst filterbank will work on pure sines of particular frequencies. The TL;DR is to try an exponential chirp with full sweep, or better, x = echirp(); x += x[::-1] (see librosa.chirp or ssqueezepy.TestSignals.echirp).
$\endgroup$
5
  • $\begingroup$ Thanks a lot for the answer. As to the question what I was trying to achieve, I was wondering if there exists a good way to have a phase less representation of an audio signal, up to perceptional equivalence. I noted e.g. that the STFT with short windows is less sensitive to a random change in phase, whereas in the STFT with large windows, all the information seems to be stored in the phase, I can replace the frequencies with a completely flat band and still be able to understand the spoken words. Based on this, I wondered how it works with the wavelet transform. $\endgroup$
    – fweth
    Commented Mar 24, 2023 at 13:31
  • $\begingroup$ Ah, and yes, you are right about windowing the input signal, the result looks much better! i.imgur.com/bjbi3ur.png $\endgroup$
    – fweth
    Commented Mar 24, 2023 at 13:37
  • 1
    $\begingroup$ @fweth That's a separate question and I don't have a complete answer, but some comments. I don't think it's anything fundamentally about window sizes alone, but windows in relation to hop size and n_fft. One way to think of it is larger window STFT is approaching the DFT, and smaller window is approaching analytic signal which contains most of the information for some signals. Fundamentally though it should be about overlap in freq-dom, which happens to be greater with common library settings. $\endgroup$ Commented Mar 24, 2023 at 13:40
  • $\begingroup$ Thanks a lot. Maybe a more concrete phrasing of my question: Assume you have frequency bands $A_f(t)\sin(\omega t+\phi)$ where $\phi\in[0,2\pi)$ is the phase shift and $A'_f(\omega)<d\omega$ where $d>0$ is some constant. How many such frequency bands (and which frequencies) do I need to be able to represent any signal up to perceptional equivalence, i.e. I find a signal in the space just defined which sounds like the original signal to the human ear? Are power of 2 frequencies (like in the DWT) enough? Note that in this model, I fix the phase shift to some random combination. $\endgroup$
    – fweth
    Commented Mar 25, 2023 at 16:28
  • $\begingroup$ @fweth That's yet more a separate question. StackExchange isn't for back & forths with extended scopes of the question. $\endgroup$ Commented Mar 26, 2023 at 10:03
1
$\begingroup$

I just write a short answer, because I mostly resolved the issue. It helped me to begin my transformation with the lowest frequency, and then subtract the inverse transform of the frequency band from the signal. This way the higher frequencies are more accurately represented.

Running the same process more than once (on the remainder of the signal), I could improve the accuracy even more.

However, randomizing the phase still audibly distorted the signal a lot. I assumed the phase wouldn't be so important in this type of transformation (w.r.t. perception), but apparently it still is.

Here you can see a variant of the code, where I used $4i\pi$ instead of $2i\pi$ inside the exponential (in order to get cleaner frequency bands). I ran the process described above $3$ times, and you can see how the remainder of the singal approaches $0$.

$\endgroup$
2
  • $\begingroup$ The useful part of this answer is on phase, other stuff is luck. You can (and should) self-accept when able so the Q&A isn't bumped up for years. $\endgroup$ Commented Mar 24, 2023 at 11:17
  • $\begingroup$ Will do in 23 hours! But if you feel there is something lacking to the answer, feel free to provide your own, and I'll accept your answer of course. $\endgroup$
    – fweth
    Commented Mar 24, 2023 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.