指向WebAssembly的数组的JavaScript数组

指向WebAssembly的数组的JavaScript数组,javascript,c,pointers,webassembly,emscripten,Javascript,C,Pointers,Webassembly,Emscripten,我目前正在尝试实现JavaScript数组的2D FFT。虽然在JS中是可行的,但矩阵通常足够大(例如3000x4000),这使得它有点慢。我考虑用C语言实现2D FFT,并使用WebAssembly来加速 2D FFT实现源于。我做了一些修改,在不同的指针中有实值和虚值。我将代码粘贴到这里: /*------------------------------------------------------------------------- Perform a 2D FFT inplace g

我目前正在尝试实现JavaScript数组的2D FFT。虽然在JS中是可行的,但矩阵通常足够大(例如3000x4000),这使得它有点慢。我考虑用C语言实现2D FFT,并使用WebAssembly来加速

2D FFT实现源于。我做了一些修改,在不同的指针中有实值和虚值。我将代码粘贴到这里:

/*-------------------------------------------------------------------------
Perform a 2D FFT inplace given a complex 2D array
The direction dir, 1 for forward, -1 for reverse
The size of the array (nx,ny)
*/
#include <stdlib.h>
#include <math.h>


/* This computes an in-place complex-to-complex FFT */
int FFT(int dir,int m, double *x,double *y)
{
  long nn,i,i1,j,k,i2,l,l1,l2;
  double c1,c2,tx,ty,t1,t2,u1,u2,z;
  
  /* Calculate the number of points */
  nn = 1;
  for (i=0;i<m;i++)
  nn *= 2;
  
  /* Do the bit reversal */
  i2 = nn >> 1;
  j = 0;
  for (i=0;i<nn-1;i++) {
    if (i < j) {
      tx = x[i];
      ty = y[i];
      x[i] = x[j];
      y[i] = y[j];
      x[j] = tx;
      y[j] = ty;
    }
    k = i2;
    while (k <= j) {
      j -= k;
      k >>= 1;
    }
    j += k;
  }
  
  /* Compute the FFT */
  c1 = -1.0;
  c2 = 0.0;
  l2 = 1;
  for (l=0;l<m;l++) {
    l1 = l2;
    l2 <<= 1;
    u1 = 1.0;
    u2 = 0.0;
    for (j=0;j<l1;j++) {
      for (i=j;i<nn;i+=l2) {
        i1 = i + l1;
        t1 = u1 * x[i1] - u2 * y[i1];
        t2 = u1 * y[i1] + u2 * x[i1];
        x[i1] = x[i] - t1;
        y[i1] = y[i] - t2;
        x[i] += t1;
        y[i] += t2;
      }
      z =  u1 * c1 - u2 * c2;
      u2 = u1 * c2 + u2 * c1;
      u1 = z;
    }
    c2 = sqrt((1.0 - c1) / 2.0);
    if (dir == 1)
    c2 = -c2;
    c1 = sqrt((1.0 + c1) / 2.0);
  }
  
  /* Scaling for forward transform */
  if (dir == 1) {
    for (i=0;i<nn;i++) {
      x[i] /= (double)nn;
      y[i] /= (double)nn;
    }
  }
  
  return 0;
}

int FFT2D(double **c_real, double **c_imag, int nx, int ny, int dir)
{
  int i,j,m;
  double *real,*imag;
  
  /* Transform the rows */
  real = (double *)malloc(nx * sizeof(double));
  imag = (double *)malloc(nx * sizeof(double));
  m = log(nx)/log(2);
  for (j=0;j<ny;j++) {
    for (i=0;i<nx;i++) {
      real[i] = c_real[j][i];
      imag[i] = c_imag[j][i];
    }
    FFT(dir,m,real,imag);
    for (i=0;i<nx;i++) {
      c_real[j][i] = real[i];
      c_imag[j][i] = imag[i];
    }
  }
  free(real);
  free(imag);
  
  /* Transform the columns */
  real = (double *)malloc(ny * sizeof(double));
  imag = (double *)malloc(ny * sizeof(double));
  m = log(ny)/log(2);
  for (i=0;i<nx;i++) {
    for (j=0;j<ny;j++) {
      real[j] = c_real[j][i];
      imag[j] = c_imag[j][i];
    }
    FFT(dir,m,real,imag);
    for (j=0;j<ny;j++) {
      c_real[j][i] = real[j];
      c_imag[j][i] = imag[j];
    }
  }
  free(real);
  free(imag);
  return 0;
}

int HL_2DFFT(double **real, double **imag, double **real_ptr, double **imag_ptr, int nx, int ny, int nx_pw2, int ny_pw2){
  int i, j, diff_nx, diff_ny;
  
  diff_nx = nx_pw2 - nx;
  diff_ny = ny_pw2 - ny;
  
  // copy matrix and expand to power of 2.
  for (i = diff_ny/2; i < ny; ++i) {
    for (j = 0; j < nx_pw2; ++j) {
      if(j<diff_nx/2){
        real_ptr[i][j] = real[i-(diff_ny/2)][0];
        imag_ptr[i][j] = imag[i-(diff_ny/2)][0];
      }
      else if(j>(diff_nx/2 - 1) && j<nx){
        real_ptr[i][j] = real[i-(diff_ny/2)][j-(diff_nx/2)];
        imag_ptr[i][j] = imag[i-(diff_ny/2)][j-(diff_ny/2)];
      } else{
        real_ptr[i][j] = real[i-(diff_ny/2)][nx-1];
        imag_ptr[i][j] = imag[i-(diff_ny/2)][nx-1];
      }
    }
  };
  // Top padding
  for(i=0; i<diff_ny/2;++i){
    for (j = 0; j < nx_pw2; ++j) {
      real_ptr[i][j] = real_ptr[diff_ny/2][j];
      imag_ptr[i][j] = imag_ptr[diff_ny/2][j];
    };
  };
  // Bottom padding
  for(i=ny; i<ny_pw2;++i){
    for (j = 0; j < nx_pw2; ++j) {
      real_ptr[i][j] = real_ptr[ny-1][j];
      imag_ptr[i][j] = imag_ptr[ny-1][j];
    };
  };
  FFT2D(real_ptr, imag_ptr, nx_pw2, ny_pw2, 1);
  
  return 0;
}
JS中的数组数组作为变量数据。hl_2dfft函数返回一个错误,表示: 未捕获运行时错误:内存访问超出界限


任何帮助都将不胜感激

对于任何感兴趣的人来说,我找不到任何和如何将指针指针传递给WASM有关的东西,而且到目前为止,我可以尝试的任何东西都不起作用。现在,我已经实现了0给出的解决方案,即展平JS数组(使用下面的展平函数)并向WASM传递一个指针

const flatten = function(arr, result = []) {
   for (let i = 0, length = arr.length; i < length; i++) {
      const value = arr[i];
      if (Array.isArray(value)) {
         flatten(value, result)
      } else {
         result.push(value)
      }
   };
return result;
};
const flatte=函数(arr,result=[]){
for(设i=0,length=arr.length;i

如果将大型数组作为数组数组存储在JS中,那么这会带来一些不便,需要花费一些时间来线性化它们,但是在JS端创建指针指针似乎并不简单。如果有人知道解决方法,那就太好了。

对于任何感兴趣的人来说,我找不到任何与如何将指针指针传递给WASM相关的内容,我可以尝试的任何内容到目前为止都不起作用。现在,我已经实现了0给出的解决方案,即展平JS数组(使用下面的展平函数)并向WASM传递一个指针

const flatten = function(arr, result = []) {
   for (let i = 0, length = arr.length; i < length; i++) {
      const value = arr[i];
      if (Array.isArray(value)) {
         flatten(value, result)
      } else {
         result.push(value)
      }
   };
return result;
};
const flatte=函数(arr,result=[]){
for(设i=0,length=arr.length;i

如果将大型数组作为数组数组存储在JS中,那么这会带来一些不便,需要花费一些时间来线性化它们,但是在JS端创建指针指针似乎并不简单。如果有人知道一个变通方法,那就太好了。

作为变通方法,你可以让你的2D函数采用大小为
nx*ny
的1D“平面数组”,而只需用
c_real[j][i]
替换为
c_real[j*nx+i]
作为变通方法,你可以让你的2D函数采用1D“平面数组”大小为
nx*ny
,只需将
c_real[j][i]
替换为
c_real[j*nx+i]