What is different between FFT and SHIFTFFT

Please can someone explain why this code :
fftshift(fft((x - mean(x)).*hann(L), NFFT)/sum(hann(L)))
is better than
fftshift(fft(x))?
Why not fftshift(fft(x))?
Why subtracting mean(x) from fftshift(fft(x))?
From matlab documentation Hann returns an L-point symmetric Hann window. Please does this mean?
Why sum(hann(L)) ? Why not hann(L)?

댓글 수: 2

Subtracting the mean gets you an fft() in which the first bin is 0 . But it is not clear that is productive since hann() starts with a 0.
sum(hann(L)) appears to be (L-1)/2 for L > 2 -- for L = 2 exactly the "0 at the end" rule takes precidence. For L = 1 exactly hann() is 1 .
University
University 2023년 11월 2일
Thank you for this

댓글을 달려면 로그인하십시오.

 채택된 답변

Chunru
Chunru 2023년 10월 31일
First, it is not correct to say which is better unless you define what is "better". fft(x) has better resolution but higher sidelobe levels than fft(x.*hann(L)).
The substraction of mean(x) from x is to remove the DC component so that the spectrum has 0 (approximately) at 0 frequency.
fftshift is to re-arrange the frequency axis. fft alone has frequency from 0-fs. fftshift has frequency range of (-fs/2, fs/2).
Division by sum(hann(L)) is a normalization factor. With this normalization a unit amplitude signal exp(2i*pi*f) will have a unit spectrum value:
f0 = 1/4;
L = 512;
w = hann(L);
x = exp(2i*pi*f0*(0:L-1)'); % unit amplitude
y = fft(x.*w./sum(w)); % unit spectrum at f0
plot(abs(y))
To recap:
Why not fftshift(fft(x))? ==> You can use it if you don't mind the side lobe level.
Why subtracting mean(x) from fftshift(fft(x))? ==> To remove DC from the signal and spectrum.
From matlab documentation Hann returns an L-point symmetric Hann window. ==> Normalization factor

댓글 수: 3

Providing that there are no nan or inf entries, and providing there is no overflow, then to within round-off error,
F1 = fft(x);
F2 = fft(x - mean(x));
then:
F1(1) will not generally be 0: F1(1) will be sum(x) which will only be 0 if x has a mean of 0
F2(1) will be 0 (to within round-off error.)
F1(2:end) will be the same as F2(2:end) to within around-off error.
That is, fft(x) and fft(x-mean(x)) are the same except for the first entry (to within round-off error.)
Now, except for the denerate case of L = 1, the first and last entries in the hann window will be 0. And if you multiply 0 by a value you will get 0 (provided the value is finite.) So if you had a non-zero entry in F1(1) and you multiply it by the 0 that starts the hann window, you are going to get 0 in that location -- the same as if you had a zero entry in that location because you had subtracted the mean before doing the fft. Therefore when you use the combination fft(x) .* hann(L) then the result is going to be the same (to within round-off error) as fft(x-mean(x)) .* hann(L) .
Therefore in context, subtracting the mean to get rid of the DC component does not do anything useful.
... but that is only because you happen to know that hann starts with 0. If you substituted a different window function that did not start with 0, then it could matter. So subtracting the mean is more general code -- less change needed if you were thinking of manybe substituting a different windowing function.
Chunru
Chunru 2023년 10월 31일
편집: Chunru 2023년 10월 31일
It seems there is a confusion here. Even the hanning window has 0 for t=0 (assuming L>>1). Multiply x(t) with h(t) will make a modified signal s(t)=x(t)*w(t) and s(t=0)=0. But this will not make F(f=0)=0 in general.
However, subtracting mean, i.e. x(t)-mean(x) will ensure its Fourier Transform has 0 (up to round-off error) amplitude for DC (ie first bin). So it DOES matter to substract mean if one wants to remove DC, whether h(0)=0 or not. See the illustration below (paying attention to DC component).
==> combination fft(x) .* hann(L) then the result is going to be the same (to within round-off error) as fft(x-mean(x)) .* hann(L)
Note that we are concerned with time domain window, x.*h, instead of frequency domain window fft(x).*h.
fftshift(fft( (x - mean(x)) .*hann(L), NFFT)/sum(hann(L)))
The above is a time domain window instead of freq domain window.
f0 = 1/4;
L = 512;
w = hann(L);
x = exp(2i*pi*f0*(0:L-1)') + 1; % unit amplitude
y = fft(x.*w./sum(w)); % unit spectrum at f0
subplot(121)
plot(abs(y)); title('With DC')
y1= fft((x-mean(x)).*w./sum(w));
subplot(122)
plot(abs(y1)); title('DC Removed')
University
University 2023년 11월 2일
Thnak you so much. Your explanation really helped

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

릴리스

R2023a

질문:

2023년 10월 30일

댓글:

2023년 11월 2일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by