2
$\begingroup$

I need to find a filter that revert the one pole filter of the current signal, a function using Python (or MATLAB) scipy.signal.filtfilt or scipy.signal.lfilter

The "original" signal that i want to recover is :

[0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] 

The "filtered" signal that i have is:

[ 0., 0., 0., 0., 0., 0., 0., 0.25956786, 0.48537242, 0.68182373, 0.85275161, 1.00148237, 1.1309067, 1.24353671, 1.34155607, 1.42686391, 1.50111103, 1.62198067, 1.67093897, 1.71355379, 1.81104326, 1.83550966, 1.85680735, 1.87534702, 1.89148581, 1.90553486, 1.9177649, 1.92841125, 1.93767929, 1.95277071]

I want find the (b, a) values to execute the "original=signal.filtfilt(b,a,filtered)"

Original = Red Filtered = Blue

filtered=blue, original=red

$\endgroup$
  • $\begingroup$ Do you know the coefficients of the filter that generated the filtered signal? $\endgroup$ – Matt L. Dec 24 '16 at 15:24
  • $\begingroup$ hi Matt, no i only have the signals, i know that the blue one have a high frequency noise near t~=15 (you can see that it's not exponential) no problem if the value of recovered signal get some exponential decreasing noise $\endgroup$ – user2559936 Dec 24 '16 at 21:16
  • $\begingroup$ i think the "red -> blue" is a one-pole lowpass filter, like 'y[t]=a*y[t-1]+(1-a)*x[t]' but i didn't checked (yet) what "a" value best fit this curve, i could use a curve fit function but, i want understand what's the rational behind 'recovering' this signal, maybe a multi zero +pole filter, but i didn't understood yet how to calculate it $\endgroup$ – user2559936 Dec 24 '16 at 21:19
2
$\begingroup$

The blue curve does not seem accurate and is a little non-smooth. However, I will give a single-pole filter to approximate it.

It is not difficult to show that the step response of the filter $$H(z)=\frac{1-p}{1-pz^{-1}}$$ is in the form of

$$s[n]=(1-p^{n+1})u[n]$$

To find the step response, it suffices to shift the signal given in the question to the left and divide it by $2$. Hence, the output at zero is $0.25956786/2= 0.1298$. To find the parameter $p$, substitute it in the given equation for $s[n]$ at $n=0$:

$$0.1298=1-p\Rightarrow p=0.8702$$ So the filter we were looking for is $$H(z)=\frac{0.1298}{1-0.8702z^{-1}}$$ More clearly, $b=0.1298$ and $a=[1,-0.8702]$.

Here is a comparison in Matlab: enter image description here

It can be seen that the first $10$ outputs are very close to the actual output. However, after the output becomes non-smooth, there is a small error. Alternatively, we could use the term at $n=22$ for our reference. In such case, $$1.95277071/2=1-p^{23}\Rightarrow p=0.8497.$$ This will lead to an accurate approximation at large $n$, but an error at small $n$.

Using the code

y = [0.25956786, 0.48537242, 0.68182373, 0.85275161, 1.00148237, 1.1309067, 1.24353671, 1.34155607, 1.42686391, 1.50111103, 1.62198067, 1.67093897, 1.71355379, 1.81104326, 1.83550966, 1.85680735, 1.87534702, 1.89148581, 1.90553486, 1.9177649, 1.92841125, 1.93767929, 1.95277071]
fun = @(P)2*(1-P.^(1:23))-y;
p = lsqnonlin(fun,.5);

$p=0.8644$ gives the least squares (LS) solution to this problem which is compared to the original output in the following plot ($\text{SSE}=0.02796$):

enter image description here

Note that, if the forward filter is $(b,a)$, then $(a,b)$ is the inverse filter which gives you the original input. If the error in the step response cannot be fixed, you can apply the LS approach on the overall output (taking into account the inverse filter) to get a better result.

$\endgroup$
  • $\begingroup$ How did you obtain the LS solution? $\endgroup$ – Matt L. Dec 25 '16 at 10:03
  • $\begingroup$ @MattL. I didn't calculate it myself. Just used Matlab to find it. Is anything wrong? $\endgroup$ – msm Dec 25 '16 at 10:09
  • $\begingroup$ I was just curious which function you used, because it's actually a badly non-linear LS problem, even though it's of course only one-dimensional. $\endgroup$ – Matt L. Dec 25 '16 at 10:12
  • $\begingroup$ Oh, yes it is a little annoying. I use an excellent tool in Matlab for these purposes: cftool (curve fitting tool). $\endgroup$ – msm Dec 25 '16 at 10:14
  • $\begingroup$ You're welcome. You can use many different types of fitting functions. For this one, I defined a custom fit y=1-p^(x+1), for x=0:22, with the parameter p. $\endgroup$ – msm Dec 25 '16 at 10:18
0
$\begingroup$
  • Using

    b=[0.1298]
    a=[1,-.8702]

    enter image description here

  • Using

    b=[1-.8497]
    a=[1,-.8497]

    enter image description here

$\endgroup$
  • $\begingroup$ You can see the ripple is caused by the abrupt fluctuations in the blue curve. Otherwise, it can be well approximated. $\endgroup$ – msm Dec 25 '16 at 10:47
  • $\begingroup$ yes, it's a ADC converter, the original signal is a 2volts on/off, but i don't know if the prolbem is ADC quantization, or ADC sample rate prolem, or a bad RC filter.... could this high frequency 'error' be removed with digital filters? without considering the ADC->on/off 'filter' of (difference(t,t-1) <0 set output 0, diference >0 set 2, diference =0 use last output value ) $\endgroup$ – user2559936 Dec 26 '16 at 2:10
  • $\begingroup$ I think it would be better to explain the problem in a new question where you can add more context about what you are doing, some context and detail about where you get these waveforms, and what the objectives are. Currently, I cannot find out exactly your question and the reason you want to reverse the filter. More clarification will help. You may provide a link to this question as well in the new question. $\endgroup$ – msm Dec 26 '16 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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