clear all ; clc;
%format shorte
format short

%%%%%%%%%%%%%%%%
% scale factor truncated to 14 or 30 bits
% coeff truncated to 14 or 30 bits
%%%%%%%%%%%%%%%%


% frequencies are in units of Fs/2
% so cutoff=0.1 and Fs=8kHz means
% 400 Hz.
cutoff1 = 0.1;
cutoff2 = 0.15;
%lowpass
[b,a]= butter(2, cutoff1);
%bandpass
%[b,a]= butter(1, [cutoff1 cutoff2]);
b
a

figure(1); clf;

%plot frequency response for the exact filter
[fresponse, ffreq] = freqz(b,a, 5000);
plot(ffreq/pi,abs(fresponse), 'b', 'linewidth',3);
xlabel('normalized frequency'); ylabel('filter amplitude');
hold on

%plot frequency response for the truncated 14 filter
b_14 = fix(b*2^14)/2^14;
a_14 = fix(a*2^14)/2^14;
%return
[fresponse, ffreq] = freqz(b_14, a_14, 5000);
plot(ffreq/pi,abs(fresponse), 'r', 'linewidth',2);
%xlabel('frequency'); ylabel('filter amplitude');
%legend ('exact','truncated 14 bits')
b_30 = fix(b*2^30)/2^30;
a_30 = fix(a*2^30)/2^30;
[fresponse, ffreq] = freqz(b_30, a_30, 5000);
plot(ffreq/pi,abs(fresponse), 'g', 'linewidth',2);
%xlabel('frequency'); ylabel('filter amplitude');
legend ('exact', 'truncated 14 bits', 'truncated 30 bits')
set(gca, 'xlim', [0 cutoff1*2])
set(gca, 'ylim', [0 1.1])
title('LowPass')
%%%


