创建一个自定义的头文件 [英] creating a customized header file

查看:83
本文介绍了创建一个自定义的头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个用于快速傅立叶变换的程序,其中用户提到了样本数.输入是从.csv文件中提取的,输出是写入另一个.csv文件中的. fft函数,原型为fft(Fs,N,x []),其中Fs是采样频率,N是采样数,x []是输入采样数组.fft的代码如下:

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 :

#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;
	}

推荐答案

使用您选择的文本编辑器,打开一个新文档并输入内容:

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


然后使用扩展名".h"(例如,以"fft.h")保存文件.


Then save the file using the extension ''.h'' (e.g. as ''fft.h'').


这篇关于创建一个自定义的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆