fCWT icon indicating copy to clipboard operation
fCWT copied to clipboard

Can this library do the inverse CWT?

Open robclouth opened this issue 2 years ago • 14 comments

This would be super handy for audio processing

robclouth avatar Feb 12 '23 03:02 robclouth

Not yet, will add it to the list. Are there more people that need this feature?

fastlib avatar Mar 15 '23 19:03 fastlib

An ICWT would be very helpful for my research!

aphoffmann avatar Apr 08 '23 14:04 aphoffmann

yes would be very handy

filefolder avatar Jun 01 '23 02:06 filefolder

I see there is great interest in a fast inverse CWT. In #36, I mentioned the difficulties with implementing an inverse CWT, however I will put it on my list for the next major update. Thank you for showing your interest.

fastlib avatar Jun 29 '23 17:06 fastlib

Yes it would also help me very much !

Side note, do any of you know any sound processing in the wavelet domain ? If existing in python, it would be fontastic to use with neutone AI audio plugin.

nicolasvair avatar Aug 18 '23 08:08 nicolasvair

It will be very helpful!

My research is ML which makes CWT data(image) If the fast ICWT exists, I will be able to put it in the learning loop, so I will have a chance to step further.

InitusNovus avatar Nov 29 '23 20:11 InitusNovus

inverse fcwt will be very helpful ~~~~l

GUIMINLONG avatar Jan 09 '24 00:01 GUIMINLONG

Agreed, I'm also interested in performing the inverse too! Thanks for this great tool!

Sean-M-Devlin avatar Feb 29 '24 21:02 Sean-M-Devlin

Likewise, would be interested in the implementation of inverse fCWT.

vctrbrtn avatar Jun 26 '24 17:06 vctrbrtn

I've been tinkering a bit to see whether an inverse fCWT was possible and it is actually very easy! I'm still planning to implement this in the next version, but due to other priorities in my PhD, this update is being postponed constantly.

For now, you can sum all real valued rows of the time frequency matrix (wavelet coefficient matrix):

signal = np.real(tfm).sum(axis=0)

Note that we are not normalizing anything yet, so this is not correct if you are really interested in the absolute amplitudes of the signal. However, if you are planning to normalize the signal anyway, this could be a straightforward solution!

fastlib avatar Aug 14 '24 21:08 fastlib

Screenshot 2024-12-23 at 22 56 21

The frequency distribution of the reconstructed signal isn't right. Too much high frequency. The quality is great though! Any idea how to correct this other than comparing them and applying an inverse filter?

import fcwt
import numpy as np
import librosa
import matplotlib.pyplot as plt
import soundfile as sf
from scipy.signal import freqz, firwin, lfilter

# Load the audio file
audio_file = "OS_SS_90_drum_loop_swagger.wav"  # Replace with your audio file path
signal, fs = librosa.load(audio_file)

f0 = 10  # lowest frequency
f1 = 20000  # highest frequency
fn = 1000  # number of frequencies

# Calculate CWT without plotting...
freqs, out = fcwt.cwt(signal, fs, f0, f1, fn)
new_signal = np.real(out).sum(axis=0)
# Analyze the frequency response
w, h_orig = freqz(signal)
_, h_recon = freqz(new_signal)
# Calculate frequency responses and plot them
w_normalized = w / np.pi * (fs / 2)  # Convert to Hz
plt.figure(figsize=(10, 6))
plt.plot(w_normalized, 20 * np.log10(np.abs(h_orig)), label="Original")
plt.plot(w_normalized, 20 * np.log10(np.abs(h_recon)), label="Reconstructed")
plt.xlabel("Frequency [Hz]")
plt.ylabel("Magnitude [dB]")
plt.legend()
plt.grid(True)
plt.show()

robclouth avatar Dec 23 '24 22:12 robclouth

Rescaling the output with the inverse of the frequency seems to be mostly correct. Not sure why.

Screenshot 2024-12-26 at 00 05 23
freqs, out = fcwt.cwt(signal, fs, f0, f1, fn)
spectrum = np.real(out)

# rescale spectrum
spectrum *= 1 / (freqs[:, np.newaxis] + 1)

 # inverse CQT
new_signal = spectrum.sum(axis=0)

# normalise
new_signal /= np.max(np.abs(new_signal))

robclouth avatar Dec 26 '24 00:12 robclouth