close all;

fs = 1e6;
fpass = 0.10*fs;
fstop = 0.13*fs;
rpass = 0.1;
rstop = 120;

f = linspace(0, fs/2, 1000);
w = 2*pi*f;

% Remez FIR
dev = [(10^(rpass/20)-1)/(10^(rpass/20)+1)  10^(-rstop/20)]; 
[Nr,fo,ao,Wr] = remezord([fpass fstop], [1 0],dev, fs);
br = remez(Nr, fo, ao, Wr);
Hr = tf(br, 1, 1/fs);
Hr = Hr / 10^(rpass/40);

Mr = freqresp(Hr, w);
Mr = Mr(1, :);

% round coefficients
B = [12 16 20];
c = ['r'; 'g'; 'm'];
for i=1:length(B)
    Bi = B(i);
    b = br/max(br);
    b = round(b* (2^Bi));
    Hr_round = tf(b, 1, 1/fs);
    Hr_round = Hr_round / evalfr(Hr_round, 1);
    Mr_round = freqresp(Hr_round, w);
    Mr_round = Mr_round(1, :);
    plot(f, 20*log10(abs(Mr_round)), c(i));
    hold on;
end

plot(f, 20*log10(abs(Mr)), 'b');
fixfig;
xlabel('Frequency  0...f_s/2  [Hz]');
ylabel('Magnitude  [dB]');
legend('8 Bits', '16 Bits', '24 Bits', 'Floating Point');
axis([min(f) max(f) -rstop-10 5]);
hold off;
