% Matlab Fixed-Point FIR filter demo part 1
% Quantization of filter coefficients and the effect on magnitude response
% DRB 11-Nov-2013
% For ECE4703
% =================================================================
% User parameters below
% =================================================================
M = 10;         % FIR filter order
Nh = 8;         % number of total bits for filter coefficients
Mh = 7;         % number of fractional bits for filter coefficients 
% =================================================================

% Things to try:
% 1. Look at the "real world" quantized values of hq via double(hq) and
%    compare to the full-precision filter coefficients.
% 2. Try different values of Nh and Qh
% 3. What happens if Qh is too large?
% 4. Look at the stored integer values via storedInteger(hq) or dec(hq)
%    and note how these change with Nh and Qh.

% design FIR filter
h = firls(M,[0 .1 .2 .5]*2,[1 1 0 0]);  % lowpass (full precision)
hq = sfi(h,Nh,Mh);                      % quantized coefficients 

% plot frequency responses
figure(1)
[H,w] = freqz(h,1,1024);
[Hq,w] = freqz(double(hq),1,1024);
plot(w/pi,20*log10(abs(H)),w/pi,20*log10(abs(Hq)));
grid on
xlabel('normalized frequency (1=Nyquist)');
ylabel('magnitude response (dB)');
legend('full precision','quantized coefficients');