Skip to content

UCD4IDS/isqSTFT.py

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Inverse Synchrosqueezing Transform (i-sqSTFT)

This Python script provides an implementation of the Inverse Synchrosqueezing Transform (i-sqSTFT) for one-dimensional signals. It works in conjunction with the sq_stft.py script, which provides the forward Synchrosqueezing Transform. The inverse transform aims to reconstruct the original time-domain signal from its Synchrosqueezed representation.

How it Works

The reconstruction method implemented here leverages both the original Short-Time Fourier Transform (STFT) matrix (TFR) and the Synchrosqueezed STFT matrix (RTFR). The RTFR is used to create a mask that highlights the dominant frequency components. This mask is then applied to the TFR, preserving the crucial phase information from the original STFT only for the synchrosqueezed components. Finally, a standard Inverse Short-Time Fourier Transform (ISTFT) is performed on this masked TFR to recover the time-domain signal.

Dependencies

The script requires the following Python libraries:

  • numpy
  • scipy
  • matplotlib (for the example usage)
  • sq_stft (the forward Synchrosqueezing Transform script, which must be in the same directory or accessible via Python path)

You can install numpy, scipy, and matplotlib using pip:

pip install numpy scipy matplotlib

Ensure that sq_stft.py is present in the same directory as inverse_sqSTFT.py for successful execution.

Functionality

i_sqSTFT(TFR, RTFR, fs, t, window, nperseg, nfft)

This is the main function that computes the Inverse Synchrosqueezing Transform.

Parameters:

  • TFR (2D numpy array): The original STFT matrix (positive frequencies) obtained from sqSTFT.
  • RTFR (2D numpy array): The synchrosqueezed STFT matrix (positive frequencies) obtained from sqSTFT.
  • fs (float): The sampling frequency of the original signal.
  • t (1D numpy array): The time steps used in the forward STFT.
  • window (str): The name of the window function used in the forward STFT (e.g., 'hann').
  • nperseg (int): The number of samples per segment used in the forward STFT.
  • nfft (int): The number of points for the FFT used in the forward STFT.

Returns:

  • np.ndarray: The reconstructed time-domain signal.

Usage

The script can be run directly from the command line to see an example of the inverse Synchrosqueezing Transform applied to a chirp signal.

python inverse_sqSTFT.py

This will generate several plots:

  1. Original Signal: The synthetic chirp signal used for the demonstration.
  2. Reconstructed Signal (from i-sqSTFT): The signal recovered after applying the forward and inverse Synchrosqueezing Transforms.
  3. Difference (Error): The point-wise difference between the original and reconstructed signals, illustrating the accuracy of the reconstruction.
  4. Synchrosqueezed Representation (RTFR): The time-frequency representation after the forward Synchrosqueezing Transform.
  5. Masked Original STFT (TFR): The original STFT with the mask derived from the RTFR applied, showing which components are used for reconstruction.

These plots collectively demonstrate the process and the fidelity of the signal reconstruction using the inverse Synchrosqueezing Transform.

Example from the script:

import numpy as np
from scipy.signal import chirp
import matplotlib.pyplot as plt
from sq_stft import sqSTFT # Assuming sq_stft.py is available

# 1. Create a synthetic signal
fs = 1000
T = 2
time_vec = np.linspace(0, T, int(fs * T), endpoint=False)
f_start = 10
f_end = fs / 4

sig = chirp(time_vec, f0=f_start, f1=f_end, t1=T, method='linear')
sig = sig / np.max(np.abs(sig))

# 2. Define parameters for transforms
n_fft = 512
n_perseg = 129
hop_size = 2
t_steps = np.arange(0, len(sig), hop_size)
window_type = 'hann'

# 3. Perform forward sqSTFT
f, times, TFR, RTFR = sqSTFT(sig, fs=fs, t=t_steps, window=window_type, nperseg=n_perseg, nfft=n_fft)

# 4. Perform inverse sqSTFT
reconstructed_signal = i_sqSTFT(TFR, RTFR, fs=fs, t=t_steps, window=window_type, nperseg=n_perseg, nfft=n_fft)

# ... (plotting and comparison code) ...
Figure_1 Figure_2

About

Performs an inverse Synchrosqueezed Short-Time Fourier Transform.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%