1. 程式人生 > >基於eigen實現matlab hamming hann blakman 窗函式的實現

基於eigen實現matlab hamming hann blakman 窗函式的實現

//窗函式
VectorXd calc_window(int half, int n, int window)
{
	VectorXd  x(half);
	//x.setllinspace((Type)0, (Type)(half - 1), half) / (Type)(n - 1));
	
	x.setLinSpaced(half, 0, (half - 1));
	x = x.array() / (n - 1);
	VectorXd  w(half);
	if (window == 0)//hamming
		w = (Type)0.54 - (Type)0.46*(2 * M_PI*x).array().cos();
	else if (window == 1)//hann
		w = (Type)0.5 - (Type)0.5*(2 * M_PI*x.array()).cos();
	else if (window == 2)//blackman
	{
		w = (Type)0.42 - (Type)0.5*((Type)2 * M_PI*x.array()).cos() + (Type)0.08*(4 * M_PI*x.array()).cos();
		w[0] = 0;
	}
	else
	{
		Type a0 = 0.21557895;
		Type a1 = 0.41663158;
		Type a2 = 0.277263158;
		Type a3 = 0.083578947;
		Type a4 = 0.006947368;
		w = a0 - a1*(2 * M_PI*x.array()).cos() + a2*(4 * M_PI*x.array()).cos() - a3*(6 * M_PI*x.array()).cos() + a4*(8 * M_PI*x.array()).cos();
	}
	return w;
}
// window =0 'hamming',  1  'hann',    2   'blackman' 3 'flattopwin'.  n 階數
VectorXd sym_window(int n, int window)
{
	int half;
	VectorXd w1, w2;
	if (n % 2 == 0)//偶數階
	{
		half = n / 2;
		w1 = calc_window(half, n, window);
		w2 = w1.reverse();
	}
	else
	{
		half = (n + 1) / 2;
		w1 = calc_window(half, n, window);
		w2 = w1.reverse().segment(1,n-half);
	}

	VectorXd w(w1.size() + w2.size());
	w << w1, w2;
	return w;

}
//n 階數   type: true->symmetric 對稱  false->periodic 
// window =0 'hamming',  1  'hann',    2   'blackman'.
VectorXd gencoswin(int n, int window, bool type)
{
	if (type)
	{
		VectorXd w = sym_window(n, window);
		return w;


	}
	else
	{
		VectorXd w = sym_window(n + 1, window);
		w.resize(n);
		return w;

	}

}