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

MFCC window size #10

Open
mpindado opened this issue May 10, 2021 · 0 comments
Open

MFCC window size #10

mpindado opened this issue May 10, 2021 · 0 comments

Comments

@mpindado
Copy link

Hi, I just came into this repo because I needed to port an MFCC calculation from librosa to java. I found your class very useful, although I had a minimal problem regarding window size.
As my pretrained models did not have default win size (n_fft), I did a minor change to MFCC.java in order to make this work the same as original librosa, producing the same results.
I simply want to share this minor tweak if someone needs this in the future:

	// Marcos not default window size
	private final static int       n_win                = 1600;
...
	private double[] getWindow(){
		//Return a Hann window for even n_fft.
		//The Hann window is a taper formed by using a raised cosine or sine-squared
		//with ends that touch zero.
		double[] win = new double[/*n_fft*/ n_win];
		for (int i = 0; i < /*n_fft*/n_win; i++){
			win[i] = 0.5 - 0.5 * Math.cos(2.0*Math.PI*i/(/*n_fft*/n_win));
		}

		// Marcos: Pad center win to n_ftt (see librosa spectrum.py)
		if (n_win < n_fft) {
			double[] padded_win = new double[n_fft];
			int lpad = (n_fft - n_win) / 2;
			int rpad = n_fft - n_win - lpad;
			for (int l=0;l<lpad;l++)
				padded_win[l] = 0.0;
			for (int m=0;m<n_win;m++)
				padded_win[lpad+m] = win[m];
			for (int r=0;r<rpad;r++)
				padded_win[lpad+n_win+r] = 0.0;
			return padded_win;
		}
		else return win;
	}
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

1 participant