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

The STFT results of single channel wav don't match with the results of librosa.stft #8

Open
Raise-me-up opened this issue Nov 23, 2022 · 13 comments

Comments

@Raise-me-up
Copy link

As mentioned, I set frame_size = 512, and shift_size = 256, and compare your results with that of the librosa.stft. However, I can't find the good match.

@Raise-me-up
Copy link
Author

Can you provide a better test.c file please?

@kooBH
Copy link
Owner

kooBH commented Nov 23, 2022

Since I usually work on STFT to match with my MATLAB algorithms, this code fits with MATLAB routines.
I don't have better test code for librosa.stft .

But as I remember it matched with librosa.stft as well, with modification on args.
There are a few options for librosa.stft. Might be delay problem from padding options.

@Raise-me-up
Copy link
Author

@kooBH
Could you please provide your m file so I can check my codes?

@Raise-me-up
Copy link
Author

Or can you provide the args modification of librosa.stft so I can check the data please?

@Raise-me-up
Copy link
Author

Hey @kooBH

I find there are lots of padding mode in the librosa.stft, such as constant, edge, linear_ramp, reflect, warp etc. Which one do you use? Does the 'Copy and Shift' part mean padding?

@kooBH
Copy link
Owner

kooBH commented Nov 26, 2022

manually shifted index due to differences in padding.

https://github.com/kooBH/STFT/blob/main/valdation/validation.ipynb

yet, there is 1e-7 error.

@Raise-me-up
Copy link
Author

Hey @kooBH

I have seen your validation codes, where the center option of librosa is false. How can I add the padding codes to match the results of librosa when the center option is true?

@Raise-me-up
Copy link
Author

Raise-me-up commented Nov 28, 2022

Well, when I set shift_size = 256, the error become a little big. Why will you set cpp_stft = cpp_stft[:,7:-1] under the shift_size = 128?

@kooBH
Copy link
Owner

kooBH commented Nov 28, 2022

cpp_stft is for real-time processing
where samples for STFT are like

 0  0  0  0
 0  0  0  s1
 0  0  s1 s2
 0  s1 s2 s3
 s1 s2 s3 s4
 s2 s3 s4 s5
...

buf most of STFT routines are for wav reading processing where samples for STFT are like

s1 s2 s3 s4
s2 s3 s4 s5
...

for frame_size = 512, shift_size = 128, which is 25% overlap

when you set padding options on librosa.stft there will be zero samples in initial sequences.

and cpp_stft = cpp_stft[:,7:-1] is just skipping initial sequences where samples are different.

@Raise-me-up
Copy link
Author

wo~ Thank you for your reply! That's a little sophisticated.

@Raise-me-up
Copy link
Author

Raise-me-up commented Nov 28, 2022

Sadly, I can't make the data match under the setting of

librosa.stft(audio, n_fft=512, hop_length=256, win_length=512, window='hann', center=True, pad_mode="reflect")

@kooBH
Copy link
Owner

kooBH commented Dec 5, 2022

https://github.com/kooBH/STFT/blob/main/valdation/validation.ipynb

  const int ch = 1;
  const int rate = 16000;
  const int frame = 512;
  const int shift = 256;  

  STFT process(ch,frame,shift);
rs_stft = rs.stft(input_wav, n_fft=512, hop_length=256, win_length=512, window='hann', center=True, pad_mode="reflect")

image

@Raise-me-up
Copy link
Author

Raise-me-up commented Dec 5, 2022

Hi @kooBH

Thank you very much for your patient reply!

I find the difference between your validation and mine. You can always find the right slice of cpp_stft, while I can't.

BTW, I find the part below can be removed to get the smaller error rate:

for (i = 0; i < frame_size; i++)
      tmp += hann[i] * hann[i];
    tmp /= shift_size;
    tmp = std::sqrt(tmp);

for (i = 0; i < frame_size; i++)
  hann[i] /= tmp;

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

2 participants