Exemple #1
0
int main() {
	/* Initialization */
	calc_shelving_coef();
	calc_hamming_coef();
	calc_bank_gain();
	twidfftrad2_fr32(twiddle_table, WINDOW_LENGTH);
	calc_dct_coef();
	/* /Initialization */

	int index;
	for (index = 0; index < TOTAL_LENGTH; index++) {
		input_fr[index] = float_to_fr32(test_input[index]);
	}

	pre_emphasis(input_fr, TOTAL_LENGTH);
	// printf("pre emphasis done\n");

	int frame_offset = 0;
	fract32 frame_data[WINDOW_LENGTH];
	int obs_length = 0;

	int frame_num;
	for (frame_num = 0; frame_num < FRAME_NUM; frame_num++) {
		for (index = 0; index < WINDOW_LENGTH; index++) {
			frame_data[index] = input_fr[index+frame_offset];
		}

		hamming(frame_data);

		float energy = calc_energy(frame_data, WINDOW_LENGTH);

		if (energy > 0.1) {
			float mfcc[FEAT_NUM] = {0.0};
			calc_mfcc(frame_data, mfcc_matrix[obs_length]);
			obs_length++;
		}
		frame_offset += WINDOW_LENGTH/2;
	}

	int feat_num;
	for (frame_num = 0; frame_num < obs_length; frame_num++) {
		for (feat_num = 0; feat_num < FEAT_NUM; feat_num++) {
			printf("%f\t", mfcc_matrix[frame_num][feat_num]);
		}
		printf("\n");
	}

	return 0;
}
int main() {
	fract16 data[WINDOW_LENGTH];

	int index;
	for (index = 0; index < WINDOW_LENGTH; index++) {
		data[index] = float_to_fr16(test_input[index]);
	}

	struct Coef shelving_coef = get_shelving_coef();
	pre_emphasis(data, WINDOW_LENGTH, shelving_coef);

	for (index = 0; index < WINDOW_LENGTH; ++index) {
		float test = fr16_to_float(data[index]);
		// printf("%f\n", fr16_to_float(data[index]));
	}
	return 0;
}
Exemple #3
0
void Ambix_warpAudioProcessor::calcParams()
{
    if (!_initialized)
    {
    
        sph_h_in.Init(in_order);
        
        const String t_design_txt (t_design::des_3_240_21_txt);
        
        // std::cout << t_design_txt << std::endl;
        
        String::CharPointerType lineChar = t_design_txt.getCharPointer();
        
        int n = 0; // how many characters been read
        int numsamples = 0;
        int i = 0;
        
        int curr_n = 0;
        int max_n = lineChar.length();
        
        while (curr_n < max_n) { // check how many coordinates we have
            double value;
            sscanf(lineChar, "%lf\n%n", &value, &n);
            lineChar += n;            
            curr_n += n;
            numsamples++;            
        } // end parse numbers
        
        numsamples = numsamples/3; // xyz
        
        Carth_coord.resize(numsamples,3); // positions in cartesian coordinates
        
        curr_n = 0;
        lineChar = t_design_txt.getCharPointer();
        
        // parse line for numbers again and copy to carth coordinate matrix
        while (i < numsamples) {
            
            double x,y,z;
            
            sscanf(lineChar, "%lf%lf%lf%n", &x, &y, &z, &n);
            
            Carth_coord(i,0) = x;
            Carth_coord(i,1) = y;
            Carth_coord(i,2) = z;
            
            lineChar += n;
            
            curr_n += n;
            i++;
            
        } // end parse numbers
        
        // std::cout << "Coordinate size: " << Carth_coord.rows() << " x " << Carth_coord.cols() << std::endl;
        // std::cout << Carth_coord << std::endl;
        
        Sph_coord.resize(numsamples,2); // positions in spherical coordinates
        
        Eigen::MatrixXd Sh_matrix;
        Sh_matrix.setZero(numsamples,AMBI_CHANNELS);
        
        int in_ambi_channels = (in_order+1)*(in_order+1);
        for (int i=0; i < numsamples; i++)
        {
            Eigen::VectorXd Ymn(in_ambi_channels); // Ymn result
            
            Sph_coord(i,0) = atan2(Carth_coord(i,1),Carth_coord(i,0)); // azimuth
            Sph_coord(i,1) = atan2(Carth_coord(i,2),sqrt(Carth_coord(i,0)*Carth_coord(i,0) + Carth_coord(i,1)*Carth_coord(i,1))); // elevation
            
            sph_h_in.Calc(Sph_coord(i,0),Sph_coord(i,1)); // phi theta
            sph_h_in.Get(Ymn);
            
            // std::cout << "Ymn Size: " << Ymn.size() << std::endl;
            // std::cout << "Sh_matrix Size: " << Sh_matrix.size() << std::endl;
            
            //Sh_matrix.row(i) = Ymn;
            Sh_matrix.block(i,0,1,in_ambi_channels) = Ymn.transpose();
            // std::cout << "Size: " << Sh_matrix.block(i,0,1,in_ambi_channels).size() << ": " << Sh_matrix.block(i,0,1,in_ambi_channels) << std::endl;
        }
        
        // Sh_matrix_inv.setZero();
        
        //Sh_matrix_inv = (Sh_matrix.transpose()*Sh_matrix).inverse()*Sh_matrix.transpose(); // not working for dynamic input order
        // if input order is different a better solving has to be used for the inverse:
        Sh_matrix_inv = (Sh_matrix.transpose()*Sh_matrix).colPivHouseholderQr().inverse()*Sh_matrix.transpose();
        
        // std::cout << "Size Sh_matrix: " << Sh_matrix.rows() << " x " << Sh_matrix.cols() << std::endl;
        
        // std::cout << "Size Inverse: " << Sh_matrix_inv.rows() << " x " << Sh_matrix_inv.cols() << std::endl;
        
        // std::cout << Sh_matrix_inv << std::endl;
    
        _initialized = true;
    }
    
    if ( (phi_param != _phi_param) ||
        (floor(phi_curve_param+0.5f) != floor(_phi_curve_param+0.5f)) ||
        (theta_param != _theta_param) ||
        (floor(theta_curve_param+0.5f) != floor(_theta_curve_param+0.5f)) ||
        (in_order != _in_order) ||
        (out_order != _out_order) ||
        (floor(preemp_param+0.5f) != floor(_preemp_param+0.5f))
        )
    {
        
        if (_out_order != out_order)
        {
            // init or reinit spherical harmonics
            sph_h_out.Init(out_order);
        }
        
        Eigen::MatrixXd Sh_matrix_mod;
        Sh_matrix_mod.setZero(Sph_coord.rows(),AMBI_CHANNELS);
        
        Eigen::VectorXd pre_emphasis;
        pre_emphasis.setOnes(Sph_coord.rows());
        
        Eigen::MatrixXd Sph_coord_mod = Sph_coord;
        
        // warping parameters - use inverse parameters! (see paper)
        double phi_alpha = -((double)phi_param*1.8f - 0.9f);
        double theta_alpha = -((double)theta_param*1.8f - 0.9f);
        
        int out_ambi_channels = (out_order+1)*(out_order+1);
        
        for (int i=0; i < Sph_coord_mod.rows(); i++)
        {
            if (theta_alpha != 0.)
            {
                // warp elevation
                // as we are using elevation from -pi/2....pi/2
                double mu = sin(Sph_coord(i,1));
                
                if (theta_curve_param <= 0.5f)
                {
                    // warp towards a pole
                    
                    Sph_coord_mod(i,1) = asin((mu+theta_alpha)/(1+theta_alpha*mu));
                    
                    // pre emphasis
                    pre_emphasis(i) = (1+theta_alpha*mu)/sqrt(1-theta_alpha*theta_alpha);
                } else {
                    //warp towards equator (< 0.)or away from equator (> 0.)
                    // in the paper this parameter is called beta
                    
                    // pre emphasis
                    pre_emphasis(i) = sqrt((1-abs(theta_alpha))*(1+abs(theta_alpha)*mu*mu))/(1-abs(theta_alpha)*mu*mu);
                  
                    if (theta_alpha > 0.) {
                        
                        Sph_coord_mod(i,1) = asin((theta_alpha-1+sqrt(pow(theta_alpha-1,2)+4*theta_alpha*pow(mu,2)))/(2*theta_alpha*mu));
                        
                    } else {
                        Sph_coord_mod(i,1) = asin(((1+theta_alpha)*mu)/(1+theta_alpha*pow(mu,2)));
                        pre_emphasis(i) = 1/pre_emphasis(i); // this is ^sgn(beta)
                    }
                    
                }
            }
            /*
             // this is not fully working yet
            if (phi_alpha != 0.)
            {
                // warp azimuth
                // as we are using azimuth from -pi....pi
                double phi_temp = Sph_coord(i,0) * 0.5f;
                
                double mu = sin(phi_temp);
                
                if (phi_curve_param <= 0.5f)
                {
                    // warp towards a pole
                    
                    Sph_coord_mod(i,0) = asin((mu+phi_alpha)/(1+phi_alpha*mu));
                    
                    // pre emphasis
                    pre_emphasis(i) *= (1+phi_alpha*mu)/sqrt(1-phi_alpha*phi_alpha);
                } else {
                    //warp towards equator (< 0.)or away from equator (> 0.)
                    // in the paper this parameter is called beta
                    
                    // pre emphasis
                    double pre_emph_temp = sqrt((1-abs(phi_alpha))*(1+abs(phi_alpha)*mu*mu))/(1-abs(phi_alpha)*mu*mu);
                    
                    if (phi_alpha > 0.) {
                        
                        Sph_coord_mod(i,0) = asin((phi_alpha-1+sqrt(pow(phi_alpha-1,2)+4*phi_alpha*pow(mu,2)))/(2*phi_alpha*mu));
                        pre_emphasis(i) *= pre_emph_temp;
                        
                    } else {
                        Sph_coord_mod(i,0) = asin(((1+phi_alpha)*mu)/(1+phi_alpha*pow(mu,2)));
                        pre_emphasis(i) *= 1/pre_emph_temp; // this is ^sgn(beta)
                    }
                    
                }
                Sph_coord_mod(i,0) *= 2;
            }
            */
            
            if (phi_alpha != 0.f)
            {
                // warp azimuth
                // as we are using azimuth from -pi....pi
                double phi_temp = Sph_coord(i,0) * 0.5f;
                
                if (phi_curve_param <= 0.5f)
                {
                    // warp towards poles
                    Sph_coord_mod(i,0) = asin((sin(phi_temp)+phi_alpha)/(1.f+phi_alpha*sin(phi_temp)));
                } else {
                    //warp towards equator
                    
                    if (phi_temp > 0)
                    {
                        Sph_coord_mod(i,0) = acos((cos(phi_temp*2)+phi_alpha)/(1+phi_alpha*cos(phi_temp*2))) / 2;
                    } else {
                        Sph_coord_mod(i,0) = -acos((cos(M_PI-(phi_temp-M_PI/2)*2)+phi_alpha)/(1+phi_alpha*cos(M_PI-(phi_temp-M_PI/2)*2))) / 2;
                    }
                }
                
                Sph_coord_mod(i,0) *= 2;
                
            }
            
            // rotate
            // Sph_mod(i,0) = Sph(i,0);
            
            Eigen::VectorXd Ymn(out_ambi_channels); // Ymn result
            
            sph_h_out.Calc(Sph_coord_mod(i,0),Sph_coord_mod(i,1)); // phi theta
            sph_h_out.Get(Ymn);
            
            // Sh_matrix_mod.row(i) = Ymn;
            Sh_matrix_mod.block(i,0,1,out_ambi_channels) = Ymn.transpose();
        }
        
        // apply preemphasis if wanted
        if (preemp_param > 0.5f)
        {
            Sh_matrix_mod = Sh_matrix_mod * pre_emphasis.asDiagonal();
        }
        
        // calculate new transformation matrix
        
        Sh_transf = Sh_matrix_inv * Sh_matrix_mod;
        
        // threshold coefficients
        for (int i = 0; i < Sh_transf.size(); i++)
        {
            if (abs(Sh_transf(i)) < 0.00001f)
                Sh_transf(i) = 0.f;
        }
        
        _phi_param = phi_param;
        _phi_curve_param = phi_curve_param;
        _theta_param = theta_param;
        _theta_curve_param = theta_curve_param;
        _in_order = in_order;
        _out_order = out_order;
        _preemp_param = preemp_param;
    }
    
    
    
}
Exemple #4
0
void bob::ap::Ceps::operator()(const blitz::Array<double,1>& input,
  blitz::Array<double,2>& ceps_matrix)
{

  // Get expected dimensionality of output array
  blitz::TinyVector<int,2> feature_shape = bob::ap::Ceps::getShape(input);
  // Check dimensionality of output array
  bob::core::array::assertSameShape(ceps_matrix, feature_shape);
  int n_frames=feature_shape(0);
  int shift_frame=0;
  double last_frame_elem=0;

  // Create the holder for the previous frame and make sure it's the same as the current frame
  // Used by SSFC features computation
  blitz::Array<double,1> _prev_frame_d;
  _prev_frame_d.resize(m_cache_frame_d.shape());
  // Create the temporary holder for SSFC features computation
  blitz::Array<double,1> _temp_frame_d;
  _temp_frame_d.resize(m_cache_frame_d.shape());

  if (m_ssfc_features) {
    //we are going to always process the next frame within the loop
    shift_frame = 1;
    // Init the first frame to the input
    extractNormalizeFrame(input, 0, _prev_frame_d);
    // Apply pre-emphasis
    pre_emphasis(_prev_frame_d, last_frame_elem);
    // Apply the Hamming window
    hammingWindow(_prev_frame_d);
    // Take the power spectrum of the first part of the FFT
    powerSpectrumFFT(_prev_frame_d);

  }

  blitz::Range r1(0,m_n_ceps-1);
  for (int i=0; i<n_frames; ++i)
  {
    // Init the current frame from the input, we process (i+1)th frame for SSFC features
    extractNormalizeFrame(input, i+shift_frame, m_cache_frame_d);

    // Update output with energy if required
    if (m_with_energy)
      ceps_matrix(i,(int)m_n_ceps) = logEnergy(m_cache_frame_d);

    // Apply pre-emphasis
    pre_emphasis(m_cache_frame_d, last_frame_elem);
    // Apply the Hamming window
    hammingWindow(m_cache_frame_d);
    // Take the power spectrum of the first part of the FFT
    // Note that after this call, we only operate on the first half of m_cache_frame_d array. The second half is ignored.
    // powerSpectrumFFT changes first half+1 elements of m_cache_frame_d array
    powerSpectrumFFT(m_cache_frame_d);

    if (m_ssfc_features)
    {
      // retrieve the previous frame into our temp
      _temp_frame_d = _prev_frame_d;
      // remember the current frame for the next round, before we change current frame
      _prev_frame_d = m_cache_frame_d;
      // Computation of SSFC features:
      // We take the previous frame and find the difference between values of current and previous frames
      m_cache_frame_d -= _temp_frame_d;
      // We compute norm2 for the difference as per SSFC features
      m_cache_frame_d = blitz::pow2(m_cache_frame_d);
      // Then, we can apply the filter and DCT later on
    }
    // Filter with triangular or rectangular filter bank (either in linear or Mel domain)
    filterBank(m_cache_frame_d);

    // Apply DCT kernel and update the output
    blitz::Array<double,1> ceps_matrix_row(ceps_matrix(i,r1));

    if (m_scfc_features)
      // do not apply DCT on SCFC features
      ceps_matrix_row = m_cache_filters(r1);
    else
      applyDct(ceps_matrix_row);
  }

  //compute the center of the cut-off frequencies
  const int n_coefs = (m_with_energy ?  m_n_ceps + 1 :  m_n_ceps);
  blitz::Range rall = blitz::Range::all();
  blitz::Range ro0(0,n_coefs-1);
  blitz::Range ro1(n_coefs,2*n_coefs-1);
  blitz::Range ro2(2*n_coefs,3*n_coefs-1);
  if (m_with_delta)
  {
    blitz::Array<double,2> ceps_matrix_0(ceps_matrix(rall,ro0));
    blitz::Array<double,2> ceps_matrix_1(ceps_matrix(rall,ro1));
    addDerivative(ceps_matrix_0, ceps_matrix_1);

    if (m_with_delta_delta)
    {
      blitz::Array<double,2> ceps_matrix_2(ceps_matrix(rall,ro2));
      addDerivative(ceps_matrix_1, ceps_matrix_2);
    }
  }
}