1

So I took this digital LPF from some website:

enter image description here

And tried implement it first in MatLab to see how it goes, here's the code used:

x=[0 0 0 0 0 0 0 0 0 0 0];
y=[0 0 0 0 0 0 0 0 0 0 0];

t = linspace(0,1,100); %a = sin(2pi0.1*t) %plot(t,a)

yy = zeros(1, 100);

for c= 1:100 x(1) = sin(2pi1000t(c)); y(1) = ((1.x(1)+2.x(2)+ 1.x(3)) + 1.789.y(2) -0.948.y(3))./4.841; yy(c) = y(1);

for i = 1:10 x(i+1) = x(i); % store xi y(i+1) = y(i); % store yi end end

plot(t,yy)

Got this for a sine with 100Hz frequency :

enter image description here

Sine with 1000Hz frequency:

enter image description here

It's just not working as intended. What am i doing wrong?

Edit:

New sampling frequency of 1000Hz

enter image description here

New code used:

x=[0 0 0 0 0 0 0 0 0 0 0];
y=[0 0 0 0 0 0 0 0 0 0 0];

t = linspace(0,1,1000); %a = sin(2pi0.1*t) %plot(t,a)

yy = zeros(1, 1000);

for c= 1:1000 x(1) = sin(2pi10t(c)); y(1) = ((1.x(1)+2.x(2)+ 1.x(3)) + 503.273.y(2) -231.158.y(3))./276.115; yy(c) = y(1);

for i = 1:10 x(i+1) = x(i); % store xi y(i+1) = y(i); % store yi end end

plot(t,yy)

The signal appears to be already attenuated at a frequency if 10Hz, how is that possible?

enter image description here

Scipio
  • 125
  • 4

2 Answers2

4

The signal appears to be already attenuated at a frequency if 10Hz, how is that possible?

Well, if the input signal is correct (which it is) and the filter coefficients are correct (which they are) and the output is wrong (which it is), the only possibility is that your filter code is wrong (which it is).

This is good opportunity to hone your debugging skills.

  1. Start with the most simple input signal and the most simple filter (unit impuls for both). Check the results.
  2. Slowly increase the complexity of the input signal and the filter but always use examples that you can simply do by hand as well.
  3. Continue until it breaks, i.e. where the answer from your code is different from the answer you have calculated manually or have from a known good reference implementation.
  4. Then single step through your code and determine where it breaks.
  5. Putting comments in your code liberally is also a VERY good idea. Your code has none.
Hilmar
  • 44,604
  • 1
  • 32
  • 63
1

Your code performs filtering correctly.

One problem is your input signal frequencies ($f_1 = 100\,\text{Hz}$ and $f_2 = 1000\,\text{Hz}$) are too high with regards to your sampling frequency ($f_s = 100\,\text{Hz}$), violating Nyquist, and introducing aliased frequencies ($f_1$ aliases to $1\,\text{Hz}$ and $f_2$ aliases to $10\,\text{Hz}$).

To see your filter in action, set $f_1 = 10\,\text{Hz}$ and $f_2 = 30\,\text{Hz}$, which are $\leq f_s/2$:

x(1) = sin(2*pi*10*t(c) % LPF will pass this signal
x(1) = sin(2*pi*30*t(c)) % LPF will attenuate this signal

EDIT

Another problem is, as @Hilmar mentioned in his answer, that there is a bug in your code with the past input and past output storing routine (your inner loop is not doing what you think it's doing).

Jdip
  • 5,980
  • 3
  • 7
  • 29
  • Things seem to work fine if i do as you say. In the new post edit i just made i changed the sampling frequency to 1000Hz, the signal seems to be attenuated already at 10Hz? – Scipio Dec 12 '22 at 12:38
  • Sorry, the code does NOT work correctly. – Hilmar Dec 12 '22 at 13:40
  • @Scipio Hilmar is correct. There is indeed a bug in your filtering routine. I edited accordingly. – Jdip Dec 12 '22 at 14:07
  • Yeah thanks to you guys i already found out, it should be i = 10:-1:1. Now everything works and intended. – Scipio Dec 12 '22 at 14:39