
% Scaling fixed point filters
figure(1);clf;

%define the filter
[b,a] = butter(2,[.2 .3])
%define the sample frequency
Fs = 8000;

%scale filter by some power of two
%Try 1, 2, 16
scaleFactor = 16;
 b = b * scaleFactor;
 a = a * scaleFactor;

 % truncate the coefficients to 8 bits of fraction
 % to simulate 8:8  fixed point
bfixed = fix(b*256)/256;
afixed = fix(a*256)/256;

%plot frequency response for the exact filter
[fresponse, ffreq] = freqz(b,a,1000);
plot(ffreq/pi*Fs/2,abs(fresponse), 'b', 'linewidth',3);
xlabel('frequency'); ylabel('filter amplitude');
hold on
%plot frequency response for the truncated filter
[fresponse, ffreq] = freqz(bfixed,afixed,1000);
plot(ffreq/pi*Fs/2,abs(fresponse), 'r', 'linewidth',2);
xlabel('frequency'); ylabel('filter amplitude');
legend ('exact','truncated')