1
$\begingroup$

I am currently trying to understand librosa.feature.melspectrogram in a mathematical sense.

In my understanding, spectrogram is based on the STFT which is for a given discrete time sequence $x[n]$ of length $L$, the value corresponding to a frame of $x[n]$ at time index $m$ and frequency index $k$ is

$$ X_m[k] = \sum_{n=0}^{N-1} x[n+mH]w[n] \ e^{-j2\pi kn/N} $$ where $$ x_m[n] = x[n+mH]w[n] $$

and $\frac{N}{2}$ corresponds to n_fft and $H$ corresponds to hop_length.

Also, the Hann window $w[n]$ of length $L$ is

$$ w[n]=\sin^2 \left( \frac{2\pi n}{L-1} \right) $$

for $0 \leq n \leq L-1$

However, n_fft and win_length do not have to be equal but just needs to satisfy win_length <= n_fft and then the window will be zero-padded to match the n_ftt.

Does this means that the actual Hann window will become

$$ w[n]=\begin{cases} \sin^2 \left(\frac{2\pi n}{L-1} \right), \qquad & 0 \leq n \leq L-1\\ \\ 0, \qquad & L \leq n \leq N-1\\ \end{cases} $$

or something like this with being centered to $\frac{N}{2}$?

Sorry if this is the duplicated question but I could not find a satisfying answer.

$\endgroup$
2
  • $\begingroup$ Related. Also see comment under accepted answer. $\endgroup$ Mar 21, 2023 at 11:13
  • $\begingroup$ Do you know what the relationship is between $H$ and $L$? $\endgroup$ Mar 21, 2023 at 19:42

1 Answer 1

0
$\begingroup$

This is known as zero-padding which is used to effectively increase the resolution of the spectrum by interpolating between frequency bins. Keep in mind that this doesn't add any new information that wasn't already contained in the original samples!

The zeros are split and appended to the beginning and end of the window for each time-bin, $m$, of the STFT. See equation 8.5 in this article as a reference. Note that this interpolation is not linear, but uses a sinc kernel. This interpolation can aid in better estimating the sinusoidal parameters of periodic signals in $x[n]$ that have periods of non-integer multiples of $N$ (prior to zero padding). See this MATLAB example for an idea of how it could be applied for roughly estimating amplitude or the frequency at which a between-bin peak would occur.

$\endgroup$
3
  • $\begingroup$ @Ash Thank you for the answer. To make it clear, so can I assume that the window function that I have shown (or the centered version) is the actual window function? Also, I think the hyperlink to the article is not correct. It redirects to this page. $\endgroup$
    – esh3390
    Mar 21, 2023 at 3:51
  • $\begingroup$ Whoops! Corrected now. OP, check out the fixed link and let me know if you have further questions. The reference combines the samples and the window to be $x_m^w[n]=x[n]w[n]$ and specifies conditional values appropriately. You can think of it as making the window larger with zeros on either end, or you could think of it as adding zeros to the already windowed samples. Both end up with the same result. $\endgroup$
    – Ash
    Mar 21, 2023 at 6:54
  • $\begingroup$ "Doesn't add any information" this is incorrect. n_fft is hop_len along frequency, and a low value will alias the spectrogram. This is confusing complex-valued STFT or DFT zero-padding. What's correct is, the Heisenberg resolution doesn't change, but that statement alone is misleading (as shown in first link). Also related. $\endgroup$ Mar 21, 2023 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.