void PSAnalysis::Analysis()
{
	//Starts now
	
	//Windowing

	double norm = sqrt( N/(2.0*hopa) );
	
	for (int i=0; i<N; i++)
		frames2[i] = frames[i]*w(i)/norm;
	
	/*Analysis*/
	if (p) fftwf_execute(p);
	
	/*Processing*/
	
	for (int i=0; i<(N/2 + 1); i++)
	{
		Xa(i) = cx_float(fXa[i][0], fXa[i][1]);
		Xa_arg(i) = angle(Xa(i));
	}
	d_phi = Xa_arg - XaPrevious_arg;
	d_phi_prime = d_phi - ((2*M_PI * hopa) / N) * I;
	AUX = floor((d_phi_prime + M_PI) / (2*M_PI));
	d_phi_wrapped = d_phi_prime - AUX * (2*M_PI);
	omega_true_sobre_fs = (2*M_PI/N) * I + d_phi_wrapped / hopa;

	Xa_abs = abs(Xa);
	XaPrevious = Xa;
	XaPrevious_arg = Xa_arg;
}
예제 #2
0
void PitchDetection::FindNote()
{
	for (int i=0; i<N; i++)
	{
		frames[N+i] = 0;
	}
	
	if (p) fftwf_execute(p);
	
	for (int i=0; i<(N + 1); i++)
	{
		Xa(i) = cx_float(fXa[i][0], fXa[i][1]);
	}
	
	Xs = Xa % conj(Xa);
	
	for (int i=0; i<(N + 1); i++)
	{
		fXs[i][0] = real(Xs(i));
		fXs[i][1] = imag(Xs(i));
	}
	
	if (p2) fftwf_execute(p2);
	
	for (int i=0; i<N; i++)
	{
		R(i) = q[i]/(2*N); 
	}
	
	NORM.zeros();
	
	NORM(0) = 2*R(0);
	
	for (int i=1; i<N; i++)
	{
		NORM(i) = NORM(i-1) - pow(frames[i-1],2)- pow(frames[N-i],2);
	}
	
	
	for (int i=0; i<N; i++)
	{
		F(i) = 1 -0.05*i/(N-1);
	}
	
	AUTO = ( 2*F % R )/NORM;
	
	int flag = 0;
	
	for (int i=0; (i<N)&&(flag==0); i++)
	{
		if( AUTO(i) > 0 )
		{
			AUTO(i) = 0;
		}
		else
		{
			flag = 1;
		}
	}
	
	uword max_index;
	double fidelity = AUTO.max(max_index);
	
	
	if ((fidelity > 0.95) && (fidelity < 1) && ((int)max_index < N-1))
	{
	
		double a = AUTO(max_index-1);
		double b = AUTO(max_index);
		double c = AUTO(max_index+1);
	
		double real_index = max_index + 0.5*(a-c)/(a-2*b+c);
	
		f = fs/real_index;
		int nota = (int)round( (12/log(2))*log(f/16.351597831287414) );
		oitava = floor(nota/12.0);
		note = nota % 12;
	
		//cout << "nota = " << note[0] << " oitava = " << oitava[0] << " fidelity = " << fidelity << "\n";	
		
	}
}