the C language of FFT
A good C language implementation. It has to be done!
Theory Introduction:
http://blog.csdn.net/cinmyheart/article/details/39052739
Here are the previous implementations of Matlab & Octave
http://blog.csdn.net/cinmyheart/article/details/39042623
First introduce the overall implementation of the file
The most basic is the fft.c this file is the core of the algorithm implementation
Fft.h
/***************************************************************code Writer:eofcode File:fft.hcode Date : 2014.09.17e-mail:[email protected]****************************************************************/# ifndef _fft_in_c_h#define _fft_in_c_h#include <stdio.h> #include <stdlib.h> #include <math.h># Define PI 3.1415926#define debugstruct complex_number{double real;double imagine;}; struct Signal{int size;//how Many points in this domain.struct complex_number points[0];}; int reverse_bits (int num,int bits), int get_r_in_wn (int k, int m, int bits), void init_signal (struct signal* p_signal,double * Array,int size), struct signal* FFT (struct signal* p_signal), struct complex_number complex_mul (struct complex_number* x , struct complex_number* y), struct complex_number complex_add (struct complex_number* x, struct complex_number *y); struct Complex_number complex_sub (struct complex_number* x, struct complex_number *y); void show_signal (struct signal* const signal); void Show_complex_number (struct complex_number * x); #endif
Complex_add.c
/***************************************************************code Writer:eofcode File:complex_add.ccode Date : 2014.09.17e-mail:[email protected]code purpose:we need add operation between complex number, so this is my method. If you find something wrong with my code, please touchme by e-mail. Thank you:) ****************************************************************/#include "fft.h" #include "fft.h" struct Complex_number complex_add (struct complex_number* x, struct complex_number *y) {struct Complex_number ret;if (!x | | !y) {printf ("passed NULL into%s () \ n", __function__); goto out;} Ret.real = x->real + y->real;ret.imagine = x->imagine + y->imagine;out:return ret;}
Complex_mul.c
/***************************************************************code Writer:eofcode File:complex_mul.ccode Date : 2014.09.17e-mail:[email protected]code Purpose:we need multiple (*) operation between complex number, so this is my method . If you find something wrong with my code, please touchme by e-mail. Thank you:) ****************************************************************/#include "fft.h" struct complex_number Complex_mul (struct complex_number* x,struct complex_number *y) {struct Complex_number ret;if (!x | |!y) {printf ("you Passed NULL into%s () \ n ", __function__); goto out;} Ret.real = (x->real) * (y->real) -(x->imagine) * (y->imagine); ret.imagine = (x->real) * (y-> Imagine) + (x->imagine) * (y->real); out:return ret;}
Complex_sub.c
#include "fft.h" struct complex_number complex_sub (struct complex_number* x, struct complex_number *y) {struct COMPLEX_ Number Ret;if (!x | |!y) {printf ("You passed NULL into%s () \ n", __function__); goto out;} Ret.real = x->real -y->real;ret.imagine = X->imagine-y->imagine;out:return ret;}
Get_r_in_wn.c
/******************************************************************************code Writer:EOFcode file:get_r_in _wn.ccode date:2014.09.17e-mail:[email protected]input Parameter: @k, the location of the Input signal @m, the current LA Yyer @N, the total number of inputed signal @bits, how many bits should is used to represent ' N ' Output Parameter: @ret, the value of ' R ' *******************************************************************************/int get_r_in_ Wn (int k, int m, int bits) {int tmp = k<< (BITS-M); return tmp& ((1<<m)-1);}
Reverse_bits.c
/***************************************************************code Writer:eofcode File:reverse_bits.ccode Date : 2014.09.17e-mail:[email Protected]code Purpose:reverse The bits of input numberif you find something wrong with my Code, Touchme by e-mail. Thank you:) ****************************************************************/int reverse_bits (int num,int bits) {int RET = 0;int Copy_num = 0;for (ret = 0,copy_num = num; bits > 0; bits--) {ret + = (copy_num% 2) * (1<< (bits- 1)); Copy_num >>= 1;} return ret;}
Show_complex_number.c
/***************************************************************code Writer:eofcode File:show_complex_ Number.ccode Date:2014.09.17e-mail:[email protected]if You find something wrong with my code, please touchme by e-mail. Thank you:) ****************************************************************/#include "fft.h" void show_complex_ Number (struct Complex_number * x) {printf ("Real:%lf imagine:%lf\n", x->real,x->imagine);}
Show_signal.c
/***************************************************************code Writer:eofcode File:show_signal.ccode Date : 2014.09.17e-mail:[email Protected]code purpose:if you want to see the detail about signal this @p_signal point to, just C All the This API. If you find something wrong with my code, please touchme by e-mail. Thank you:) ****************************************************************/#include "fft.h" void show_signal ( struct signal* const p_signal) {if (!p_signal) {printf ("You passed NULL into function%s line:%d\n", __function__,__ line__); return;} int tmp = 0;FOR (tmp = 0; tmp < p_signal->size;tmp++) {printf ("Point%d real:%lf imagine%lf\n", Tmp,p_signal->poi Nts[tmp].real,p_signal->points[tmp].imagine);} printf ("\ n");}
Fft.c
/***************************************************************code Writer:eofcode File:fft.ccode Date : 2014.09.17e-mail:[email protected]code purpose:this code Core-part of FFT in my implementation. If you find something wrong with my code, please touchme by e-mail. Thank you:) ****************************************************************/#include "fft.h" struct signal* FFT ( struct signal* p_signal) {struct signal* p_input_signal = (struct signal*) malloc (sizeof (struct complex_number) * (p_ signal->size) + sizeof (p_signal->size)), struct signal* p_out_put_signal = (struct signal*) malloc (sizeof (struct Complex_number) * (p_signal->size) + sizeof (p_signal->size)) *p_input_signal = *p_signal;*p_out_put_signal = *p_ Signal;int tmp = 0;int index = 0;int bits = 0;int layyer= 0;int selected_point = 0;int Pre_half = 0;int r = 0;dou ble x = 0;struct complex_number w_rn; struct complex_number complex_tmp;/***we caculate How many bits should is used to * * represent the SizE of the number of input signal.*/for (tmp = p_signal->size-1;tmp > 0;tmp>>=1) {bits++;} /***we should re-sequence the input signal** by bit-reverse.*/for (tmp = 0;tmp < p_signal->size;tmp++) {index = revers E_bits (tmp,bits);p _input_signal->points[tmp] = p_signal->points[index]; #ifdef debugprintf ("tmp:%5d index:%5d ", Tmp,index); Show_complex_number (&p_signal->points[index]); #endif}for (Layyer = 1;layyer <= bits;layyer++ {#ifdef debugprintf ("Layyer%d input\n", layyer); show_signal (p_input_signal); #endiffor (Selected_point = 0;selected_ Point < P_signal->size;selected_point + = 1<< (Layyer)) {for (pre_half = selected_point,r = 0,x = 0; Pre_half < (Selected_point + (1<< (layyer-1))); pre_half++) {r = get_r_in_wn (pre_half,layyer,bits), #ifdef debugprintf ("R:%d\n", r), #endifx = -2*pi*r/((double) (p_ Input_signal->size)); w_rn.real = cos (x); W_rn.imagine = sin (x); complex_tmp = Complex_mul (&w_rn, & (P_input_signal->points[pre_half + (1<< (LAYYER-1)]); #ifdef Debugshow_complex_number (&complex_tmp); #endifp_out_put_signal->points[pre_ Half] = Complex_add (&p_input_signal->points[pre_half],&complex_tmp);p _out_put_signal->points[pre_ Half + (1<< (layyer-1))] = Complex_sub (&p_input_signal->points[pre_half],&complex_tmp);}} #ifdef debugprintf ("layyer%d output\n", Layyer); show_signal (p_out_put_signal); #endiffor (tmp = 0;tmp < P_out_put_ signal->size;tmp++) {p_input_signal->points[tmp] = p_out_put_signal->points[tmp];}} Free (p_input_signal); return p_out_put_signal;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
The C language of FFT