예제 #1
0
//=======================================================================================
// EJECUCIÓN FILTRO FIR
//=======================================================================================
int main(int argc, char *argv[])
{
	unsigned short  i,j,L,LE,LE1;

	for (L=1; L<=EXP; L++)		// Create twiddle factor table
	{
		LE=1<<L;
		LE1=LE>>1;
		W[L-1].re = cos(pi/LE1);
		W[L-1].im = -sin(pi/LE1);
	}

	for (j=0; j<(13*N); j++)	// este for deberia ser solo hasta j<13, para lograr el efecto de 13*128=1664 (tamaño del archivo .dat)
	{
		for (i=0; i<N; i++);
		{
			X[i].re = input7_f[j++];	// input7_f[j*i]
			X[i].im = 0.0;
		}

		// Start FFT
		bit_rev(X,EXP);
		fft(X,EXP,W,1);

		for (i=0; i<N; i++)
		{
			temp.re = X[i].re*X[i].re;
			temp.im = X[i].im*X[i].im;
			spectrum[i] = (short)((temp.re+temp.im)*32767);		// spectrum[j*i]
		}
	}
	return (0);
}
예제 #2
0
파일: fft.c 프로젝트: RubiksD/Cordic
void ifft_Npt(int *inputX, int *inputY, int *X_out, int *Y_out,int N)
{
	int i,j,k;
	int offset=0;
	int Set_Count=1;
#ifdef DOUBLE_CALCULATIONS
	double theta = convert_to_cordic_weight(2*pi/N);
	double Set_theta,Butterfly_theta;
#else
	int theta = convert_to_cordic_weight(2*pi/N);
	int Set_theta,Butterfly_theta;
#endif
	int X_in[1024],Y_in[1024];
	int X_temp[3],Y_temp[3];
	for(i=0;i<N;i++){
		X_in[i]=inputX[i];
		Y_in[i]=inputY[i];
	}

	Set_theta = theta;
	for(i=N/2;i>=1;i=i/2){
		offset = 0;
		for(j=0;j<Set_Count;j++){
			Butterfly_theta = 0;
			for(k=0;k<i;k++){
				X_temp[0] = X_in[k+offset]+X_in[k+i+offset];
				Y_temp[0] = Y_in[k+offset]+Y_in[k+i+offset];

				X_temp[1] = X_in[k+offset]-X_in[k+i+offset];
				Y_temp[1] = Y_in[k+offset]-Y_in[k+i+offset];

				//X_temp[2] = (X_temp[1]*cos(Butterfly_theta)) + (Y_temp[1]*sin(Butterfly_theta));
				//Y_temp[2] = (-(X_temp[1]*sin(Butterfly_theta))) + (Y_temp[1]*cos(Butterfly_theta));
#ifdef DOUBLE_CALCULATIONS
				cordic_rotate_double(&X_temp[1],&Y_temp[1],-Butterfly_theta);
#else
				cordic_rotate_int(&X_temp[1],&Y_temp[1],-Butterfly_theta);
#endif
				X_temp[2] = X_temp[1];
				Y_temp[2] = Y_temp[1];

				X_in[k+offset] = X_temp[0]>>1;
				Y_in[k+offset] = Y_temp[0]>>1;
				X_in[k+i+offset] = X_temp[2]>>1;
				Y_in[k+i+offset] = Y_temp[2]>>1;

				Butterfly_theta += Set_theta;
			}
			offset +=i*2;
		}
		Set_Count = Set_Count*2;
		Set_theta = Set_theta*2;
	//	printf("---------\n");
	}
	for(i=0;i<N;i++){
		j = bit_rev(i,N);
		X_out[i] = X_in[j];
		Y_out[i] = Y_in[j];
	}
}
예제 #3
0
void FFT(float*x, float*X, unsigned short M) {
	
	int N;
	N = (int) 2*M; //Integer of merit for many functions called herein.  Equal to the number of complex values that'll be in 'X' (which is twice as many as in 'x')

	int a; //Counter variable, gets used in several "for" loops.

	int b; /*This will be used for some of the bookkeeping in the loop immediately below */
	int c; /*Similar to b, this is used for different matrix indexing.*/

	for (a=0; a<M; a++) //Assign the data from x to every other slot in X, inserting zeroes in-between.
	{
		b=2*a;
		c=1+2*a;
		X[b]=x[a];
		X[c]=0;
	}

	for (a=2*M; a<2*N; a++) //Appends the additional zeroes on to the end.  Note that this is both the real and imaginary elements
	{
		X[a]=0;
	}

	float* w =(float*)malloc(sizeof(float)*M); //"w," twiddle-factor container, must be defined in this way since its length depends on a variable
	gen_w_r2(w,N); //Twiddle factor generation; function prototyped and included below

//------ACTUAL FFT HERE:----------------
	bit_rev(w, N >> 1);
	DSPF_sp_cfftr2_dit(X,w,N);
	bit_rev(X,N);

	 	 	 	 /* These to bits (bit_rev & 'for' loop) are the parts that make the 'raw' outputs look like the Matlab outputs.  To
	 	 	 	  * re-include them in the code to be compiled, simply remove the comments around them. */

		for (a=0;a<2*N;a=a+2)
			{
				x[a+1]=-x[a+1];
			}

//--------------------------------------

}
예제 #4
0
파일: fft.c 프로젝트: RubiksD/Cordic
void fft_Npt_DiT(int *input, double *X_out, double *Y_out,int N)
{
	int i,j,k;
	int offset=0;
	int Set_Count=1;
	double theta = 2*pi/N;
	double X_in[1024],Y_in[1024];
	double X_temp[3],Y_temp[3];
	for(i=0;i<N;i++){
		j = bit_rev(i,N);
		X_in[j]=(double)input[i];
		Y_in[j]=0;
	}

	for(i=N/2;i>=1;i=i/2){
		offset = 0;
		for(j=0;j<N/(i*2);j++){
			for(k=0;k<i;k++){
				X_temp[0] = X_in[k+offset]+X_in[k+i+offset];
				Y_temp[0] = Y_in[k+offset]+Y_in[k+i+offset];

				X_temp[1] = X_in[k+offset]-X_in[k+i+offset];
				Y_temp[1] = Y_in[k+offset]-Y_in[k+i+offset];

				X_temp[2] = (X_temp[1]*cos(theta*Set_Count*k)) + (Y_temp[1]*sin(theta*Set_Count*k));
				Y_temp[2] = (-(X_temp[1]*sin(theta*Set_Count*k))) + (Y_temp[1]*cos(theta*Set_Count*k));

				X_in[k+offset] = X_temp[0];
				Y_in[k+offset] = Y_temp[0];
				X_in[k+i+offset] = X_temp[2];
				Y_in[k+i+offset] = Y_temp[2];
			}
			offset +=i*2;
		}
		Set_Count = Set_Count*2;
		printf("---------\n");
	}
}
예제 #5
0
void main(void)
{

	float A[128] = {43.000000,0.000000,  3.616321,30.125547,  -14.479670,-2.567202,  12.362492,-21.842867,  32.385956,13.828468,  -7.707342,18.480019,  1.519983,-3.868871,  4.327905,-4.279924,  20.798990,2.485281,  2.305606,11.042850,  16.090977,2.064070,  5.466386,19.738562,  0.345887,10.251665,  -3.438493,4.526769,  12.931265,9.287642,  -14.795074,12.285769,  17.000000,-10.000000,  4.956760,30.588863,  -4.126019,7.640384,  -12.621098,21.370527,  -6.488023,-10.174742,  1.631006,20.984383,  -14.443722,-3.912756,  6.433661,14.007210,  -18.798990,14.485281,  -23.586613,0.723100,  -5.995662,-12.674122,  -23.052052,8.538252,  -10.243816,-50.597946,  35.359089,3.127013,  8.502851,-7.042882,  24.741423,53.781017,  -81.000000,0.000000,  24.741428,53.781006,  8.502846,-7.042882,  35.359089,3.127012,  -10.243820,-50.597939,  -23.052046,8.538255,  -5.995665,-12.674120,  -23.586613,0.723101,  -18.798990,14.485281,  6.433663,14.007207,  -14.443724,-3.912753,  1.631013,20.984381,  -6.488022,-10.174742,  -12.621092,21.370529,  -4.126016,7.640386,  4.956764,30.588863,  17.000000,-10.000000,  -14.795071,12.285774,  12.931269,9.287637,  -3.438493,4.526770,  0.345886,10.251665,  5.466391,19.738560,  16.090977,2.064065,  2.305608,11.042850,  20.798990,2.485281,  4.327904,-4.279922,  1.519982,-3.868872,  -7.707339,18.480021,  32.385952,13.828463,  12.362488,-21.842869,  -14.479672,-2.567199,  3.616327,30.125546};
	unsigned short N=64; //There're 64 complex numbers in A, taking up 128 array entries.


	//PRINT FFT'd VALUES (inputs)
	printf("Original Inputs (Results of an FFT w/ Post-Processing (a-la Matlab):");
	printf("\n");
	int i;
	int a;
	for(i=0;i<N;i=i+2)
	{
		printf("%f",A[i]);
		printf("  ");
		printf("%f i",A[i+1]);
		printf("\n");
	}

	printf("\n");

	float* w =(float*)malloc(sizeof(float)*(N/2));
	gen_w_r2(w,N);
	bit_rev(w, N>>1 );

	printf("\n");

	//"Fixing" the FFT results; I.E. undoing post-processing----------------
	for (a=0;a<N;a=a+2)
	{
		A[a+1]=-A[a+1];
	}
	bit_rev(A,N);
	//----------------------------------------------------------------------

	printf("FFT'd Results, WITHOUT Post-Processing");
	printf("\n");
	for (i=0;i<N;i=i+2)
	{
		printf("%f",A[i]);
		printf("  ");
		printf("%f",A[i+1]);
		printf(" i;");
		printf("\n");
	}
	printf("\n");

	//IFFT here:
	DSPF_sp_icfftr2_dif(A,w,N);



	//Scale by N
	for (i = 0; i < N*2; i++) /*ACHTUNG! Originally the limiting condition was "i < N*2"  because "N" represents the number of COMPLEX
								numbers in the array; since it takes two array entries (real & imaginary) to represent one complex\
								number, the array is twice as long as this "N."  If there's a problem with the IFFT, consider changing
								this first to see if anything's solved.*/
	   {
	       A[i] /= N;
	   }

	printf("IFFT'd Final Outputs");
	printf("\n");
	//PRINT IFFT'd VALUES (OUTPUTS)
	for (i=0;i<N;i=i+2)
	{
		printf("%f",A[i]);
		printf("  ");
		printf("%f",A[i+1]);
		printf(" i;");
		printf("\n");
	}

	//Now we should have no complex values, so even indices of Y are real-valued and odd (cpx) indices or Y are zeroes.  We now chop out
	//these interstitial zeroes
	float* y =(float*)malloc(sizeof(float)*((N/2)-1));
	for (i=0; i<N;i++)
	{
		y[i]=A[2*i];
	}

}
예제 #6
0
/* Autocorrelation-based LPC analysis
	Performs the LPC analysis on a given frame... returns the lpc coefficients
	Input arguments
	xx: input buffer pointer
	aa: LPC coefficients pointer (output)
	size: size of the input buffer (xx)
	nlpc: LPC order, i.e., number of LPC coefficients
	SC (2008/05/06): Incoroporating cepstral lifting */
void LPFormantTracker::getAi(dtype* xx, dtype* aa) {
	int i0;	

	//dtype temp_frame[maxBufLen + maxNLPC];	// Utility buffer for various filtering operations
	//dtype R[maxNLPC];					// lpc fit Autocorrelation estimate
	
	int nlpcplus1 = nLPC + 1;

	for(i0 = 0; i0 < nlpcplus1; i0++)
		temp_frame[i0] = 0;
	
	// Window input
	//SC vecmu1: vector multiply
	//SC	hwin: a Hanning window
	DSPF_dp_vecmul(xx, winFunc, &temp_frame[nlpcplus1], bufferSize);	// Apply a Hanning window
	// TODO: Check temp_frame size

	////////////////////////////////////////////////////
	//SC(2008/05/07) ----- Cepstral lifting -----
	if (bCepsLift){
		for (i0=0;i0<nFFT;i0++){
			if (i0 < bufferSize){
				ftBuf1[i0 * 2] = temp_frame[nlpcplus1 + i0];
				ftBuf1[i0 * 2 + 1]=0;
			}
			else{
				ftBuf1[i0 * 2] = 0;
				ftBuf1[i0 * 2 + 1] = 0;
			}
		}
		DSPF_dp_cfftr2(nFFT, ftBuf1, fftc, 1);	
		bit_rev(ftBuf1, nFFT);
		// Now ftBuf1 is X
		for (i0 = 0; i0 < nFFT; i0++){
			if (i0 <= nFFT / 2){
				ftBuf2[i0 * 2] = log(sqrt(ftBuf1[i0*2]*ftBuf1[i0*2]+ftBuf1[i0*2+1]*ftBuf1[i0*2+1]));	// Optimize
				ftBuf2[i0 * 2 + 1]=0;
			}
			else{
				ftBuf2[i0 * 2] = ftBuf2[(nFFT - i0) * 2];
				ftBuf2[i0 * 2 + 1]=0;
			}
		}
		DSPF_dp_icfftr2(nFFT, ftBuf2, fftc, 1);
		bit_rev(ftBuf2, nFFT);

		// Now ftBuf2 is Xceps: the cepstrum
		for (i0 = 0; i0 < nFFT; i0++){
			if (i0 < cepsWinWidth || i0 > nFFT - cepsWinWidth){			// Adjust! 
				ftBuf1[i0 * 2] = ftBuf2[i0 * 2] / nFFT;		// Normlize the result of the previous IFFT
				ftBuf1[i0 * 2 + 1] = 0;
			}
			else{
				ftBuf1[i0 * 2] = 0;
				ftBuf1[i0 * 2 + 1] = 0;
			}
		}
		// Now ftBuf1 is Xcepw: the windowed cepstrum
		DSPF_dp_cfftr2(nFFT,ftBuf1,fftc,1);
		bit_rev(ftBuf1,nFFT);
		for (i0=0;i0<nFFT;i0++){
			if (i0<=nFFT/2){
				ftBuf2[i0*2]=exp(ftBuf1[i0*2]);
				ftBuf2[i0*2+1]=0;
			}
			else{
				ftBuf2[i0*2]=ftBuf2[(nFFT-i0)*2];
				ftBuf2[i0*2+1]=0;
			}
		}
		DSPF_dp_icfftr2(nFFT,ftBuf2,fftc,1);	// Need normalization
		bit_rev(ftBuf2,nFFT);
		
		for (i0 = 0; i0 < bufferSize / 2; i0++){
			temp_frame[nlpcplus1 + bufferSize / 2 + i0] = ftBuf2[i0 * 2] / nFFT;
		}
		for (i0 = 1; i0 < bufferSize / 2; i0++){
			temp_frame[nlpcplus1 + bufferSize / 2 - i0] = ftBuf2[i0 * 2] / nFFT;
		}
		temp_frame[nlpcplus1] = 0.0;
	}
	//~SC(2008/05/07) ----- Cepstral lifting -----
	///////////////////////////////////////////////////////////

	// Find autocorrelation values
	DSPF_dp_autocor(R, temp_frame, bufferSize, nlpcplus1);		//SC Get LPC coefficients by autocorrelation

	// Get unbiased autocorrelation
	for(i0 = 0; i0 < nlpcplus1; i0++)
		R[i0] /= bufferSize;

	// levinson recursion
	levinson(R, aa, nlpcplus1);
}
예제 #7
0
int str_match(const char *str, const char *format) {

    const char *s   = str;
    const char *fmt = format;
    unsigned int flag = 0;
    int width = 0;

    bit_t bit;
    char c;
    const char *s0;

    while (*fmt) {
        if (*fmt == '%') {
            fmt++;

            while (*fmt >= '0' && *fmt <= '9') 
                width = width * 10 + *fmt++ - '0';

            switch(*fmt) {
                case 'd':
                    if (width) {
                        while (*s && isdigit(*s) && width-- > 0)
                            s++;
                    } else {
                        while (*s && isdigit(*s))
                            s++;
                    }
                    fmt++;
                    break;

                case '[':
                    fmt++;
                    bit_zero(&bit);

                    if (*fmt == '^') {
                        fmt++;
                        flag |= STATUS_CARET;
                    }

                    while (*fmt && *fmt != ']') {
                        if (fmt[1] == '-') {
                            for (c = *fmt; c < fmt[2]; c++) {
                                bit_set(&bit, c);
                            }
                            if (c == fmt[2])
                                fmt += 2;
                        } else {
                            bit_set(&bit, *fmt);
                            fmt++;
                        }
                    }

                    if (flag & STATUS_CARET) {
                        bit_rev(&bit);
                        /* clear STATUS_CARET */
                        flag &= ~STATUS_CARET;
                    }

                    if (width) {
                        while (*s && bit_isset(&bit, *s) && width-- > 0) {
                            s++;
                        }
                    } else {
                        while (*s && bit_isset(&bit, *s)) {
                            s++;
                        }
                    }

                    if (*fmt == ']')
                        fmt++;
                    break;

                case 'f':

                    if (width) {
                        while (*s && isdigit(*s) && width > 0)
                            s++, width--;

                        if (*s == '.' && width > 0)
                            s++, width--;

                        while (*s && isdigit(*s) && width > 0)
                            s++, width--;
                    } else {
                        while (*s && isdigit(*s))
                            s++;

                        if (*s == '.')
                            s++;

                        while (*s && isdigit(*s))
                            s++;
                    }

                    fmt++;
                    break;

                case '{':
                    fmt++;

                    flag &= ~STATUS_OR;
                    while(*fmt && *fmt != '|' && *fmt != '}') {
                        s0 = s;

                        /* match */
                        while (*fmt && *fmt != '|' && *fmt != '}' && (*s0 == *fmt))
                            s0++, fmt++;

                        /* match ok*/
                        if (*fmt == '|' || *fmt == '}') {
                            flag |= STATUS_OR;
                            while (*fmt && *fmt != '}')
                                fmt++;
                            s = s0;
                        } else {
                            /* match no*/
                            while (*fmt && *fmt != '|' && *fmt != '}')
                                fmt++;
                            if (*fmt == '|' || *fmt == '}')
                                fmt++;
                        }
                    }

                    if (!(flag & STATUS_OR))
                        return 0;

                    if (*fmt == '}')
                        fmt++;
                    break;

                default:
                    fmt++;
                    break;
            }

            width = 0;
        } else {

            /*printf("%d:%d:%c:%c\n", *fmt, *s, *fmt, *s);*/
            if (!*fmt && !*s)
                return 1;

            if (*fmt != *s) {
                /* %?*/
                if (*fmt && fmt[1] == '%' && fmt[2] == '?') {
                    fmt++;
                    continue;
                }
                else
                    return 0;
            }

            fmt++;
            s++;
        }
    }

    return !*fmt && !*s;
}