广告

原创 STM32学习日志(23)----使用dsp库的FFT函数.rar

2011-4-19 14:57 4706 0 5 分类: MCU/ 嵌入式

attachment download


/**
  ******************************************************************************
  * @file FFT_Demo/src/main.c
  * @author  MCD Application Team
  * @version  V2.0.0
  * @date  04/27/2009
  * @brief  Main program body
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <math.h>
#include "stm3210b_lcd.h"
#include "stm32_dsp.h"
#include "table_fft.h"   
//------------------------------------------------------------------------------


#define FreqSample    3200  // 50Hz*64=3200
#define ScalingFactor   20000  // 幅值系数,最大为32767
#define PERCENT_DC    0.06  // 直流分量对应的百分比,对应直流分量lBUFMAG[0]=1200
#define PI2  6.28318530717959     // 2*3.14
#define NPT 64               /* 采样点数,必须为64NPT = No of FFT point*/
//------------------------------------------------------------------------------
extern volatile uint32_t TimingDelay ;
long lBUFIN[NPT];         /* Complex input vector */
long lBUFOUT[NPT];        /* Complex output vector */
long lBUFMAG[NPT + NPT/2];/* Magnitude vector */


/* Private function prototypes -----------------------------------------------*/
void MyDualSweep(uint32_t freqinc1,uint32_t freqinc2);
void MygSin(long nfill, long Fs, long Freq1, long Freq2, long Ampli);
void powerMag(long nfill, char* strPara);
void DSPDemoInit(void);
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int main(void)
{
  DSPDemoInit();


  while (1)
  {
  MyDualSweep(FreqSample/NPT,6*FreqSample/NPT);     // 测试fft
  }
}
//-----------------------------------------------------------------------------


/**
  * @brief  Produces a combination of two sinewaves as input signal
  * @param freq1: frequency increment for 1st sweep,频率步进值1
  *   Freq2: frequency increment for 2nd sweep,频率步进值2
  * @retval : None
  */
void MyDualSweep(uint32_t freqinc1,uint32_t freqinc2)
{
  uint32_t freq;
 
  //for (freq=FreqSample/NPT; freq <FreqSample/2; freq+=freqinc1)  // 40--4000,频率步进值为freqinc1
  for (freq=FreqSample/NPT; freq <FreqSample*6/NPT; freq+=freqinc1)  // 40--4000,频率步进值为freqinc1
  {
     MygSin(NPT, FreqSample, freq, 0, ScalingFactor);// 单独的频率,频率步进值2=0;
               
#if (NPT==64)
     cr4_fft_64_stm32(lBUFOUT, lBUFIN, NPT);
#elif (NPT==256)
     cr4_fft_256_stm32(lBUFOUT, lBUFIN, NPT);
#else
     cr4_fft_1024_stm32(lBUFOUT, lBUFIN, NPT);
#endif


     powerMag(NPT,"2SIDED");
  }


//-----------------------------------------------------------------------------


 for (freq=FreqSample/NPT; freq <FreqSample/2; freq+=freqinc2)
   {
     MygSin(NPT, FreqSample, freq, FreqSample*5/NPT, ScalingFactor);


#if (NPT==64)
     cr4_fft_64_stm32(lBUFOUT, lBUFIN, NPT);
#elif (NPT==256)
     cr4_fft_256_stm32(lBUFOUT, lBUFIN, NPT);
#else
     cr4_fft_1024_stm32(lBUFOUT, lBUFIN, NPT);
#endif
  
     powerMag(NPT,"2SIDED");
  }
}
//------------------------------------------------------------------------------
/**
  * @brief  Produces a combination of two sinewaves as input signal
  * @param ill: length of the array holding input signal
  *   Fs: sampling frequency
  *   Freq1: frequency of the 1st sinewave
  *   Freq2: frequency of the 2nd sinewave
  *   Ampli: scaling factor
  * @retval : None
  */
void MygSin(long nfill, long Fs, long Freq1, long Freq2, long Ampli)
{
  uint32_t i;
  float fFs, fFreq1, fFreq2, fAmpli;
  float fZ,fY;


  fFs = (float) Fs;
  fFreq1 = (float) Freq1;
  fFreq2 = (float) Freq2;
  fAmpli = (float) Ampli;


  for (i=0; i < nfill; i++)
  {
    fY =PERCENT_DC/2+sin(PI2 * i * (fFreq1/fFs)) + 0.2*sin(PI2 * i * (fFreq2/fFs));
    fZ = fAmpli * fY;
 
// lBUFIN数组中,每个单元数据高字(高16位)中存储采样数据的实部,低字(低16位)存储采样数据的虚部(总是为0)
    lBUFIN[i]= ((short)fZ) << 16 ;  /* sine_cosine  (cos=0x0) */
         // 高16bit为实部,低16bit为虚部,没有的话,就用0代替
  }
}
//------------------------------------------------------------------------------
/**
  * @brief  Removes the aliased part of the spectrum (not tested)
  * @param ill: length of the array holding power mag
  * @retval : None
  */
void onesided(long nfill)
{
  uint32_t i;
 
  lBUFMAG[0] = lBUFMAG[0];
  lBUFMAG[nfill/2] = lBUFMAG[nfill/2];
  for (i=1; i < nfill/2; i++)
  {
    lBUFMAG[i] = lBUFMAG[i] + lBUFMAG[nfill-i];
    lBUFMAG[nfill-i] = 0x0;
  }
}
//------------------------------------------------------------------------------
/**
  * @brief  Compute power magnitude of the FFT transform  进行FFT变换,并计算各次谐波幅值
  * @param ill: length of the array holding power mag
  *   : strPara: if set to "1SIDED", removes aliases part of spectrum (not tested)
  * @retval : None
  */
void powerMag(long nfill, char* strPara)
{
  static int32_t lX,lY;
  uint32_t i;


  for (i=0; i < nfill/2; i++)
  {
    lX= (lBUFOUT[i]<<16)>>16; /* 取低16bit,sine_cosine --> cos */
    lY= (lBUFOUT[i] >> 16);   /* 取高16bit,sine_cosine --> sin */   
    {
      float X=  NPT*((float)lX)/32768;
      float Y = NPT*((float)lY)/32768;
      float Mag = sqrt(X*X+ Y*Y)/nfill;
      lBUFMAG[i] = (uint32_t)(Mag*65536);
    }   
  }
}
//------------------------------------------------------------------------------


/**
  * @brief  Inserts a delay time.
  * @param ount: specifies the delay time length (time base 10 ms).
  * @retval : None
  */
void Delay(uint32_t nCount)
{
  /* Configure the systick */
  __enable_irq();
   
     /* Setup SysTick Timer for 10 msec interrupts */
  if (SysTick_Config(SystemFrequency /100)) /* SystemFrequency is defined in
   搒ystem_stm32f10x.h?and equal to HCLK frequency */
   {
    /* Capture error */
     while (1);
    }    
  /* Enable the SysTick Counter */
  TimingDelay = nCount;
   while (TimingDelay )  {}    
   TimingDelay=0;
   __disable_irq();


}


/**
  * @brief  Initializes the DSP lib demo (clock, peripherals and LCD).
  * @param  None
  * @retval : None
  */
void DSPDemoInit(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  ErrorStatus HSEStartUpStatus = ERROR;


  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();


  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);


  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();


  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);


    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);


    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);


    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);


    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);


    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);


    /* Enable PLL */
    RCC_PLLCmd(ENABLE);


    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }


    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);


    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }


  /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);


  /* TIM1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);



  /* SPI2 Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);


  /* TIM2  and TIM4 clocks enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM4, ENABLE);


  /*------------------- Resources Initialization -----------------------------*/
  /* GPIO Configuration */
  /* Configure PC.06 and PC.07 as Output push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);


 


  /*------------------- Drivers Initialization -------------------------------*/
  /* Initialize the LCD */
  STM3210B_LCD_Init();


  /* Clear the LCD */
  LCD_Clear(White);


  LCD_SetTextColor(White);
  LCD_SetBackColor(Black);


}


#ifdef  USE_FULL_ASSERT



/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval : None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */


  /* Infinite loop */
  while (1)
  {
  }
}
#endif


/**
  * @}
  */


 


/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/

广告

文章评论 5条评论)

登录后参与讨论

weiyuliang2006_444079975 2014-1-17 16:17

感谢分享

albertkao.tw_845212114 2013-3-9 04:33

学习一下!谢谢楼主分享!

tjniuqun_364700471 2012-12-21 11:35

感谢楼主

sdzhao2005 2012-6-29 16:34

谢谢楼主分享!

liang34567_883038316 2012-5-8 14:51

学习一下!谢谢楼主分享!
相关推荐阅读
sz_lihongtao 2011-04-20 21:32
32bit无符号数快速开平方根
//*******************************************************************************// 32bit无符号数开平方根// ...
sz_lihongtao 2011-04-20 12:12
STM32学习日志(24)----使用dsp库的FFT函数测相位
attachment download/**  ****************************************************************************...
sz_lihongtao 2011-04-19 10:39
stm32 dsp lib V2.0
attachment downloadattachment download...
sz_lihongtao 2011-04-19 10:37
STM32学习日志(22)----使用DMA功能自动更新PWM的输出
attachment download/*******************************************************************************编...
sz_lihongtao 2010-09-08 21:59
Labwindows/cvi8.5学习日志(56)----任意波形发生器
//******************************************************************************// Labwindows/cvi8.5...
我要评论
5
0
广告
关闭 热点推荐上一条 /2 下一条