ar1st0crat / NWaves

.NET DSP library with a lot of audio processing functions
MIT License
453 stars 71 forks source link

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 1 year ago

Scxw010516 commented 7 months ago

I have the same need, have you solved it?

ar1st0crat commented 6 months ago

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