### Ridge Extraction Example (Python) Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/README.md Demonstrates the ridge extraction functionality within the ssqueezepy library. This snippet is part of the examples provided by contributor David Bondesson. ```python # Example usage of ridge extraction (specific code not provided in text) # Refer to ridge_extraction.py and examples/extracting_ridges.py for details. ``` -------------------------------- ### Compute Synchrosqueezed CWT (SSQ_CWT) Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Performs synchrosqueezed CWT to achieve sharper time-frequency localization. Includes examples for custom wavelets, parameter tuning, and inverse synchrosqueezing. ```python import numpy as np from ssqueezepy import ssq_cwt, issq_cwt, Wavelet N = 2048 t = np.linspace(0, 10, N, endpoint=False) x = np.cos(2 * np.pi * 2 * (np.exp(t / 2.2) - 1)) Tx, Wx, ssq_freqs, scales = ssq_cwt(x, wavelet='gmw') wavelet = Wavelet(('gmw', {'gamma': 3, 'beta': 60})) Tx, Wx, ssq_freqs, scales = ssq_cwt(x, wavelet=wavelet, scales='log-piecewise', nv=32, fs=1/t[1], gamma=1e-6, squeezing='sum') x_reconstructed = issq_cwt(Tx, wavelet='gmw') ``` -------------------------------- ### Extracting Ridges from CWT and SSQ-CWT of Two Chirp Signals Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/examples/ridge_extraction/README.md Illustrates ridge extraction from CWT and SSQ-CWT for a signal containing a linear chirp and a quadratic chirp. The example showcases the `extract_ridges` function with different `padtype` and `penalty` settings, and visualizes the results. ```python "Linear + quadratic chirp." N = 500 penalty = 0.5 padtype = 'reflect' t = np.linspace(0, 10, N, endpoint=True) x1 = sig.chirp(t, f0=2, f1=8, t1=20, method='linear') x2 = sig.chirp(t, f0=.4, f1=4, t1=20, method='quadratic') x = x1 + x2 Tx, Wx, ssq_freqs, scales = ssq_cwt(x, ('gmw', {'beta': 12}), t=t, padtype=padtype) # CWT example ridge_idxs = extract_ridges(Wx, scales, penalty, n_ridges=2, bw=25) viz(x, Wx, ridge_idxs) # SSQ_CWT example ridge_idxs = extract_ridges(Tx, scales, penalty, n_ridges=2, bw=2) viz(x, Tx, ridge_idxs, ssq=True) ``` -------------------------------- ### Demonstrating Penalty Term Impact on Ridge Extraction Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/examples/ridge_extraction/README.md Illustrates the effect of the `penalty` parameter in the `extract_ridges` function when applied to a signal composed of two quadratic chirps. The example shows ridge extraction with `penalty=0.0` (no penalty) and `penalty=0.5`, highlighting how the penalty influences the ridge identification and prevents frequency jumps. ```python "Linear + quadratic chirp." N = 600 padtype = 'symmetric' t = np.linspace(0, 3, N, endpoint=True) x1 = sig.chirp(t-1.5, f0=30, t1=1.1, f1=40, method='quadratic') x2 = sig.chirp(t-1.5, f0=10, t1=1.1, f1=5, method='quadratic') x = x1 + x2 Tx, Wx, ssq_freqs, scales = ssq_cwt(x, t=t, padtype=padtype) # CWT example no penalty ridge_idxs = extract_ridges(Wx, scales, penalty=0.0, n_ridges=2, bw=25) viz(x, Wx, ridge_idxs) # CWT example with penalty ridge_idxs = extract_ridges(Wx, scales, penalty=0.5, n_ridges=2, bw=25) viz(x, Wx, ridge_idxs) ``` -------------------------------- ### Configure ssqueezepy Execution Mode (Python) Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/ssqueezepy/README.md Demonstrates how to set environment variables to control the execution mode of ssqueezepy. This includes options for single-threaded CPU, multi-threaded CPU (default), and GPU acceleration. GPU mode requires CuPy and PyTorch. ```python import os # CPU, single thread os.environ['SSQ_PARALLEL'] = '0' # CPU, multi-threaded (default) os.environ['SSQ_PARALLEL'] = '1' # GPU, overrides 'SSQ_PARALLEL' os.environ['SSQ_GPU'] = '1' ``` -------------------------------- ### Perform STFT and Inverse STFT Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Demonstrates how to compute the Short-Time Fourier Transform (STFT) with custom parameters, including windowing and derivative calculation, and how to reconstruct the signal using the inverse STFT. ```python from ssqueezepy import stft, istft, get_window # Basic STFT Sx = stft(x) # STFT with custom parameters Sx = stft(x, window='hann', n_fft=256, win_len=256, hop_len=1, fs=1/t[1], modulated=True) # STFT with derivative Sx, dSx = stft(x, n_fft=256, derivative=True, fs=1/t[1]) # Inverse STFT x_reconstructed = istft(Sx, window='hann', n_fft=256, hop_len=1, N=len(x)) ``` -------------------------------- ### Enable GPU Acceleration Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Configures ssqueezepy to use CuPy or PyTorch for GPU-accelerated CWT and SSQ-CWT calculations. This is useful for processing large signals efficiently. ```python import os from ssqueezepy import cwt os.environ['SSQ_GPU'] = '1' os.environ['SSQ_PARALLEL'] = '0' # CWT on GPU Wx, scales = cwt(x, wavelet='gmw', astensor=True) # Disable GPU os.environ['SSQ_GPU'] = '0' ``` -------------------------------- ### Compute Continuous Wavelet Transform (CWT) Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Demonstrates how to compute the CWT using various wavelet types, scale configurations, and padding options. It also shows how to compute time derivatives necessary for synchrosqueezing. ```python import numpy as np from ssqueezepy import cwt, Wavelet N = 2048 t = np.linspace(0, 10, N, endpoint=False) x = np.cos(2 * np.pi * 2 * (np.exp(t / 2.2) - 1)) Wx, scales = cwt(x) wavelet = Wavelet(('morlet', {'mu': 13.4})) Wx, scales = cwt(x, wavelet=wavelet, scales='log-piecewise', nv=32) Wx, scales, dWx = cwt(x, wavelet='gmw', derivative=True, fs=1/t[1]) Wx, scales = cwt(x, padtype='reflect', rpadded=False) Wx_ho, scales = cwt(x, wavelet='gmw', order=(0, 1, 2), average=True) ``` -------------------------------- ### Generate and Compare Test Signals Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Demonstrates the TestSignals class to generate various synthetic signals like chirps and AM signals. It also shows how to compare different wavelets, CWT vs STFT, and ridge extraction methods. ```python from ssqueezepy import TestSignals, Wavelet tsigs = TestSignals(N=2048, snr=10) x_chirp, t = tsigs.lchirp() # Compare wavelets wavelets = [Wavelet(('morlet', {'mu': 5})), Wavelet(('morlet', {'mu': 20}))] tsigs.wavcomp(wavelets, signals=['lchirp', 'echirp']) # Compare CWT vs STFT tsigs.cwt_vs_stft(wavelet=Wavelet('gmw'), window='hann', signals=['packed-poly'], n_fft=256) ``` -------------------------------- ### Perform CWT and STFT Synchrosqueezing in Python Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/README.md This script demonstrates how to generate a synthetic signal with noise, apply synchrosqueezing transforms (ssq_cwt and ssq_stft), and visualize the results using matplotlib. It also shows how to map scales to physical frequencies for proper axis labeling. ```python import numpy as np import matplotlib.pyplot as plt from ssqueezepy import ssq_cwt, ssq_stft from ssqueezepy.experimental import scale_to_freq def viz(x, Tx, Wx): plt.imshow(np.abs(Wx), aspect='auto', cmap='turbo') plt.show() plt.imshow(np.abs(Tx), aspect='auto', vmin=0, vmax=.2, cmap='turbo') plt.show() N = 2048 t = np.linspace(0, 10, N, endpoint=False) xo = np.cos(2 * np.pi * 2 * (np.exp(t / 2.2) - 1)) xo += xo[::-1] x = xo + np.sqrt(2) * np.random.randn(N) Twxo, Wxo, *_ = ssq_cwt(xo) viz(xo, Twxo, Wxo) Twx, Wx, *_ = ssq_cwt(x) viz(x, Twx, Wx) Tsxo, Sxo, *_ = ssq_stft(xo) viz(xo, np.flipud(Tsxo), np.flipud(Sxo)) Tsx, Sx, *_ = ssq_stft(x) viz(x, np.flipud(Tsx), np.flipud(Sx)) from ssqueezepy import Wavelet, cwt, stft, imshow fs = 400 t = np.linspace(0, N/fs, N) wavelet = Wavelet() Wx, scales = cwt(x, wavelet) Sx = stft(x)[::-1] freqs_cwt = scale_to_freq(scales, wavelet, len(x), fs=fs) freqs_stft = np.linspace(1, 0, len(Sx)) * fs/2 ikw = dict(abs=1, xticks=t, xlabel="Time [sec]", ylabel="Frequency [Hz]") imshow(Wx, **ikw, yticks=freqs_cwt) imshow(Sx, **ikw, yticks=freqs_stft) ``` -------------------------------- ### Select Scales for CWT (Python) Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/ssqueezepy/README.md This code snippet illustrates how to configure the scales for the Continuous Wavelet Transform (CWT) in ssqueezepy. It defines signal parameters, chooses a wavelet, and sets various scale selection parameters like type, preset, number of voices, and downsampling factor. ```python # signal length N = 2048 # your signal here t = np.linspace(0, 1, N, endpoint=False) x = np.cos(2*np.pi * 16 * t) + np.sin(2*np.pi * 64 * t) # choose wavelet wavelet = 'gmw' # choose padding scheme for CWT (doesn't affect scales selection) padtype = 'reflect' # one of: 'log', 'log-piecewise', 'linear' scaletype = 'log-piecewise' # one of: 'minimal', 'maximal', 'naive' (not recommended) preset = 'maximal' # number of voices (wavelets per octave); more = more scales v = 32 # downsampling factor for higher scales (used only if `scaletype='log-piecewise'`) downsample = 4 # show this many of lowest-frequency wavelets show_last = 20 ``` -------------------------------- ### Compute Short-Time Fourier Transform (STFT) Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Basic implementation of the STFT function for time-frequency analysis using various windowing functions. ```python import numpy as np from ssqueezepy import stft, istft, get_window N = 2048 t = np.linspace(0, 10, N, endpoint=False) x = np.cos(2 * np.pi * 10 * t) + 0.5 * np.cos(2 * np.pi * 25 * t) ``` -------------------------------- ### Manage Wavelets with Wavelet Class Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Utilizes the Wavelet class to instantiate built-in or custom wavelets, inspect their properties, evaluate them in time/frequency domains, and visualize their characteristics. ```python from ssqueezepy import Wavelet # Create wavelets wavelet_gmw = Wavelet('gmw') wavelet_morlet = Wavelet(('morlet', {'mu': 13.4})) # Properties and Info print(wavelet_gmw.wc) wavelet_gmw.info() # Visualization wavelet_gmw.viz('time-frequency') # Custom wavelet def custom_wavelet_fn(w): return np.exp(-0.5 * (w - 5)**2) wavelet_custom = Wavelet(custom_wavelet_fn) ``` -------------------------------- ### Visualize Wavelets and Transforms Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Utilizes the visuals module to plot time-frequency representations, wavelet filterbanks, and higher-order CWT results. These tools help in analyzing the resolution and characteristics of transforms. ```python from ssqueezepy import ssq_cwt, Wavelet from ssqueezepy.visuals import imshow, wavelet_tf, wavelet_filterbank Tx, Wx, ssq_freqs, scales = ssq_cwt(x, wavelet='gmw') imshow(Wx, abs=1, title="CWT Magnitude", yticks=scales) wavelet = Wavelet(('morlet', {'mu': 13.4})) wavelet_tf(wavelet, N=2048, scale=10) wavelet_filterbank(wavelet, N=1024, scales='log') ``` -------------------------------- ### BibTeX Citation for ssqueezepy Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/README.md Provides the BibTeX formatted citation for the ssqueezepy project, useful for academic referencing. This includes author, journal, year, and DOI. ```bibtex @article{OverLordGoldDragon2020ssqueezepy, title={ssqueezepy}, author={John Muradeli}, journal={GitHub. Note: https://github.com/OverLordGoldDragon/ssqueezepy/}, year={2020}, doi={10.5281/zenodo.5080508}, } ``` -------------------------------- ### Reconstruct Signal using Inverse CWT (iCWT) Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Shows how to reconstruct an original signal from CWT coefficients using single or double integral methods, including handling DC component mean correction. ```python import numpy as np from ssqueezepy import cwt, icwt N = 2048 t = np.linspace(0, 10, N, endpoint=False) x = np.cos(2 * np.pi * 5 * t) + 0.5 * np.cos(2 * np.pi * 12 * t) Wx, scales = cwt(x, wavelet='gmw') x_reconstructed = icwt(Wx, wavelet='gmw', scales=scales) x_mean = x.mean() x_reconstructed = icwt(Wx, wavelet='gmw', scales=scales, x_mean=x_mean) x_reconstructed_2int = icwt(Wx, wavelet='gmw', scales=scales, one_int=False) ``` -------------------------------- ### Extracting Ridges from CWT and SSQ-CWT of Two Constant Frequency Signals Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/examples/ridge_extraction/README.md Demonstrates the extraction of two prominent frequency ridges from both Continuous Wavelet Transform (CWT) and Synchrosqueezed Continuous Wavelet Transform (SSQ-CWT) of a signal composed of two constant frequency sine and cosine waves. It highlights the use of the `extract_ridges` function with different penalty values and visualization. ```python "Sine + cosine." N, f1, f2 = 500, 0.5, 2.0 padtype = 'wrap' penalty = 20 t = np.linspace(0, 10, N, endpoint=True) x1 = np.sin(2*np.pi * f1 * t) x2 = np.cos(2*np.pi * f2 * t) x = x1 + x2 Tx, Wx, ssq_freqs, scales = ssq_cwt(x, t=t, padtype=padtype) # CWT example ridge_idxs = extract_ridges(Wx, scales, penalty, n_ridges=2, bw=25) viz(x, Wx, ridge_idxs, scales) # SSQ_CWT example (note the jumps) ridge_idxs = extract_ridges(Tx, scales, penalty, n_ridges=2, bw=4) viz(x, Tx, ridge_idxs, ssq_freqs, ssq=True) ``` -------------------------------- ### Compute Synchrosqueezed STFT Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Computes the synchrosqueezed STFT for enhanced time-frequency localization and demonstrates how to extract phase information and perform inverse synchrosqueezing. ```python from ssqueezepy import ssq_stft, issq_stft # Basic SSQ STFT Tx, Sx, ssq_freqs, Sfs = ssq_stft(x) # Custom parameters Tx, Sx, ssq_freqs, Sfs = ssq_stft(x, window='hann', n_fft=256, win_len=128, hop_len=1, fs=1/t[1], modulated=True, squeezing='sum', gamma=1e-6) # Inverse SSQ STFT x_reconstructed = issq_stft(Tx, window='hann', n_fft=256, hop_len=1) ``` -------------------------------- ### Extracting Ridges from CWT and SSQ-CWT of Sweep and Constant Frequency Signals Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/examples/ridge_extraction/README.md Demonstrates ridge extraction for a composite signal consisting of a cubic polynomial frequency sweep and a pure tone. The code applies `extract_ridges` to both CWT and SSQ-CWT representations, varying the `penalty` parameter and visualizing the extracted ridges. ```python "Cubic polynomial frequency variation + pure tone." N, f = 500, 0.5 padtype = 'wrap' t = np.linspace(0, 10, N, endpoint=True) p1 = np.poly1d([0.025, -0.36, 1.25, 2.0]) x1 = sig.sweep_poly(t, p1) x2 = np.sin(2*np.pi * f * t) x = x1 + x2 Tx, Wx, ssq_freqs, scales = ssq_cwt(x, t=t, padtype=padtype) # CWT example penalty = 2.0 ridge_idxs = extract_ridges(Wx, scales, penalty, n_ridges=2, bw=25) viz(x, Wx, ridge_idxs) # SSQ_CWT example ridge_idxs = extract_ridges(Tx, scales, penalty, n_ridges=2, bw=2) viz(x, Tx, ridge_idxs, ssq=True) ``` -------------------------------- ### Convert Between Scales and Frequencies Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Provides utility functions to map wavelet scales to physical frequencies and vice versa, which is essential for interpreting time-frequency plots. ```python from ssqueezepy import Wavelet from ssqueezepy.experimental import scale_to_freq, freq_to_scale wavelet = Wavelet('gmw') fs = 100 scales = np.logspace(0, 3, 100) freqs = scale_to_freq(scales, wavelet, 2048, fs=fs) ``` -------------------------------- ### Extract Time-Frequency Ridges Source: https://context7.com/overlordgolddragon/ssqueezepy/llms.txt Uses dynamic programming to track ridges in CWT or STFT representations, allowing for the extraction of frequency and energy parameters from complex signals. ```python from ssqueezepy import cwt, ssq_cwt from ssqueezepy.ridge_extraction import extract_ridges # Extract ridges from CWT Wx, scales = cwt(x, wavelet='gmw', fs=1/t[1]) ridge_idxs, ridge_f, ridge_e = extract_ridges(Wx, scales, penalty=2.0, n_ridges=2, bw=15, transform='cwt', get_params=True) # Extract from SSQ CWT Tx, Wx, ssq_freqs, scales = ssq_cwt(x, wavelet='gmw', fs=1/t[1]) ssq_ridge_idxs = extract_ridges(Tx, ssq_freqs, penalty=2.0, n_ridges=2, bw=2, transform='cwt') ``` -------------------------------- ### POST /ssq_stft Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/README.md Performs Short-Time Fourier Transform (STFT) followed by synchrosqueezing. ```APIDOC ## POST /ssq_stft ### Description Computes the synchrosqueezed Short-Time Fourier Transform of a given signal. ### Method POST ### Endpoint /ssq_stft ### Parameters #### Request Body - **x** (array) - Required - The input signal array to be transformed. ### Request Example { "x": [0.1, 0.5, 0.2, ...] } ### Response #### Success Response (200) - **Tsx** (array) - The synchrosqueezed STFT result. - **Sx** (array) - The raw STFT result. #### Response Example { "Tsx": [[...]], "Sx": [[...]] } ``` -------------------------------- ### POST /ssq_cwt Source: https://github.com/overlordgolddragon/ssqueezepy/blob/master/README.md Performs Continuous Wavelet Transform (CWT) followed by synchrosqueezing to produce a time-frequency representation. ```APIDOC ## POST /ssq_cwt ### Description Computes the synchrosqueezed Continuous Wavelet Transform of a given signal. ### Method POST ### Endpoint /ssq_cwt ### Parameters #### Request Body - **x** (array) - Required - The input signal array to be transformed. - **wavelet** (object) - Optional - The wavelet configuration to use. ### Request Example { "x": [0.1, 0.5, 0.2, ...], "wavelet": "morlet" } ### Response #### Success Response (200) - **Tx** (array) - The synchrosqueezed CWT result. - **Wx** (array) - The raw CWT result. #### Response Example { "Tx": [[...]], "Wx": [[...]] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.