Ramon Balthazar
Ramon Balthazar

Reputation: 4290

Python + Scipy: How to set the time frame for a spectrogram?

I'm trying to calculate the spectrogram for an audio signal using scipy.signal.spectrogram. As a project specification it is needed that the time frames be spaced 20ms from each other, but I can't find a way to set it.

The maximum value I was able to get was a non-exact 5ms after tweaking noverlap a bit.

Does anyone know how to achieve this?

Thanks

Upvotes: 2

Views: 2161

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114946

You said "it is needed that the time frames be spaced 20ms from each other". I'll assume that means the delay between the start of each window (or "segment") is 20 ms.

The three relevant parameters are fs, nperseg and noverlap. nperseg is the number of samples in each "segment". That is, it is the number of samples in the "window" that slides over the input data. noverlap is the number of samples in the overlap of consecutive windows. Therefore, the delay between success windows is nperseg - noverlap samples.

You want the window to move 20 ms, which corresponds to fs*20/1000 samples (assuming fs is measured in samples per second, i.e. Hz). Suppose your window length is T ms. Then nperseg is fs*T/1000. You want the overlap to be T-20 ms, and so noverlap = int(fs*(T-20)/1000).

Upvotes: 2

Related Questions