FFT 的C 语言
FFT 的C 语言
说好的C 语言实现。必须搞定它!
理论介绍:
http://blog.csdn.net/cinmyheart/article/details/39052739
这里有之前matlab & Octave 的实现
http://blog.csdn.net/cinmyheart/article/details/39042623
先介绍一下整体的实现文件
最基本的是fft.c这个文件是算法实现的核心
fft.h
/***************************************************************
code writer : EOF
code file : fft.h
code date : 2014.09.17
e-mail : jasonleaster@gmail.com ****************************************************************/
#ifndef _FFT_IN_C_H
#define _FFT_IN_C_H #include <stdio.h>
#include <stdlib.h>
#include <math.h> #define PI 3.1415926
#define DEBUG struct 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 : EOF
code file : complex_add.c
code date : 2014.09.17
e-mail : jasonleaster@gmail.com code purpose :
We need add operation between complex number, so here is
my method. If you find something wrong with my code, please touch
me 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("You 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 : EOF
code file : complex_mul.c
code date : 2014.09.17
e-mail : jasonleaster@gmail.com code purpose :
We need multiple(*) operation between complex number, so here is
my method. If you find something wrong with my code, please touch
me 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 : EOF
code file : get_r_in_Wn.c
code date : 2014.09.17
e-mail : jasonleaster@gmail.com Input Parameter : @k, the location of input signal
@m, the current layyer
@N, the total number of inputed signal
@bits, how many bits should be 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 : EOF
code file : reverse_bits.c
code date : 2014.09.17
e-mail : jasonleaster@gmail.com code purpose : Reverse the bits of input number If you find something wrong with my code, please touch
me 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 : EOF
code file : show_complex_number.c
code date : 2014.09.17
e-mail : jasonleaster@gmail.com If you find something wrong with my code, please touch
me 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 : EOF
code file : show_signal.c
code date : 2014.09.17
e-mail : jasonleaster@gmail.com code purpose :
If you want to see the detail about signal that @p_signal point to, just call this API. If you find something wrong with my code, please touch
me 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->points[tmp].real,\
p_signal->points[tmp].imagine);
}
printf("\n\n");
}
fft.c
/***************************************************************
code writer : EOF
code file : fft.c
code date : 2014.09.17
e-mail : jasonleaster@gmail.com code purpose : This code core-part of fft in my implementation.
If you find something wrong with my code, please touch
me 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;
double x = 0;
struct complex_number W_rN ;
struct complex_number complex_tmp ; /*
** We caculate how many bits should be 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 = reverse_bits(tmp,bits);
p_input_signal->points[tmp] = p_signal->points[index];
#ifdef DEBUG
printf(" tmp:%5d index:%5d ",tmp,index);
show_complex_number(&p_signal->points[index]);
#endif
} for(layyer = 1;layyer <= bits;layyer++)
{ #ifdef DEBUG
printf("layyer %d input\n",layyer);
show_signal(p_input_signal);
#endif for(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 DEBUG
printf("r: %d\n",r);
#endif x = -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 DEBUG
show_complex_number(&complex_tmp);
#endif p_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 DEBUG
printf("layyer%d output\n",layyer);
show_signal(p_out_put_signal);
#endif for(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;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
FFT 的C 语言的更多相关文章
- 关于一个通俗易懂的FFT的C语言实现教程
找到一个通俗易懂并且神奇并且有趣的FFT算法C语言实现教程:http://www.katjaas.nl/FFTimplement/FFTimplement.html 只要对矩阵比较熟悉就能在教程的辅助 ...
- FFT算法的完整DSP实现(转)
源:FFT算法的完整DSP实现 傅里叶变换或者FFT的理论参考: [1] http://www.dspguide.com/ch12/2.htm The Scientist and Engineer's ...
- Other-Website-Contents.md
title: 本站目录 categories: Other sticky: 10 toc: true keywords: 机器学习基础 深度学习基础 人工智能数学知识 机器学习入门 date: 999 ...
- 灰度图像--图像分割 阈值处理之OTSU阈值
学习DIP第55天 转载请标明本文出处:***http://blog.csdn.net/tonyshengtan ***,出于尊重文章作者的劳动,转载请标明出处!文章代码已托管,欢迎共同开发:http ...
- 用于ARM上的FFT与IFFT源代码(C语言,不依赖特定平台)(转)
源:用于ARM上的FFT与IFFT源代码(C语言,不依赖特定平台) 代码在2011年全国电子大赛结束后(2011年9月3日)发布,多个版本,注释详细. /*********************** ...
- FFT算法理解与c语言的实现
完整内容迁移至 http://www.face2ai.com/DIP-2-3-FFT算法理解与c语言的实现/ http://www.tony4ai.com/DIP-2-3-FFT算法理解与c语言的实现 ...
- 用于ARM上的FFT与IFFT源代码-C语言
/********************************************************************************* 程序名称:快速傅里叶变换(FFT) ...
- 【转】用C语言实现FFT算法
傅里叶变换 快速傅里叶变换(Fast Fourier Transform,FFT)是一种可在 时间内完成的离散傅里叶变换(Discrete Fourier transform,DFT)算法. 在算法 ...
- 二维FFT,IFFT,c语言实现
学习DIP第6天 完整内容迁移至http://www.face2ai.com/DIP-2-4-二维FFT-IFFT-c语言实现/ http://www.tony4ai.com/DIP-2-4-二维FF ...
随机推荐
- ASP.NET MVC4实现URL伪静态
1.在Web.config添加节点配置: <system.webServer> <modules runAllManagedModulesForAllRequests="t ...
- java通过抛异常来返回提示信息
结论:如果把通过抛异常的方式得到提示信息,可以使用java.lang.Throwable中的构造函数: public Throwable(String message) { fillInStackTr ...
- 批量创建采购订单 BAPI_PO_CREATE1
*&---------------------------------------------------------------------* *& REPORT ZMM_PO_C ...
- Java多线程中wait, notify and notifyAll的使用
本文为翻译文章,原文地址:http://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example 在Java的Obje ...
- 24L01/SI24R1调试笔记
1.SPI MSB优先,8Bit寄存器地址与内容: 2.寄存器结构与之前使用的LT8900不同,分为R.W寄存器与特殊功能寄存器: 3.特别注意:在TX.RX.RT中断或者轮询后置1,必须写1清零与清 ...
- NSIS:强制结束软件进程
原文NSIS:强制结束软件进程 有时候,我们选择卸载软件后发现安装目录中的主文件依然存在,不是我们卸载代码写的不对,而是卸载的时候软件根本就没有关闭! 在卸载前加上下面这个宏可以在一定程度上免除上述的 ...
- 【安德鲁斯】于java代码集drawableLeft给予适当的大小如何,当?
textView.setCompoundDrawables(drawable, null, null, null);如果看不到图片,这是由于需要手动定drawable适当的大小,使用drawable. ...
- 使用批处理文件命令行方式快速启动和停止IIS、SqlServer
原文:使用批处理文件命令行方式快速启动和停止IIS.SqlServer 虽然现在内存便宜了,但是自己还是嫌自己的512M内存太小,没办法,后台运行的东西太多了,有很多都是有用的没法关闭的.IIS和SQ ...
- 华为-on练习--重复的字符过滤
称号: 请写一个字符串过滤程序,如果使用多个相同的字符出现在字符串中,字符首次出现在非过滤,. 比方字符串"abacacde"过滤结果为"abcde". 演示样 ...
- POJ 1743 Musical Theme Hash+二分法
标题效果:有一个美丽的旋律,它们是由一些不大于88音调.如果计为五个音调的量度,问:是否有相同的节奏的多个部分(相同的差,以及两者之间的相同的节奏不能重叠),并寻求最长长度. 思考:这个问题是八人中的 ...