I've written a script to apply a Gaussian filter to a time series (signal), however I must have done something wrong, as the ifft of the filtered signal is complex, even though the original signal was real.
I know that when the first and last elements of the imaginary part vector of the FFT are equal to zero, the symmetry condition is met, i.e. the result of the IFFT will be real rather than complex. However I'm not sure if I should just manually make them be equal to zero.
Could the problem lie in how I've defined the Gaussian filter, i.e. along the entire frequency range of the FFT, and symmetrical?
Code is below. Many thanks in advance for any suggestions.
Y = fft(y);
f0 = 2;
sigma = 0.2 * f0;
f = 1:length(Y);
gaussFilter_firstHalf = normpdf (f, f0, sigma);
gaussFilter_firstHalf = gaussFilter_firstHalf ./ max(gaussFilter_firstHalf);
gaussFilter_secondHalf(i) = gaussFilter_firstHalf(length(Y)-i+1);
gaussFilter = gaussFilter_firstHalf + gaussFilter_secondHalf;
Y_filtered = Y .* gaussFilter;
y_filtered = ifft (Y_filtered);