Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. #if defined (__USE_LPCOPEN)
  2. #if defined(NO_BOARD_LIB)
  3. #include "chip.h"
  4. #else
  5. #include "board.h"
  6. #endif
  7. #endif
  8. #include <cr_section_macros.h>
  9. #include <math.h>
  10. #include "arm_math.h"
  11. #include "oled.h"
  12. #define _LPC_ADC_ID LPC_ADC
  13. #define FS 32000
  14. #define FFT_N 256
  15. #define FFT_2N (FFT_N*2)
  16. #define FFT_N2 (FFT_N/2)
  17. arm_cfft_radix4_instance_f32 fftInstance;
  18. float32_t x_buff[FFT_2N]={0};
  19. float32_t x_buff_point[FFT_2N]={0};
  20. float32_t win[FFT_N];
  21. float32_t in_buff[FFT_N];
  22. uint32_t wr=0;
  23. uint16_t dataADC;
  24. volatile bool f1=false;
  25.  
  26. void SysTick_Handler(void)
  27. {
  28. for(int i=0; i<FFT_2N; i++){
  29. if(x_buff_point[i]>0){
  30. x_buff_point[i]--;
  31. }
  32. }
  33. }
  34.  
  35. /*--------------------------------------------------------------------------------*/
  36. void Spectrum_Line_f32(float32_t *pSrc, uint32_t numSamples)
  37. {
  38. uint16_t y=0, y1=63;
  39. for(int i=1; i<numSamples; i++){
  40. y=63-pSrc[i];
  41. OLED_Draw_Line(i-1, y1,i, y);
  42. y1=y;
  43. }
  44. }
  45. /*--------------------------------------------------------------------------------*/
  46. void Spectrum_Log_Line_f32(float32_t *pSrc, uint32_t numSamples)
  47. {
  48. uint16_t y=0, y1=63;
  49. for(int i=1; i<numSamples; i++){
  50. y=40*log10(1/((pSrc[i]/(float32_t)FFT_N2)+0.026)); // -32 dB
  51. OLED_Draw_Line(i-1, y1,i, y);
  52. y1=y;
  53. }
  54. }
  55. /*--------------------------------------------------------------------------------*/
  56.  
  57. void Spectrum_Bar_f32(float32_t *pSrc, uint32_t numSamples, bool peak)
  58. {
  59. for(int i=1; i<numSamples; i=i+4){
  60. uint16_t y=pSrc[i]; // -32 dB
  61. OLED_Draw_Line(i, 0,i, y);
  62. if(pSrc[i]>x_buff_point[i]){
  63. x_buff_point[i]=pSrc[i];
  64. }
  65.  
  66. OLED_Draw_Point(i,1+x_buff_point[i],1);
  67.  
  68. }
  69.  
  70. }
  71. /*--------------------------------------------------------------------------------*/
  72. void ADC_IRQHandler(void){
  73. Chip_ADC_ReadValue(_LPC_ADC_ID, ADC_CH0, &dataADC);
  74. if(f1==false){
  75. in_buff[wr++]=(dataADC-2048)/2048.0;
  76. if(wr==FFT_N){
  77. f1=true;
  78. wr=0;
  79. }
  80. }
  81. }
  82.  
  83.  
  84. /*--------------------------------------------------------------------------------*/
  85. int main(void) {
  86. arm_cfft_radix4_init_f32(&fftInstance, FFT_N, 0, 1);
  87. static ADC_CLOCK_SETUP_T ADCSetup;
  88. uint32_t timerFreq;
  89. #if defined (__USE_LPCOPEN)
  90. // Read clock settings and update SystemCoreClock variable
  91. SystemCoreClockUpdate();
  92. #if !defined(NO_BOARD_LIB)
  93. // Set up and initialize all required blocks and
  94. // functions related to the board hardware
  95. Board_Init();
  96. // Set the LED to the state of "On"
  97. Board_LED_Set(0, true);
  98. #endif
  99. #endif
  100. // TODO: insert code here
  101. /*--------------------- OLED Init -----------------------*/
  102. OLED_Init();
  103. SysTick_Config(SystemCoreClock / 64);
  104. /*--------------------- ADC Init ------------------------*/
  105. Chip_ADC_Init(_LPC_ADC_ID, &ADCSetup);
  106. Chip_ADC_Int_SetChannelCmd(_LPC_ADC_ID, ADC_CH0, ENABLE);
  107. Chip_ADC_EnableChannel(_LPC_ADC_ID, ADC_CH0, ENABLE);
  108. NVIC_ClearPendingIRQ(ADC_IRQn);
  109. NVIC_EnableIRQ(ADC_IRQn);
  110. Chip_ADC_SetStartMode(_LPC_ADC_ID, ADC_START_ON_ADCTRIG0, ADC_TRIGGERMODE_RISING);
  111. /*-------------------- Timer32_0 Init --------------------*/
  112. Chip_TIMER_Init(LPC_TIMER32_0);
  113. timerFreq = Chip_Clock_GetSystemClockRate();
  114. Chip_TIMER_Reset(LPC_TIMER32_0);
  115. Chip_TIMER_MatchEnableInt(LPC_TIMER32_0, 0);
  116. Chip_TIMER_SetMatch(LPC_TIMER32_0, 0, (timerFreq / (2*FS)));
  117. Chip_TIMER_ResetOnMatchEnable(LPC_TIMER32_0, 0);
  118. Chip_TIMER_ExtMatchControlSet(LPC_TIMER32_0, 0, TIMER_EXTMATCH_TOGGLE, 0);
  119. Chip_TIMER_Enable(LPC_TIMER32_0);
  120. /*-------------------- End of Init ----------------------*/
  121. // Time Window
  122. for(int n=0; n<FFT_N;n++){
  123. //win[n]=1; // Rect
  124. win[n]=(0.5*(1-arm_cos_f32(2*PI*n/(FFT_N-1)))); // Hann
  125. }
  126. while(1) {
  127. if(f1){
  128. for(int i=0; i<FFT_N;i++){
  129. x_buff[i*2]=win[i]*in_buff[i];
  130. x_buff[i*2+1]=0;
  131. }
  132. arm_cfft_radix4_f32(&fftInstance, x_buff);
  133. arm_cmplx_mag_f32(x_buff, x_buff, FFT_N);
  134. OLED_Clear_Screen(0);
  135. OLED_Puts(0, 0 ,"Real-Time FFT Radix-4");
  136. for(int i=0; i<FFT_N;i++)
  137. x_buff[i]*=(0.25*i);// emphase
  138. //Spectrum_Log_Line_f32(x_buff, FFT_N2);
  139. Spectrum_Bar_f32(x_buff, FFT_N2, 1);
  140. OLED_Refresh_Gram();
  141. f1=false;
  142. }
  143. }
  144. return 0 ;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement