question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

stft changes time resolution when center=False and n_fft changes

See original GitHub issue

I am having a problem performing two stft for a signal with the same hop and window lengths but with a different number of fft points. The problem occurs with the frames_to_time function too.

To Reproduce

import librosa
import numpy as np

# generates a signal with sr=100 and f0=10
sr=100
ts = np.arange(0, 1, 1/sr)
x = np.sin(2*np.pi*10*ts)

# computes the stft with n_fft=win_length=hop_length
n_fft = win_length = hop = 10
s1 = lb.stft(x, n_fft=n_fft, win_length=win_length, hop_length=hop,  center=False)
f1 = lb.fft_frequencies(sr, n_fft)
t1 = lb.frames_to_time(range(s1.shape[1]), sr=sr, hop_length=hop)
print(s1.shape, f1.shape, t1.shape)
#computes the stft with more points and fft but not changing win_length and hop_length
n_fft = 20
s2 = lb.stft(x, n_fft=n_fft, win_length=win_length, hop_length=hop,  center=False)
f2 = lb.fft_frequencies(sr, n_fft)
t2 = lb.frames_to_time(range(s2.shape[1]), sr=sr, hop_length=hop, n_fft=n_fft)
print(s2.shape, f2.shape, t2.shape)

I expected de output to be:

(6, 10) (6,) (10,)
(11, 10) (11,) (10,)

Since I’m still dividing the 100 samples into 10 windows of length 10, but instead I got:

(6, 10) (6,) (10,)
(11, 9) (11,) (9,)

if I inspect the content of t1, t2, f1 and f2:

f1 = [ 0. 10. 20. 30. 40. 50.]
f2 = [ 0.  5. 10. 15. 20. 25. 30. 35. 40. 45. 50.]
t1 = [0.05 0.15 0.25 0.35 0.45 0.55 0.65 0.75 0.85 0.95]
t2 = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]

I can see that the frequency resolution has increased, but the time resolution has decreased instead of remaining the same.

I’ve tested in different environments but here’s where I’ve tested last

Linux-4.19.112+-x86_64-with-Ubuntu-18.04-bionic
Python 3.6.9 (default, Oct  8 2020, 12:12:24) 
[GCC 8.4.0]
NumPy 1.19.5
SciPy 1.4.1
librosa 0.8.0

I know that are other issues related to this, but they don’t include the frames_to_time part so I posted again.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Feulocommented, Feb 11, 2021

I see, so for the example where the signal y has length 40, n_fft = 20, hop_length = win_length = 10, and center=False the frames will look like this:

frame_2(1)

I have 3 frames starting at samples 0, 10 and 20. The frame that would start at sample 30 is not created because the signal ends at sample 39 and there is no padding at the end of the signal

Because win_length < n_fft the (n_fft - win_length)/2 = 5 samples in the beginning of the signal won’t be used, the same for the last (n_fft - win_length)/2 = 5`.

Finally, the n_fft parameter sets the frame size to be used and it is the number of points used in the DFT too, therefore I can’t have a number of points in the DFT greater than the frame size (using the stft function)

Thank you one more time for the clarifications, I will you the stft code as base for a custom function where I can change the number of points in the DFT without changing the frame size.

0reactions
bmcfeecommented, Feb 26, 2021

Seems like this one is resolved, so I’m closing it out. Feel free to reopen if it warrants further discussion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Short-time Fourier Transform > Frequency Domain Analysis ...
STFT is segmenting the signal into narrow time intervals and takes the Fourier ... When the signal changes fast, you need a small...
Read more >
Number T frames output of STFT in 0.4.1 - PyTorch Forums
When updating torch from 0.4.0 to 0.4.1 it seems like the number of frames computed by STFT has changed for a same given...
Read more >
librosa.stft — librosa 0.10.0.dev0 documentation
The STFT represents a signal in the time-frequency domain by computing discrete Fourier transforms (DFT) over short overlapping windows. This function returns a ......
Read more >
Short-Time Fourier Analysis Why STFT for Speech Signals ...
alternative form of STFT (based on change of variables) is ... between good temporal resolution (short windows) and good frequency resolution (l.
Read more >
Short-time Fourier transform - Wikipedia
The short-time Fourier transform (STFT), is a Fourier-related transform used to determine ... One then usually plots the changing spectra as a function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found