Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use stft like scipy.signal "f, t, tf_data = signal.stft(wavedata, fs=fs, window='hamming', nperseg=N_fft, noverlap=int(N_fft*0.8))"? Now we cannot get f,t value. The tf_data is different from var spectrogram = stft.Spectrogram(discreteSignal, normalize: true). #78

Open
John1911603424 opened this issue May 6, 2023 · 2 comments

Comments

@John1911603424
Copy link

No description provided.

@Scxw010516
Copy link

I have the same need, have you solved it?

@ar1st0crat
Copy link
Owner

ar1st0crat commented Feb 16, 2024

Should be something like this:

sciPy:

fs = 16000
N = 10000
time = np.arange(N)
mod = 2*np.cos(2*np.pi*200/fs*time)

N_fft = 1024
f, t, tf_data = signal.stft(mod, fs=fs, window='hamming', nperseg=N_fft, noverlap=int(N_fft*0.8))

NWaves:

var N = 10000;
var fs = 16000;
var wavedata = Enumerable.Range(0, N).Select(i => 2*Math.Cos(2*Math.PI*200/fs*i)).ToFloats();

var N_fft = 1024;
var overlapSize = (int)(N_fft * 0.8);
var hopSize = N_fft - overlapSize;
var stft = new Stft(N_fft, hopSize, WindowType.Hamming);
var tf_data = stft.Direct(wavedata);

// tf_data is array of complex spectra (i.e. array of complex arrays)
// ...

// note. Numbers may differ slightly from sciPy's version. 
// Also, not normalized by default. If necessary, divide by (N_fft/2 + 1).

// .........

// f and t can be calculated separately:

var freqResolution = (double)fs / N_fft;
var freqBinCount = N_fft / 2 + 1;
var f = Enumerable.Range(0, freqBinCount).Select(f => f * freqResolution).ToArray();

var timeResolution = (double)hopSize / fs;
var t = Enumerable.Range(0, tf_data.Count()).Select(t => t * timeResolution).ToArray();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants