0
$\begingroup$

I need to get the difference equation of a specific elliptic filter. I calculated the transfer function coefficients in MATLAB with:

  %% Low pass design
  n = 10;
  passband_ripple = 1;
  stopband_attenuation = 60;
  f = 4750;
  fs = 44100;
  [B,A] = ellip(n,passband_ripple,stopband_attenuation,f/(0.5*fs),'low');

However, when I use the difference equation, I calculated from the transfer function, the samples go to +-inf. Here is the code where I create the noise input sound and filter it with difference eq.

  xin = rand(1,fs/20);     
  xin = xin - mean(xin);         
  xin = [xin zeros(1,fs*2)];  

  x = xin;
  y = zeros(1, length(x));

  %% Low pass
  b1 = 0.0027;
  b2 = -0.0134;
  b3 = 0.0362;
  b4 = -0.0651;
  b5 = 0.089;
  b6 = -0.0978;

  a1 = -7.953;
  a2 = 29.7485;
  a3 = -68.5714;
  a4 = 107.5494;
  a5 = -119.7162;
  a6 = 95.6848;
  a7 = -54.2043;
  a8 = 20.8340;
  a9 = -4.9109;
  a10 = 0.5401;

  for n = 11:length(y)
      y(n) = b1*x(n) - b2*x(n-1) + b3*x(n-2) - b4*x(n-3) + b5*x(n-4) - ...
      b6*x(n-5) + b5*x(n-6) - b4*x(n-7) + b3*x(n-8)- b2*x(n-9) + ...
      b1*x(n-10) - ...
      a1*y(n-1) - a2*y(n-2) - a3*y(n-3) - a4*y(n-4) - a5*y(n-5) - ...
      a6*y(n-6) - a7*y(n-7) - a8*y(n-8) - a9*y(n-9) - a10*y(n-10);
  end

The filter function does what I want, but I need to be able to filter the sound sample-by-sample.

y = filter(B,A,x);

How can I obtain the right difference equation of my filter?

$\endgroup$
0
$\begingroup$

There are two problems with your code. First, you should not invert every other $b$ coefficient, they should all have a positive sign, just like the $a$ coefficients that all have a negative sign. Second, - and that's the main problem -, why do you quantize the coefficients? Your actual coefficient $b[1]$ is 0.00266475348894505 (and even longer), and you rounded it to 0.0027. That's bad for the $b$ (numerator) coefficients, but detrimental for the $a$ (denominator) coefficients, because the filter's characteristic is significantly changed, and the filter can become unstable. You should just use the coefficients as stored in the vectors A and B.

Implementing the difference equation is a nice exercise to understand what's going on, but I think you'd be better off using filter.m.

$\endgroup$
  • $\begingroup$ Using the actual coefficient instead of rounded solved the problem! Thank you! I didn't realize that the coefficients are rounded as I was transcribing them from Variables window in Matlab. I am sorry about the coefficients inverting mistake in my original code. I was trying different things out of craziness after the code that was supposed to be working didn't work and I forgot to fix the experimental code. $\endgroup$ – Tomáš Gajarský Jan 8 '18 at 17:46

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.