view fft/fftw/FFT.js @ 19:26056e866c29

Add FFTW to comparison table
author Chris Cannam
date Tue, 06 Oct 2015 13:08:39 +0100
parents
children a901eeeabf3b
line wrap: on
line source
"use strict";

var fftwModule = FFTWModule({});

var fftwf_plan_dft_r2c_1d = fftwModule.cwrap(
    'fftwf_plan_dft_r2c_1d', 'number', ['number', 'number', 'number', 'number']
);

var fftwf_plan_dft_c2r_1d = fftwModule.cwrap(
    'fftwf_plan_dft_c2r_1d', 'number', ['number', 'number', 'number', 'number']
);

var fftwf_execute = fftwModule.cwrap(
    'fftwf_execute', 'void', ['number']
);

var fftwf_destroy_plan = fftwModule.cwrap(
    'fftwf_destroy_plan', 'void', ['number']
);

function FFTW(size) {

    this.size = size;
    this.rptr = fftwModule._malloc(size*4 + (size+2)*4);
    this.cptr = this.rptr + size*4;
    this.r = new Float32Array(fftwModule.HEAPU8.buffer, this.rptr, size);
    this.c = new Float32Array(fftwModule.HEAPU8.buffer, this.cptr, size+2);

    var FFTW_ESTIMATE = (1 << 6);
    this.fplan = fftwf_plan_dft_r2c_1d(size, this.rptr, this.cptr, FFTW_ESTIMATE);
    this.iplan = fftwf_plan_dft_c2r_1d(size, this.cptr, this.rptr, FFTW_ESTIMATE);

    this.forward = function(real) {
	this.r.set(real);
	fftwf_execute(this.fplan);
	return (new Float32Array
		(fftwModule.HEAPU8.buffer, this.cptr, this.size+2))
	    .slice(0);
    }
    
    this.inverse = function(cpx) {
	this.c.set(cpx);
	fftwf_execute(this.iplan);
	return (new Float32Array
		(fftwModule.HEAPU8.buffer, this.rptr, this.size))
	    .slice(0);
    }
    
    this.dispose = function() {
	fftwf_destroy_plan(this.fplan);
	fftwf_destroy_plan(this.iplan);
	fftwModule._free(this.rptr);
    }
}