Hi,
When I use filter() function in matlab, I get
correct results, but when I write my own code for same, I get results which are
slightly different. While the differerence looks innocous in the floating point,
it is sufficient to introduce serious errors in the further
processing.
I have designed a notch filter of the form
h[z] = 1/2*( (a1 +a2 * z-2 + z-4) /( 1 + a2 *
z-2 + a1 * z-4) + (a3 + z-2)/(1 + a3*
z-2))
The coefficients a1, a2, a3 are : 0.7951,
1.7541, 0.80634
CASE 1:
The code is:
%Please note that the coefficients have been
converted from fixed point.
N1_DEN = [16384, 0, 28739, 0, 13027]/2^14;
N1_NUM = [13027, 0, 28739, 0, 16384]/2^14; n3 = [13211, 0, 16384; 16384, 0, 13211]/2^14; out = 1/2*(filter(N1_NUM, N1_DEN, inp) +
filter(n3(1, :), n3(2, :), inp));
CASE 2:
N1_DEN = [16384, 0, 28739, 0, 13027]/2^14;
N1_NUM = [13027, 0, 28739, 0, 16384]/2^14; n3 = [13211, 0, 16384; 16384, 0, 13211]/2^14; nb = zeros(9, 1); % notch filter buffer to keep
the delay elements. from 1:5 location input buffer is maintained and from 6:9
output buffer is maintained.
for i = 1:length(inp)
for j = 5:-1:2 nb(j) = nb(j-1); end; nb(1) = inp(i); temp = N1_NUM(1)*nb(1) + N1_NUM(3)*nb(3) + nb(5) - N1_NUM(1)* nb(9) - N1_NUM(3)*nb(7); temp = temp + nb(3) + n3(1, 1)*nb(1) - n3(1, 1)*nb(7); out(i) = (temp)/2; for j = 9:-1:7 nb(j) = nb(j - 1); end nb(6) = inpn(i); end; -------------------------------
A sample of the input used
is:
inp = 0.00011822
0.0016066 0.010308 0.043496 0.10114 0.12366 0.096314 0.063672 0.017284 -0.011495 -0.028185 -0.073075 -0.14391 -0.18678 -0.04443 0.092537 0.069423 0.11965 0.11911 0.052073 0.040319 -0.017407 -0.020459 -0.061295 -0.13418 The output obtained in the first case
is:
9.4658e-005
0.0012864 0.0082957 0.035398 0.084605 0.11401 0.1102 0.083313 0.022507 -0.01178 -0.023764 -0.061026 -0.12386 -0.17308 -0.080375 0.025542 0.074803 0.1668 0.10572 0.029844 0.065665 0.011773 -0.027068 -0.072938 -0.10786 The output in the second case is: 9.4658e-005
0.0012864 0.0082954 0.035394 0.084578 0.1139 0.10997 0.083139 0.02263 -0.011409 -0.0233 -0.060952 -0.12457 -0.17337 -0.079099 0.026531 0.073238 0.1648 0.10644 0.0312 0.065631 0.012134 -0.027643 -0.074952 -0.10615 |
precision loss in the filter implementation
Started by ●July 1, 2004