1. 程式人生 > >c語言之傅立葉變化

c語言之傅立葉變化

#include <stdio.h>

void StartTimer0(void);

void Interrupt_Init(void);
void Interrupt_Start(void);
#if 0
void main (void)
{
	printf("Start Timer\n");
	StartTimer0();//設定定時器暫存器
	
//	Interrupt_Init();
//	Interrupt_Start();

	printf("while(1)\n");
//	while(1);
}
#endif



#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//精度0.0001弧度

typedef struct//複數型別
{
    float real;       //實部
    float imag;       //虛部
}complex;

#define PI 3.1415926


///////////////////////////////////////////
void conjugate_complex(int n,complex in[],complex out[]);
void c_plus(complex a,complex b,complex *c);//複數加
void c_mul(complex a,complex b,complex *c) ;//複數乘
void c_sub(complex a,complex b,complex *c); //複數減法
void c_div(complex a,complex b,complex *c); //複數除法
void fft(int N,complex f[]);//傅立葉變換 輸出也存在陣列f中
void c_abs(complex f[],float out[],int n);//複數陣列取模

void conjugate_complex(int n,complex in[],complex out[])
{
    int i = 0;
    for(i=0;i<n;i++)
    {
        out[i].imag = -in[i].imag;
        out[i].real = in[i].real;
    }
}

void c_abs(complex f[],float out[],int n)
{
    int i = 0;
    float t;
    for(i=0;i<n;i++)
    {
        t = f[i].real * f[i].real + f[i].imag * f[i].imag;
        out[i] = sqrt(t);
    }
}


void c_plus(complex a,complex b,complex *c)
{
    c->real = a.real + b.real;
    c->imag = a.imag + b.imag;
}

void c_sub(complex a,complex b,complex *c)
{
    c->real = a.real - b.real;
    c->imag = a.imag - b.imag;
}

void c_mul(complex a,complex b,complex *c)
{
    c->real = a.real * b.real - a.imag * b.imag;
    c->imag = a.real * b.imag + a.imag * b.real;
}

void c_div(complex a,complex b,complex *c)
{
    c->real = (a.real * b.real + a.imag * b.imag)/(b.real * b.real +b.imag * b.imag);
    c->imag = (a.imag * b.real - a.real * b.imag)/(b.real * b.real +b.imag * b.imag);
}

#define SWAP(a,b)  tempr=(a);(a)=(b);(b)=tempr

void Wn_i(int n,int i,complex *Wn,char flag)
{
    Wn->real = cos(2*PI*i/n);
    if(flag == 1)
        Wn->imag = -sin(2*PI*i/n);
    else if(flag == 0)
        Wn->imag = -sin(2*PI*i/n);
}

//傅立葉變化
void fft(int N,complex f[])
{
    complex t,wn;//中間變數
    int i,j,k,m,n,l,r,M;
    int la,lb,lc;
    /*----計算分解的級數M=log2(N)----*/
    for(i=N,M=1;(i=i/2)!=1;M++);
    /*----按照倒位序重新排列原訊號----*/
    for(i=1,j=N/2;i<=N-2;i++)
    {
        if(i<j)
        {
            t=f[j];
            f[j]=f[i];
            f[i]=t;
        }
        k=N/2;
        while(k<=j)
        {
            j=j-k;
            k=k/2;
        }
        j=j+k;
    }

    /*----FFT演算法----*/
    for(m=1;m<=M;m++)
    {
        la=pow(2,m); //la=2^m代表第m級每個分組所含節點數
        lb=la/2;    //lb代表第m級每個分組所含碟形單元數
        //同時它也表示每個碟形單元上下節點之間的距離
        /*----碟形運算----*/
        for(l=1;l<=lb;l++)
        {
            r=(l-1)*pow(2,M-m);
            for(n=l-1;n<N-1;n=n+la) //遍歷每個分組,分組總數為N/la
            {
                lc=n+lb;  //n,lc分別代表一個碟形單元的上、下節點編號
                Wn_i(N,r,&wn,1);//wn=Wnr
                c_mul(f[lc],wn,&t);//t = f[lc] * wn複數運算
                c_sub(f[n],t,&(f[lc]));//f[lc] = f[n] - f[lc] * Wnr
                c_plus(f[n],t,&(f[n]));//f[n] = f[n] + f[lc] * Wnr
            }
        }
    }
}


#define NUM 512
int main()
{
    int N,i;

    unsigned short amp[NUM];
	complex input[NUM];

    int fs = 44100;
	N = 512;

    for(i=0;i<N;i++)
    {
        input[i].real = 7000*cos(1000*2*(double)PI*((double)i/44100) + (double)3/5*PI)
                + 200*cos(600*2*(double)PI*((double)i/44100) + (double)1/2*PI);
        input[i].imag = 0;
    }

    fft(N,input);

    printf("得到的頻譜值為:\n");
    for(i=0;i<N/2;i++)
    {
        double originamp = sqrt(input[i].real*input[i].real + input[i].imag*input[i].imag);
        if(i == 0)
        {
            amp[i] = originamp/N;
        }
        else
        {
            amp[i] = originamp/N*2;
        }
        if(amp[i] > 20)
        {
            printf("f:%.2lf amp:%d\n",(double)i*fs/N,amp[i]);
        }
    }
    return 0;
}