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

How can I get the spectrum out of a 2048 float32Array #27

Open
JSmithOner opened this issue Aug 16, 2024 · 2 comments
Open

How can I get the spectrum out of a 2048 float32Array #27

JSmithOner opened this issue Aug 16, 2024 · 2 comments

Comments

@JSmithOner
Copy link

I've looked at your documentation and couldn't find a real situation example where the array is filled with data.So my question is how can I get the Spectrum values of a float32array filled with audio values.

Thanks in advance

@skoerfgen
Copy link

Recently I asked myself the same thing. Hope this helps:

// example audio values
const myFloat32Array = new Float32Array(4096).map(i=>Math.random()*2-1);

const f = new FFT(4096);
const out = f.createComplexArray();
f.realTransform(out, myFloat32Array);

const getMagnitude=(arr)=>{
	// input "arr" contains [ real0, imaginary0, real1, imaginary1, ... ];
	// pairwise get scalar magnitude (power)
	return arr.reduce((a,c,i)=>{
		if (i%2===0) a.push(Math.hypot(c,arr[i+1]));
		return a;
	},[]);
};

// "out" has length 4096*2, only use left half (containing the Fourier Transform's complex output)
const spectrum=getMagnitude(out.slice(0,out.length/2));
// result has now length 2048 (half the fftSize)

@JSmithOner
Copy link
Author

JSmithOner commented Sep 1, 2024

@skoerfgen

Recently I asked myself the same thing. Hope this helps:

// example audio values
const myFloat32Array = new Float32Array(4096).map(i=>Math.random()*2-1);

const f = new FFT(4096);
const out = f.createComplexArray();
f.realTransform(out, myFloat32Array);

const getMagnitude=(arr)=>{
	// input "arr" contains [ real0, imaginary0, real1, imaginary1, ... ];
	// pairwise get scalar magnitude (power)
	return arr.reduce((a,c,i)=>{
		if (i%2===0) a.push(Math.hypot(c,arr[i+1]));
		return a;
	},[]);
};

// "out" has length 4096*2, only use left half (containing the Fourier Transform's complex output)
const spectrum=getMagnitude(out.slice(0,out.length/2));
// result has now length 2048 (half the fftSize)

thank you meanwhile I figured it out

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