I have a problem understanding some code fragment from Matlab Signal processing Toolbox fir2() function:
% My comment: at this point vector H contains nn+1 (nn is an even number) points (double numbers) of amplitudes for a dense frequency grid
% Fourier time-shift.
dt = 0.5 .* (nn - 1);
rad = -dt .* sqrt(-1) .* pi .* (0:npt-1) ./ (npt-1);
H = H .* exp(rad);
%My comment: now H contains nn+1 complex numbers
%My comment: creates a horizontal mirror with 2*nn points
Hconj = [H conj(H(npt-1:-1:2))]; % Fourier transform of real series.
ht = real(ifft(Hconj)); % Symmetric real series.
%My comment: throws away the half after ht[nn]
b = ht(1:nn); % Raw numerator.
wind = hamming(nn);
b = b .* wind(:).'; % Apply window.
What confuses me is:
– if I comment out that Fourier time-shift, the result of ifft is symmetrical around ht[nn]
– if I leave Fourier time shift code intact, the result of ifft is no longer symmetrical around ht[nn] but it has two symmetrical groups around ht[nn/2] and ht[3*nn/4], while both parts around ht[nn] look really different in a plot. But everything after ht[nn] is thrown away, so if I need the final output to be symmetrical, I have to leave that Fourier time shift.
Why is that Fourier time shift needed? Can I replace it with some simpler algorithm in my C++ application which is not using complex numbers and still get out nn points with symmetry around ht[nn/2] so I can throw away everything after ht[nn]?
P.S. I just looked at the plots with and without Fourier timeshift and noticed that I can get the same result by shifting the second result nn/2 to the right. So theoretically I could avoid using Fourier time shift in my C++ application but just shift the real results of ifft by nn/2 to the right and then throw away everything after nn. Am I right? Is it safe to do?
According to this paper, the time-shift is needed for causal filter design. To quote,
In other words, you can bypass frequency-domain time-shift and still get the right filter back, but it simply will not be causal. It will sill have symmetry and be centered at the origin.