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;
      if(k != 0)
        ye(i+floor(k/2)) = sum/k;

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

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

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?

enter image description here

  • $\begingroup$ See also dsp.stackexchange.com/q/48477/11256 $\endgroup$ – MBaz Apr 18 '20 at 20:31
  • $\begingroup$ @MBaz Not really answering my question. $\endgroup$ – Daniel Mårtensson Apr 18 '20 at 20:42
  • $\begingroup$ @MBaz Could you recommend a good filter for me? $\endgroup$ – Daniel Mårtensson Apr 18 '20 at 20:42
  • $\begingroup$ 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? $\endgroup$ – 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.

Here is a dumb example of a noisy sine wave and a moving average filter just to illustrate.

enter image description here

  • $\begingroup$ Thank you! What's the math behind filtfilt? $\endgroup$ – Daniel Mårtensson Apr 19 '20 at 0:15
  • $\begingroup$ 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). $\endgroup$ – Engineer Apr 19 '20 at 0:29
  • $\begingroup$ Sorry. I don't have Matlab :) $\endgroup$ – Daniel Mårtensson Apr 19 '20 at 0:33
  • $\begingroup$ Oh. Saw it was aviable for octave too. Is it a low pas filter that being called twice? $\endgroup$ – Daniel Mårtensson Apr 19 '20 at 0:36
  • $\begingroup$ You pass it the filter coefficients. It could low pass if you want, whatever type you designed. The picture shows moving average which is a crappy low pass $\endgroup$ – Engineer Apr 19 '20 at 1:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.