Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have written a program for fast fourier transform where the user mentions the no of samples.the input is taken from a .csv file and the output is written to another .csv file.Now i have to make a header file for fft function with the prototype being fft(Fs,N,x[]) where Fs is the sampling frequency, N is the number of samples ans x[] is the input sample array.The code for fft is as follows :

C++
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define pi 3.14159265359
int flip_bit(int, int);
typedef struct;
{
	double real;
	double imag;
}complex;
complex comp_mult(complex , complex );

int main()
{
	int N , N1, i,j,n;
	int log_2_N;
	double p,k,Fs;
	char buf[100],*valstr;
	FILE *y_fp,*x_fp*z_fp;
    printf("\n enter sampling frequency : ");
    scanf("%g",&Fs);
	printf(" Length of the time domain signal : ");
	scanf("%d",&N);
	N1 = (int)pow(2,ceil(log((double)N)/log(2.0)));		
	double *fre = (double *)malloc(sizeof(double)*(2*N1));
		k=Fs/(2*N1);
	double *x = (double *)malloc(sizeof(double)*N1);					
												
	x_fp = fopen("auto_corr_input.txt","r");
	j=0;
	while(fgets(buf,sizeof(buf),x_fp)!=NULL && j<n)>
		valstr = strtok(buf,",");
		while(valstr!=NULL && j<n)>
		{
			x[j] = atof(valstr);
			
			j++;
			valstr = strtok(NULL," ,");
		}
	}
	printf("\n The Input Sequence is : ");
	for(i=0;i<n;i++)>
	  {
          printf(" %g,",x[i]);
      }
	
	

	
	for(i=N;i<n1;i++)>
	{
    	x[i]=0;
     }

	complex *X = (complex *)malloc(sizeof(complex)*N1);	
	double *temp = (double *)malloc(sizeof(double)*N1);
	complex *x_temp = (complex *)malloc(sizeof(complex)*N1);	
	N=N1;													
	log_2_N =lrint(log((double)N)/log(2.0));					
	complex W = {cos(-2*pi/N),sin(-2*pi/N)};		
	complex W1,newx;

	/*====================================== Main FFT Block =====================================================*/
	for(i=0;i<n;i++)>
	{
		x_temp[i].real = x[flip_bit(i,log_2_N)];					
		x_temp[i].imag = 0;
	}
	for(i=0;i<n;i++)>
		X[i] = x_temp[i];									
	
	for(n=1;n<=log_2_N;n++)						
	{
	for(i=0; i<n;>
	{
		for(j=0;j<pow(2.0,n-1);j++)>
		{
			if(j==0)									
			{
				x_temp[i+j].real = X[i+j].real + X[i+j+(int)pow(2.0,n-1)].real;
				x_temp[i+j].imag = X[i+j].imag + X[i+j+(int)pow(2.0,n-1)].imag;
				x_temp[i+j+(int)pow(2.0,n-1)].real = X[i+j].real - X[i+j+(int)pow(2.0,n-1)].real;
				x_temp[i+j+(int)pow(2.0,n-1)].imag = X[i+j].imag - X[i+j+(int)pow(2.0,n-1)].imag;
			}
			else
			{
				p=(N/2)/pow(2.0,n-1);
				W1.real = cos(-2*pi*j*p/N); W1.imag = sin(-2*pi*j*p/N);
				newx = comp_mult(W1 , X[i+j+(int)pow(2.0,n-1)]);
				x_temp[i+j].real = X[i+j].real + newx.real;
				x_temp[i+j].imag = X[i+j].imag + newx.imag;
				x_temp[i+j+(int)pow(2.0,n-1)].real = X[i+j].real - newx.real;
				x_temp[i+j+(int)pow(2.0,n-1)].imag = X[i+j].imag - newx.imag;
			}
		}
	}
	for(i=0;i<n;i++)>
	{
		X[i] = x_temp[i];					
	}
}
/*==================================== End of FFT Block ================================================================*/

	printf("\n");
	printf("\n FFT from Decimation in Time is : ");
	for(i=0;i<n;i++)>
	{
	printf("%g+i(%g), ",X[i].real,X[i].imag);
	
    }
    printf("\n\n");
    for(i=0;i<n;i++)>
	{
                    temp[i]=pow(X[i].real,2)+pow(X[i].imag,2);
                    temp[i]=sqrt(temp[i]);
	printf("%g,",temp[i]);

    
    
	y_fp = fopen("fft_results.csv","w");
  if (y_fp == NULL )
    {
        printf( "Cannot open fft_results.csv file in write mode" ) ;
    }
  else
  {   
      for (j=0;j      {
          fprintf(y_fp,"%g\n",temp[j];
      } 
          fprintf (y_fp,"\n");
  }
      
   
  fclose(y_fp);						
  for(i=0;i<(2*N);i++)
	{
	    fre[i]=2*i*k;
     }
      
  z_fp = fopen("frequency.csv","w");
  if (z_fp == NULL )
    {
        printf( "Cannot open frequency.csv file in write mode" ) ;
    }
  else
  {   
      for (j=0;j      {
          fprintf(z_fp,"%g\n",fre[j]);
      } 
          fprintf (z_fp,"\n");
  }
  
   
  fclose(z_fp);

	free(x);
	free(X);
	free(x_temp);
	free(temp);
	free(fre);
	fflush(stdin);
	char c = getchar(); 
	return 1;
}

	int flip_bit(int n, int limit)
	{
		int i, mask = 1, n1=0;
		for(i=0;i<limit;i++)>
		{
			if(n & (mask<<i))
				n1=n1|mask<<(limit-i-1);
		}
		return(n1);
	}

	complex comp_mult(complex a, complex b)
	{
		complex c;
		c.real = a.real*b.real-a.imag*b.imag;
		c.imag = a.imag*b.real + b.imag*a.real;
		return c;
	}
Posted
Updated 5-Jul-12 2:35am
v2
Comments
[no name] 5-Jul-12 7:48am    
And... so?
9668762186 5-Jul-12 7:50am    
so i dont know how to create a .h file...and googling didnt help me cause the answers i found were specific to a program.

1 solution

Use a text editor of your choice, open a new document and enter the content:

#ifndef FFT_H_INCLUDED
#define FFT_H_INCLUDED

double fft(double Fs, int N, double x[]);
#endif

Then save the file using the extension '.h' (e.g. as 'fft.h').
 
Share this answer
 
v2
Comments
9668762186 5-Jul-12 8:05am    
thank you. but shud i define the array as x[] or use a pointer there *x??
Jochen Arndt 5-Jul-12 8:07am    
You may use either. Using 'double *x' is more common. I just copied it from your question.
9668762186 5-Jul-12 8:07am    
and when i use the fft function in another program suppose i write X[i]=fft(10000,4000,x[i]).....X[i] will caontain what??if it contains the temp[i] values how will i retrive the fre[] values??
Jochen Arndt 5-Jul-12 8:16am    
You would use it in another module of a program. The function as declared (I changed the return value to double) may just return a calculated value. All depends on the implementation of the function and how it is called.
9668762186 5-Jul-12 8:17am    
so suppose i need the value of temp[] and fre[]...how do i call them??can u give mr the syntax??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900