⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 512点FFT在Microchip dsPIC33FJ256GP710 上的实现
💻 C
字号:
#include "p33fj256gp710.h"
#include "dsp.h"
#include "fft.h"


/* Extern definitions */
extern fractcomplex sigCmpx[FFT_BLOCK_LENGTH] __attribute__((section("ymemory"),aligned(FFT_BLOCK_LENGTH*2*2)));
extern const fractcomplex twiddleFactors[FFT_BLOCK_LENGTH/2] __attribute__((space(auto_psv),aligned(FFT_BLOCK_LENGTH*2*2)));

/* Declare post-FFT variables to compute the frequency of the largest spectral component */
int	peakFrequencyBin = 0;			
unsigned long peakFrequency = 0;			

fractional FFTMagnitude[512];
fractional FFTReal[512];
fractional FFTImag[512];
int main(void)
{
	int i = 0;
	fractional *p_real = &sigCmpx[0].real ;
	fractcomplex *p_cmpx = &sigCmpx[0] ;

	for ( i = 0; i < FFT_BLOCK_LENGTH; i++ )/* The FFT function requires input data */
	{					                    /* to be in the fractional fixed-point range [-0.5, +0.5]*/
		*p_real = *p_real >>1 ;		        /* So, we shift all data samples by 1 bit to the right. */
		*p_real++;			                /* Should you desire to optimize this process, perform */
	}					                    /* data scaling when first obtaining the time samples */
						                    /* Or within the BitReverseComplex function source code */
	Nop();
	p_real = &sigCmpx[(FFT_BLOCK_LENGTH/2)-1].real;	/* Set up pointers to convert real array */
	p_cmpx = &sigCmpx[FFT_BLOCK_LENGTH-1] ;         /* to a complex array. The input array initially has all */
						                            /* the real input samples followed by a series of zeros */
    Nop();
	for ( i = FFT_BLOCK_LENGTH; i > 0; i-- ) /* Convert the Real input sample array */
	{					                     /* to a Complex input sample array  */
		(*p_cmpx).real = (*p_real--);	     /* We will simpy zero out the imaginary  */
		(*p_cmpx--).imag = 0x0000;	         /* part of each data sample */
	}
	Nop();

	/* Perform FFT operation */
	FFTComplexIP (LOG2_BLOCK_LENGTH, \
				  &sigCmpx[0],\
				  (fractcomplex*)__builtin_psvoffset(&twiddleFactors[0]),\
				  (int)__builtin_psvpage(&twiddleFactors[0]));
	Nop();
	/* Store output samples in bit-reversed order of their addresses */
	BitReverseComplex (LOG2_BLOCK_LENGTH, &sigCmpx[0]);

	//提取FFT实部和虚部
	for(i=0;i<512;i++)
	{
		FFTReal[i] = sigCmpx[i].real;
		FFTImag[i] = sigCmpx[i].imag;
	}
	
	Nop();
	/* Compute the square magnitude of the complex FFT output array so we have a Real output vetor */
	SquareMagnitudeCplx(FFT_BLOCK_LENGTH, &sigCmpx[0], &sigCmpx[0].real);

	Nop();
	//FFTMagnitude数组据就是傅立叶变换后的幅度值
	p_real=&sigCmpx[0].real;
	for(i=0;i<512;i++)
	{
		FFTMagnitude[i]=*p_real;
		p_real++;
	}
	Nop();
	/* Find the frequency Bin ( = index into the SigCmpx[] array) that has the largest energy*/
	/* i.e., the largest spectral component */
	VectorMax(FFT_BLOCK_LENGTH/2, &sigCmpx[0].real, &peakFrequencyBin);
	Nop();
	/* Compute the frequency (in Hz) of the largest spectral component */
	peakFrequency = peakFrequencyBin * (SAMPLING_RATE/FFT_BLOCK_LENGTH);

    while (1);	/* Place a breakpoint here and observe the watch window variables */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -