初级会员

- 积分
- 68
- 金钱
- 68
- 注册时间
- 2019-8-8
- 在线时间
- 27 小时
|

楼主 |
发表于 2019-12-25 16:38:34
|
显示全部楼层
这个是求相位部分的代码
- #define pi 3.1415926f
- #define FFT_LENGTH 4096
- float fft_inputbuf[FFT_LENGTH*2]; //FFT输入数组 计算采样电阻前的电压
- float fft_outputbuf[FFT_LENGTH]; //FFT输出数组
- float FFT_ADC(u8 ch,u8 mode)
- {
- float ADC_temp;
- float max;
- int max2;
- float v_ac;
- float v_dc;
- float phase;//相位
- double real,im;
- float hudu;
- u16 i;
- while(!adc_flag)
- {
- }
- TIM_Cmd(TIM3,ENABLE);
- adc_flag=0;
- max=0;
- for(i=0;i<FFT_LENGTH;i++)
- {
- ADC_temp=(float)ADC1data[ch][i]*(3.3f/4095.0f);
- fft_inputbuf[2*i]=ADC_temp;//高位放置实数 1010 0000
- fft_inputbuf[2*i+1]=0;//低位放置虚数
- }
- arm_cfft_f32(&arm_cfft_sR_f32_len4096,fft_inputbuf,0,1);
- arm_cmplx_mag_f32(fft_inputbuf,fft_outputbuf,FFT_LENGTH); //FFT计算(基4)
- for(i=0;i<(FFT_LENGTH/2+1);i++)
- {
- if(max < fft_outputbuf[i+2])
- {
- max = fft_outputbuf[i+2]; //找fftout的最大值
- max2=i+2;
- }
- }
- if(mode==1)
- {
-
- v_ac = (max/FFT_LENGTH)*2.308834f;
- return v_ac;
- }
- else if(mode==2)
- {
- v_dc = (fft_outputbuf[0]/FFT_LENGTH);
- return v_dc;
- }
- else
- {
- real=(double)fft_inputbuf[2*max2];
- im=(double)fft_inputbuf[2*max2+1];
- hudu=atan2(im,real);
- phase=hudu*180.0f/pi;
- if(phase<0)
- phase+=360.0f;
- // phase= atan2((double)fft_inputbuf[2*max2+1],(double)fft_inputbuf[2*max2])*180.0/pi;
- return phase;
- }
- }
复制代码
|
|