Example #1
0
/************************************************************
	IDCT()

	参数:

		F为频域值
		f为时域值
		power为2的幂数

	返回值:

		无

	说明:

		本函数利用快速傅立叶反变换实现快速离散反余弦变换
*************************************************************/
void IDCT(double *F, double *f, int power)
{
	int i,count;
	COMPLEX *X;
	double s;

	/*计算离散反余弦变换点数*/
	count=1<<power;
	
	/*分配运算所需存储器*/
	X=(COMPLEX *)malloc(sizeof(COMPLEX)*count*2);
	
	/*延拓*/
	memset(X,0,sizeof(COMPLEX)*count*2);
	
	/*调整频域点,写入存储器*/
	for(i=0;i<count;i++)
	{
		X[i].re=F[i]*cos(i*PI/(count*2));
		X[i].im=F[i]*sin(i*PI/(count*2));
	}
	
	/*调用快速傅立叶反变换*/
	IFFT(X,X,power+1);
	
	/*调整系数*/
	s=1/sqrt(count);
	for(i=1;i<count;i++)
	{
		f[i]=(1-sqrt(2))*s*F[0]+sqrt(2)*s*X[i].re*count*2;
	}
	
	/*释放存储器*/
	free(X);
}
Example #2
0
void doit(int iter, struct problem *p)
{
     int i;
     if (p->kind == PROBLEM_COMPLEX) {
	  bench_complex *in = (bench_complex *) p->in;
	  int two_n = 2 * p->n[0];

	  if (p->sign > 0)
	       for (i = 0; i < iter; ++i)
		    FFT(in, &two_n);
	  else
	       for (i = 0; i < iter; ++i)
		    IFFT(in, &two_n);
     }
     else /* PROBLEM_REAL */ {
	  bench_real *in = (bench_real *) p->in;
	  int n = p->n[0];

	  if (p->sign < 0)
	       for (i = 0; i < iter; ++i)
		    REAL_FFT(in, &n);
	  else
	       for (i = 0; i < iter; ++i)
		    REAL_IFFT(in, &n);
     }
}
static void fastConvolution(float* fastConvResult,float* ir,float* x_bucket,int ir_startindex,int ir_Size,int x_bucket_startindex,int x_Size,int ish0convolution)
{
	int x_fft_input_Size;
	int ir_fft_input_Size;

	//if convolving with h0, do this
	if(ish0convolution == TRUE)
	{
		//set sizes of the arrays that will be passed to FFT function. Both need to be set to 4*N
		x_fft_input_Size = 4*N;
		ir_fft_input_Size = 4*N;
	}
	else
	{
		//set sizes of the arrays that will be passed to FFT function. Both need to be set to 4*N
		x_fft_input_Size = 2*x_Size;
		ir_fft_input_Size = 2*ir_Size;	
	}
		
	//FFT input arrays declared, set to size as stipulated above
	float x_fft_input[x_fft_input_Size];
	float ir_fft_input[ir_fft_input_Size];
		
	//copy over input bucket data to FFT input array for the input signal
	int i;
	for(i=0;i<x_Size;i++)
	{
		x_fft_input[i] = x_bucket[x_bucket_startindex+i];
	}
		
	//copy over input bucket data to FFT input array for the input signal
	for(i=0;i<ir_Size;i++)
	{
		ir_fft_input[i] = ir[ir_startindex+i];
	}
		
	//set sizes of the arrays that will contain the result of the FFTs. These will be double of the input and ir arrays
	int X_Size = 2*x_fft_input_Size;
	int IR_Size = 2*ir_fft_input_Size;
		
	//FFT output arrays declared, set to double the size of the FFT input arrays
	float X[X_Size];
	float IR[IR_Size];
		
	//Call FFT function, write to X_fft_output and IR_fft_output
	FFT(x_fft_input,X,x_fft_input_Size);
	FFT(ir_fft_input,IR,ir_fft_input_Size);
		
	//Declare function that will contain the complex multiplication results
	float complexFastMultResult[X_Size];
		
	//Call complexFastMult function
	complexFastMult(X,IR,complexFastMultResult,X_Size);
		
	//Call IFFT to convert data in complexFastMultResult to the time domain and write it to fastConvResult
	IFFT(complexFastMultResult,fastConvResult,x_fft_input_Size);		
}
Example #4
0
int main(void) {
Complex c (1 ,0);
Complex c_arr [4]={c,c,c,c};
FFT (c_arr ,4 ,0); //[4 ,0 ,0 ,0]

IFFT (c_arr ,4 ,0);


    system("Pause");
    return 1;
}
Example #5
0
int CepstrumFromMagnitude(float* DestRe, float* DestIm, float* Magnitude, int Power)
{
	int i;
	int Amount = pow(2, Power);
	if(Amount > 65536)
		return 0;

    TempSpace[0] = Boost_Ln(Magnitude[0]);
    for(i = 1; i < Amount / 2; i ++)
	{
        TempSpace[i] = Boost_Ln(Magnitude[i]);
		TempSpace[Amount - i] = TempSpace[i];
	}
	
	Boost_FloatSet(TempSpace + 65536, 0, Amount);
	IFFT(DestRe, DestIm, TempSpace, TempSpace + 65536, Power);
	return 1;
}
Example #6
0
void FastCorrelation(float* Dest, float* Wave1, float* Wave2, int Power)
{
	int Length = pow(2, Power);
	float* Re1 = malloc(Length * sizeof(float));
	float* Im1 = malloc(Length * sizeof(float));
	float* Re2 = malloc(Length * sizeof(float));
	float* Im2 = malloc(Length * sizeof(float));
	Boost_FloatSet(Re1, 0, Length);
	Boost_FloatSet(Re2, 0, Length);
	Boost_FloatSet(Im1, 0, Length);
	Boost_FloatSet(Im2, 0, Length);

	FFT(Wave1, Re1, Re1, Im1, Power);
	FFT(Wave2, Re2, Re2, Im2, Power);
	Boost_FloatComplexMulArr(Re1, Im1, Re1, Im1, Re2, Im2, Length);
	IFFT(Re1, Im1, Dest, Im2, Power);
	
	Boost_FloatDiv(Dest, Dest, Length ,Length);
	
	free(Re1);
	free(Im1);
	free(Re2);
	free(Im2);
}
int main(void)
{
  MONO_PCM pcm00, pcm0, pcm1;
  int n, k, N, offset, frame, number_of_frame;
  double threshold, *w, *x_real, *x_imag, *A, *T, *y_real, *y_imag;
 
  double *x2_real,*x2_imag,*A2,*T2,*y2_real,*y2_imag;
  double *y3_real,*y3_imag;
  
  FILE *fp;
  char tring = k;
  char *fname = "result.dat";
  fp = fopen( fname, "w" );

  mono_wave_read(&pcm00,"grouwl.wav"); 
  mono_wave_read(&pcm0,"sample.wav");
  //mono_wave_read(&pcm0,"hajimemasite.wav");


  N = 4096;
  N = 16384;
  N = 32768;
  N = 44000;
  w = calloc(N, sizeof(double)); /* メモリの確保 */
  x_real = calloc(N, sizeof(double)); /* メモリの確保 */
  x_imag = calloc(N, sizeof(double)); /* メモリの確保 */
  A = calloc(N, sizeof(double)); /* メモリの確保 */
  T = calloc(N, sizeof(double)); /* メモリの確保 */
  y_real = calloc(N, sizeof(double)); /* メモリの確保 */
  y_imag = calloc(N, sizeof(double)); /* メモリの確保 */
  
  x2_real = calloc(N, sizeof(double)); /* メモリの確保 */
  x2_imag = calloc(N, sizeof(double)); /* メモリの確保 */
  A2 = calloc(N, sizeof(double)); /* メモリの確保 */
  T2 = calloc(N, sizeof(double)); /* メモリの確保 */
  y2_real = calloc(N, sizeof(double)); /* メモリの確保 */
  y2_imag = calloc(N, sizeof(double)); /* メモリの確保 */
 
  y3_real = calloc(N, sizeof(double)); /* メモリの確保 */
  y3_imag = calloc(N, sizeof(double)); /* メモリの確保 */

  Hanning_window(w,N);
  number_of_frame = (pcm0.length - N / 2) / (N / 2);
  
  pcm1.fs = pcm0.fs;
  pcm1.bits = pcm0.bits; /* 量子化精度 */
  pcm1.length = pcm0.length; /* 音データの長さ */
  pcm1.s = calloc(pcm1.length, sizeof(double)); /* メモリの確保 */

    for (n = 0; n < N; n++)
    {
      //x_real[n] = pcm00.s[n+33000];
      x_real[n] = pcm00.s[n];
      x_imag[n] = 0.0;
    }
    FFT(x_real,x_imag,N);
    
    for (k = 0; k < N; k++)
      {
        A[k] = sqrt(x_real[k] * x_real[k] + x_imag[k] * x_imag[k]);
        if (x_imag[k] != 0.0 && x_real[k] != 0.0)
        {
          T[k] = atan2(x_imag[k], x_real[k]);
        }
        
        printf("%d %f %f\n",k,A[k],T[k]);
        
      }
      /* スペクトルサブトラクション */
      for (k = 0; k < N; k++)
      {
        A[k] -= threshold;
        if (A[k] < 0.0)
        {
          A[k] = 0.0;
       }
      }
      
      for (k = 0; k < N; k++)
      {
        y_real[k] = A[k] * cos(T[k]);
       y_imag[k] = A[k] * sin(T[k]);
      }
      IFFT(y_real,y_imag,N);
      
      for(k=0;k<N;k++) {
        pcm1.s[k] = y_real[k];
      }

  mono_wave_write(&pcm1, "ffttry.wav"); /* WAVEファイルにモノラルの音データを出力する */
  
  free(pcm0.s); /* メモリの解放 */
  free(pcm00.s);
  free(pcm1.s); /* メモリの解放 */
  free(w); /* メモリの解放 */
  free(x_real); /* メモリの解放 */
  free(x_imag); /* メモリの解放 */
  free(A); /* メモリの解放 */
  free(T); /* メモリの解放 */
  free(y_real); /* メモリの解放 */
  free(y_imag); /* メモリの解放 */

  fclose(fp);
  free(x2_real);
  free(x2_imag);
  free(A2);
  free(T2);
  free(y2_real);
  free(y2_imag);

  free(y3_real);
  free(y3_imag);
  return 0;
}
static void fastConvolution(float* fastConvResult,float* ir,float* x_bucket,int ir_startindex,int ir_Size,int x_bucket_startindex,int x_Size,int ish0convolution)
{
	int x_fft_input_Size;
	int ir_fft_input_Size;

	//if convolving with h0, do this
	if(ish0convolution == TRUE)
	{
		//set sizes of the arrays that will be passed to FFT function. Both need to be set to 4*N
		x_fft_input_Size = 2*N;
		ir_fft_input_Size = 2*N;
	}
	else
	{
		//set sizes of the arrays that will be passed to FFT function. Both need to be set to 4*N
		x_fft_input_Size = x_Size;
		ir_fft_input_Size = ir_Size;	
	}
		
	//FFT input arrays declared, set to size as stipulated above
	//float x_fft_input[x_fft_input_Size];
	//float ir_fft_input[ir_fft_input_Size];
		
	float* x_fft_input = (float*) malloc(sizeof(float)*x_fft_input_Size);
	float* ir_fft_input = (float*) malloc(sizeof(float)*ir_fft_input_Size);
	
	//copy over input bucket data to FFT input array for the input signal
	int i;
	for(i=0;i<x_Size;i++)
	{
		x_fft_input[i] = x_bucket[x_bucket_startindex+i];
	}
		
	//copy over IR data to FFT input array for the IR
	for(i=0;i<ir_Size;i++)
	{
		ir_fft_input[i] = ir[ir_startindex+i];
	}
		
	//set sizes of the arrays that will contain the result of the FFTs. These will be double of the input and ir arrays
	int X_Size = 4*x_fft_input_Size;
	int IR_Size = 4*ir_fft_input_Size;
		
	//FFT output arrays declared, set to double the size of the FFT input arrays
	//float X[X_Size];
	//float IR[IR_Size];
		
	float* X = (float*) malloc(sizeof(float)*X_Size);
	float* IR = (float*) malloc(sizeof(float)*IR_SIZE);
	
	//Call FFT function, write to X_fft_output and IR_fft_output
	FFT(x_fft_input,X,x_fft_input_Size);
	FFT(ir_fft_input,IR,ir_fft_input_Size);
		
	//Declare function that will contain the complex multiplication results
	//float complexFastMultResult[X_Size];
	
	float* complexFastMultResult = (float*) malloc(sizeof(float)*X_Size);
	
	//Performs multiplication of two complex arrays
    for(i=0; i<2*x_fft_input_Size; i++)
    {
    	//(a + ib)(c + id) = ac - bd + i (ad + bc)

    	//Real part
    	complexFastMultResult[2*i] = X[2*i]*IR[2*i] - X[2*i+1]*IR[2*i+1];

    	//Imaginary Part
    	complexFastMultResult[2*i+1] = X[2*i]*IR[2*i+1] + X[2*i+1]*IR[2*i];

    }	

	
	//Call IFFT to convert data in complexFastMultResult to the time domain and write it to fastConvResult
	IFFT(complexFastMultResult,fastConvResult,x_fft_input_Size*2);		
	
	free(x_fft_input);
	free(ir_fft_input);
	free(X);
	free(IR);
	free(complexFastMultResult);
	
}
int main(void)
{
  MONO_PCM pcm0, pcm1;
  int n, k, N, offset, frame, number_of_frame;
  double threshold, *w, *x_real, *x_imag, *A, *T, *y_real, *y_imag;
  
  mono_wave_read(&pcm0, "sample10.wav"); /* WAVEファイルからモノラルの音データを入力する */
  
  pcm1.fs = pcm0.fs; /* 標本化周波数 */
  pcm1.bits = pcm0.bits; /* 量子化精度 */
  pcm1.length = pcm0.length; /* 音データの長さ */
  pcm1.s = calloc(pcm1.length, sizeof(double)); /* メモリの確保 */
  
  threshold = 0.5; /* しきい値 */
  
  N = 1024; /* DFTのサイズ */
  
  number_of_frame = (pcm0.length - N / 2) / (N / 2); /* フレームの数 */
  
  w = calloc(N, sizeof(double)); /* メモリの確保 */
  x_real = calloc(N, sizeof(double)); /* メモリの確保 */
  x_imag = calloc(N, sizeof(double)); /* メモリの確保 */
  A = calloc(N, sizeof(double)); /* メモリの確保 */
  T = calloc(N, sizeof(double)); /* メモリの確保 */
  y_real = calloc(N, sizeof(double)); /* メモリの確保 */
  y_imag = calloc(N, sizeof(double)); /* メモリの確保 */
  
  Hanning_window(w, N); /* ハニング窓 */
  
  for (frame = 0; frame < number_of_frame; frame++)
  {
    offset = N / 2 * frame;
    
    /* x(n)のFFT */
    for (n = 0; n < N; n++)
    {
      x_real[n] = pcm0.s[offset + n] * w[n];
      x_imag[n] = 0.0;
    }
    FFT(x_real, x_imag, N);
    
    /* 振幅スペクトルと位相スペクトル */
    for (k = 0; k < N; k++)
    {
      A[k] = sqrt(x_real[k] * x_real[k] + x_imag[k] * x_imag[k]);
      if (x_imag[k] != 0.0 && x_real[k] != 0.0)
      {
        T[k] = atan2(x_imag[k], x_real[k]);
      }
    }
    
    /* スペクトルサブトラクション */
    for (k = 0; k < N; k++)
    {
      A[k] -= threshold;
      if (A[k] < 0.0)
      {
        A[k] = 0.0;
     }
    }
    
    for (k = 0; k < N; k++)
    {
      y_real[k] = A[k] * cos(T[k]);
      y_imag[k] = A[k] * sin(T[k]);
    }
    IFFT(y_real, y_imag, N);
    
    /* 加工結果の連結 */
    for (n = 0; n < N; n++)
    {
      pcm1.s[offset + n] += y_real[n];
    }
  }
  
  mono_wave_write(&pcm1, "ex10_2.wav"); /* WAVEファイルにモノラルの音データを出力する */
  
  free(pcm0.s); /* メモリの解放 */
  free(pcm1.s); /* メモリの解放 */
  free(w); /* メモリの解放 */
  free(x_real); /* メモリの解放 */
  free(x_imag); /* メモリの解放 */
  free(A); /* メモリの解放 */
  free(T); /* メモリの解放 */
  free(y_real); /* メモリの解放 */
  free(y_imag); /* メモリの解放 */
  
  return 0;
}
Example #10
0
void apply_hdaf_reg(
	int m,
	double sigma,
	int diff_order,
	double h,
	double eps_min,
	double eps_max,
    double * in,
	double * & out,
    int length,
    int in_stride,
    int out_stride)
{
	//if (out == 0) out = ml_alloc<double> (length*out_stride);
	
	static vector<int > m_list;
	static vector<double > sigma_list;
	static vector<int > diff_order_list;
	static vector<int > length_list;
	static vector<double > h_list;
	
	static vector<complex<double> * > kernel_fft_list;
	
	// not dealing with eps_range for now, fix later !!
	
	static int I = 0;
	bool found = 0;
	
	// look for existing parameter combination
	if (I)
	if (m_list[I] == m && sigma_list[I] == sigma && diff_order_list[I] == diff_order && length_list[I] == length && h_list[I] == h )
	    found = true;
	
	if (!found)
	for ( I = 0; I<m_list.size(); I++)
		if ( m_list[I] == m && sigma_list[I] == sigma && diff_order_list[I] == diff_order && length_list[I] == length && h_list[I] == h  )
		{
			found = true;
			break;
		}
	
	if (!found)
	{
		// new parameter combination
		
		I = m_list.size();
		
		m_list.push_back( m );
		sigma_list.push_back( sigma );
		diff_order_list.push_back( diff_order );
		length_list.push_back( length );
		h_list.push_back( h );
		
		complex<double > *kernel_fft = 0;		
		double * kernel = ml_alloc<double > (length);
		for (int k=0; k<length; k++)
			kernel[k] = 0.0;
		
		double x_max = hdaf_truncate_point (eps_min, eps_max, m, sigma, diff_order );
		int k_max = (int)ceil(x_max/h);
		if ( k_max >= length )
		{
			std::cout << "error: bad combination of hdaf parameters; truncate point exceeds data length in apply_hdaf_reg:\n";
            std::cout << "(eps1,eps2) = (" << eps_min << ", " << eps_max << "),\tm= " << m << ",\tsigma:" << sigma << ",\tdiff_order: " << diff_order << ",\tdata_length: " << length << ",\tx_max: " << x_max << ",\tk_max: " << k_max << std::endl;
			std_exit();
		}
		
		mp::mp_init(30);
		ml_poly<mp_real > P;
		make_hdaf_ml_poly( P, m );
		differentiate_hdaf_poly( P, diff_order );
        static mp_real sqrt2 = pow((mp_real)2.0,0.5);
     
        mp_real p = (pow(sqrt2*sigma,-diff_order)/(sqrt2*sigma))*h;
        
		for (int k=0; k<=k_max; k++)
        {
            mp_real r = (((mp_real)k)*h)/(sqrt2*sigma);
            kernel[k] = dble(exp(-r*r)*P(r)*p);
        }
        
		for (int k=1; k<=k_max; k++)
        {
            mp_real r = -(((mp_real)k)*h)/(sqrt2*sigma);        
            kernel[length-k] = dble(exp(-r*r)*P(r)*p);
        }
            
		FFT(kernel,kernel_fft, length, 1,1 );	
		kernel_fft_list.push_back(kernel_fft);
        
		ml_free( kernel );
	}
	
	// run
	complex<double> * q=0;
	complex<double> * ker_fft = kernel_fft_list[I];
	
	FFT(in, q, length, in_stride,1);
    
	for (int k=0; k<div_up(length,2); k++)
		q[k] *= ker_fft[k]/((double)length);
    
	IFFT(q,out,length,1,out_stride);
}