How to get phase spectrum after do FFT Topic is solved

 Member++
 Posts: 26
 Joined: Tue Jul 02, 2019 8:38 am
How to get phase spectrum after do FFT
I use dsp_fft_bit_reverse_and_forward_real function to get the a sequence of imaginary and real numbers. Is there the function to get phase spectrum? So that I can do IFFT to get the data what I want if I do some operation in this sequence
View Solution

 Respected Member
 Posts: 335
 Joined: Wed May 31, 2017 6:55 pm
You can use dsp_math_atan2_hypot() to convert each complex number to polar form.

 Member++
 Posts: 26
 Joined: Tue Jul 02, 2019 8:38 am
Hi, How to do that. Actually, I don't know the algorithm very well. I find the way from internet to get the phase and want to recover signal by amplitude and phase, but when I input 1k Hz, there is 90 degree phase difference between input and output.
Code: Select all
dsp_fft_bit_reverse_and_forward_real(sample,512,dsp_sine_256,dsp_sine_512);
dsp_complex_t * local = (int32_t *)sample;
dsp_complex_window_hanning_post_fft_half(local,FEEDBACK_SAMPLE_MAX); //add window
for(int i = 0; i < BUFEER_SIZE; i++)
{
if(i==0)
{ p_state>fft_data0_re= local[i].re; p_state>fft_data0_im = local[i].im;} //store the local[0]
else
{
temp = dsp_math_multiply(local[i].re, local[i].re, 24)+ dsp_math_multiply(local[i].im, local[i].im, 24); // temp = sqrt(re^2 + im^2)
temp = dsp_math_sqrt(temp);
p_state>fftFrqArray[i]= temp;
p_state>angle[i] = dsp_math_atan(dsp_math_divide(local[i].im, local[i].re, 24)); // calculate the phase
}
}
for(int i =1; i< BUFEER_SIZE;i++)
{
local[i].re=dsp_math_multiply(p_state>fftFrqArray[i],dsp_math_cos(p_state>angle[i]),24); // re = temp * cos(phase)
local[i].im=dsp_math_multiply(p_state>fftFrqArray[i],dsp_math_sin(p_state>angle[i]),24); // im= temp * sin(phase)
}
dsp_fft_bit_reverse_and_inverse_real(local,512,dsp_sine_256,dsp_sine_512);
Last edited by link0513 on Mon Jul 22, 2019 8:18 am, edited 1 time in total.

 Member++
 Posts: 26
 Joined: Tue Jul 02, 2019 8:38 am
The above code is reference matlab code
Code: Select all
mag = abs(F);
mag = sqrt(real(F).^2 + imag(F).^2);
phase = atan2(imag(F),real(F));
re = mag .* cos(phase);
im = mag .* sin(phase);
F = re + 1i*im;
f = ifft2(F);

 Member++
 Posts: 26
 Joined: Tue Jul 02, 2019 8:38 am
By the way, I try to use below code to instead of p_state>angle = dsp_math_atan(dsp_math_divide(local.im, local.re, 24));, but the phase of the output signal is still wrong. Anybody know why? Please!!!
Code: Select all
loca_im_re[0] = local[i].re;
loca_im_re[1] = local[i].im;
dsp_math_atan2_hypot(loca_im_re,0);
p_state>angle[i] = loca_im_re[1];
p_state>angle[i] = dsp_math_multiply(p_state>angle[i],PI_Q8_24,24); // Transfer the angle to radian. angle*pi/180 = radian
p_state>angle[i] = dsp_math_divide(p_state>angle[i],Q24(180),24);

 Member++
 Posts: 26
 Joined: Tue Jul 02, 2019 8:38 am
This problem is solved.