# A filter with low phase shift or no phase shift at all? Which one? [duplicate]

I'm seeking a good advice for a filter. Can you help me?

Right now I made my own simple filter. Not sure what I'm doing, but it seems to work.

function testFilter (t, y, p)

% Forward filtering
% Do first filtering by jumping k/2 elements in the future
l = length(t);
ye = y;
for i = 1:l
sum = 0;
k = 0;
for j = 0:p-1
if(i + j <= l)
sum = sum + ye(i + j);
k = k + 1;
end
end
if(k != 0)
ye(i+floor(k/2)) = sum/k;
end
end

% Do the first p elements!
for i = p:-1:1
sum = 0;
for j = 1:i
sum = sum + ye(j);
end
ye(i) = sum/i;
end

% Done!
figure
plot(t, ye, t, y);
legend("Filtered", "Noisy")
end


It's some kind of moving average, ich. The reason why I selected this is beacuse a low pass filter caused phase shift and I don't like that.

Here is a plot that shows a example how my filter algorithm works. I think it's OK, but can it be done better without phase shift? Do you think Fast Fourier Transform is a much better filter in this noisy case?

If there is no filter that cause phase shifting, can you recommend a good filter for me then with low phase shift?

• – MBaz Apr 18 '20 at 20:31
• @MBaz Not really answering my question. – Daniel Mårtensson Apr 18 '20 at 20:42
• @MBaz Could you recommend a good filter for me? – Daniel Mårtensson Apr 18 '20 at 20:42
• Your example reminds me a lot of the one in this answer. So might it be that non-linear filtering (e.g., median filtering) is more suitable than linear filtering for your type of signals? – Matt L. Apr 19 '20 at 8:41

If your filter is $$h[n] \leftrightarrow H(e^{j\omega})$$, then the DTFT of the time reversed filter $$h[-n]$$ is $$H^*(e^{j\omega})$$. Consider the result of passing your signal through the combined filter $$h[n]*h[-n]$$, which has a DTFT of $$H(e^{j\omega})H^*(e^{j\omega})=|H(e^{j\omega})|^2$$ and has zero-phase. In this setup, I've assumed that $$h[n]$$ is causal so that makes $$h[-n]$$ anticausal, and this makes the overall filter $$h[n]*h[-n]$$ noncausal and not applicable for real time applications but can be used for offline processing.
This is the idea used in the MATLAB command filtfilt. You pass filtfilt the filter coefficients and it runs the input signal through the filter and then the time reversed filter. Of course, you can also just as well do that by hand.
• I know it is not a lot, but believe it or not the few math in the answer the all filtfilt is doing. You can easily test this by running filtfilt and then doing it "by-hand" by calling filter twice with $h[n]$ and then the time reversed version $h[-n]$, which is actually what is shown in the illustration (I did verify with filtfilt that they are the same). – Engineer Apr 19 '20 at 0:29