예제 #1
0
파일: Ceps.cpp 프로젝트: bioidiap/bob.ap
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);
    }
  }
}
bool TePDIWaveletAtrous::RunImplementation_decompose()
{
/* Getting parameters */
	TePDITypes::TePDIRasterPtrType input_raster;
	TEAGN_TRUE_OR_RETURN(params_.GetParameter("input_raster", input_raster), "Missing parameter: input_raster");

	int band;
	TEAGN_TRUE_OR_RETURN(params_.GetParameter("band", band), "Missing parameter: band");

	int levels;
	TEAGN_TRUE_OR_RETURN(params_.GetParameter("levels", levels), "Missing parameter: levels");

	TePDITypes::TePDIRasterVectorType output_wavelets;
	TEAGN_TRUE_OR_RETURN(params_.GetParameter("output_wavelets", output_wavelets), "Missing parameter: output_wavelets");
  
  // Retriving filter matrix

	TeMatrix filter;
  if( params_.CheckParameter< FilterType > ( "filter_type" ) )
  {
    FilterType filterType;
    TEAGN_TRUE_OR_RETURN( params_.GetParameter( "filter_type",
      filterType ), "Missin parameter filter_type" );
      
    TEAGN_TRUE_OR_THROW( filterBank( filterType, filter ), 
      "Filter matrix generation failed");
  }
  else if( params_.CheckParameter< std::string > ( "filter_file" ) )
  {
    std::string filter_file;
    TEAGN_TRUE_OR_RETURN( params_.GetParameter( "filter_file",
      filter_file ), "Missin parameter filter_file" );
  
	  TEAGN_TRUE_OR_THROW( filterBank( filter_file, filter), 
      "Filter matrix generation failed");
  }
  else
  {
    TEAGN_TRUE_OR_THROW( filterBank( TriangleFilter, filter ), 
      "Filter matrix generation failed");
  }

/* Allocating output rasters */	
	/*TeRasterParams base_raster_params = input_raster->params();
	for(int l = 1; l <= levels; l++)
	{
		TeRasterParams input_raster_params = base_raster_params;
		input_raster_params.nBands(waveletPlanes);
		if (input_raster_params.projection() != 0)
		{
			TeSharedPtr<TeProjection> proj(TeProjectionFactory::make(base_raster_params.projection()->params()));
			input_raster_params.projection(proj.nakedPointer());
		}
		input_raster_params.boxResolution(base_raster_params.box().x1(), base_raster_params.box().y1(), base_raster_params.box().x2(), base_raster_params.box().y2(), base_raster_params.resx_, base_raster_params.resy_);
		input_raster_params.setPhotometric(TeRasterParams::TeMultiBand, -1);
		input_raster_params.setDataType(TeFLOAT, -1);
		input_raster_params.setDummy(0.0, -1);
		for(int n = 0; n < waveletPlanes; n++)
			input_raster_params.nbitsperPixel_[n] = base_raster_params.nbitsperPixel_[band];
		TEAGN_TRUE_OR_RETURN(output_wavelets[l]->init(input_raster_params), "Output wavelets reset error " + Te2String(l));
	}*/

/* Setting variables */
	int	l,
		multi,
		x,
		y,
		i,
		j,
		filter_dim = filter.Nrow(),
		offset = (int)(filter_dim / 2),
		k,
		m,
		lines = input_raster->params().nlines_,
		columns = input_raster->params().ncols_;
	double	p_ori,
			p_ant,
			p_new;

/* Computing wavelet decomposition */
	for(l = 1; l <= levels; l++)
	{
		multi = (int)pow(2.0, l-1);
		TePDIPIManager progress("Decomposing Wavelet Level " + Te2String(l), input_raster->params().nlines_, progress_enabled_);
		for (j = 0; j < lines; j++)
		{
			for (i = 0; i < columns; i++)
 			{
				p_ori = p_ant = p_new = 0.0;
				for (k = 0; k < filter_dim; k++)
				{
					for (m = 0; m < filter_dim; m++)
					{
						x = i+(k-offset)*multi;
						y = j+(m-offset)*multi;
						if (x < 0)
							x = columns + x;
						else if (x >= columns)
							x = x - columns;
						if (y < 0)
							y = lines + y;
						else if (y >= lines)
							y = y - lines;
						output_wavelets[l-1]->getElement(x, y, p_ori, 0);
						p_new += p_ori * filter(k, m);
					}
				}
				output_wavelets[l]->setElement(i, j, p_new, 0);
				output_wavelets[l-1]->getElement(i, j, p_ori, 0);
				output_wavelets[l]->setElement(i, j, p_ori-p_new, 1);
			}
			progress.Increment();
		}
	}

	return true;
}