How to properly take the one-sided fft in MATLAB?
69 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
L'O.G.
el 22 de Mzo. de 2022
Comentada: Star Strider
el 22 de Mzo. de 2022
My understanding is that MATLAB's fft() function takes the two-sided FFT, and that the one-sided FFT has only positive frequency components and twice the amplitude of the two-sided FFT. I want the latter one here and then to take the real and imaginary components. So here are my questions to make sure I understand this because I don't want to do this without understanding it:
- Why the form of fix() here? Should it be length(nfft) or length(f_t)? I have seen both used in examples.
- Is the form of f_w correct (i.e., divide by length(f_t) and multiply by 2 to take the one-sided transform? If so, why divide by the length?
- To take the real and imaginary components in this case, should I take the absolute value or take them as is? Taking them as is results in a negative value for the imaginary component in my case, which I don't understand.
t = linspace(0.01,100,10000);
nfft = 2^nextpow2(length(f_t));
dt = t(2) - t(1);
df = 1/dt;
Frequency = (df/2)*linspace(0,1,fix(length(f_t)/2)+1);
f_w = 2*ffft(f_t,nfft)/length(f_t);
Real_fw = real(f_w);
Imag_fw = imag(f_w);
0 comentarios
Respuesta aceptada
Star Strider
el 22 de Mzo. de 2022
There appears to be missing information (specifically ‘G_t’ and ‘f_t’), so running the code is not possible.
Otherwise:
Frequency = (df/2)*linspace(0,1,fix(length(f_t)/2)+1); % why the form of fix() here?
The fix call is only necessary if ‘f_t’ has an odd number of elements, because the third argument to linspace must be an integer. Since this code snippet uses :
nfft = 2^nextpow2(length(G_t));
the length will always be even and the ‘Frequency’ vector can be wrritten:
Frequency = (df/2)*linspace(0,1,nfft/2+1);
‘Also, I have seen in some cases people divie the output of fft() by the length of the signal. Why is that done?’
That is the recommended approach. It normalises the fft result by the length of the original time-domain vector length. The Fourier transform calculates the integral of the time domain signal, and dividing it by the length of the signal restores the correct amplitudes.
‘Finally, could somebody please explain the purpose behind line 2 defining nfft, and line 5 with the Nyquist criterion?’
The nextpow2 function returns the first power of 2 that is larger than the argument, not the actual value for the length. The ‘2^’ converts it from that to the desired length of the fft.
.
4 comentarios
Star Strider
el 22 de Mzo. de 2022
As always, my pleasure!
None of this is necessarily straightforward, and it takes time to appreciate all the complexities.
Más respuestas (0)
Ver también
Categorías
Más información sobre Fourier Analysis and Filtering en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!