OverLordGoldDragon / ssqueezepy

Synchrosqueezing, wavelet transforms, and time-frequency analysis in Python
MIT License
647 stars 95 forks source link

How to Correctly Obtain the Frequency of a Sinusoidal Signal by ssq_cwt or ssq_stft? #99

Closed yangluowisdom closed 1 year ago

yangluowisdom commented 1 year ago

I tested a sine signal, but how do I get the frequency of that signal? And the difference between the frequency value obtained from the spectrum graph and the true frequency value of the signal is too large. May I ask why this situation occurs?

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()

%%# Define signal

N = 2048 t = np.linspace(0, 10, N, endpoint=False)

xo = np.cos(2 np.pi 2 * (np.exp(t / 2.2) - 1))

xo = np.cos(2 np.pi 2 * t)

xo += xo[::-1] # add self reflected

x = xo + 0np.sqrt(2) np.random.randn(N) # add noise

plt.plot(xo); plt.show() plt.plot(x); plt.show()

%%# CWT + SSQ CWT

Twxo, Wxo, _ = ssq_cwt(xo) viz(xo, Twxo, Wxo) viz(xo, Twxo, Wxo) exit() Twx, Wx, _ = ssq_cwt(x) viz(x, Twx, Wx) viz(x, Twx, Wx)

%%# STFT + SSQ STFT

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))