%% Estimate the Frequency Response of Fixed-Point FIR Filter
% Estimate the frequency response of a fixed-point FIR filter.
%

firFilt = design(fdesign.lowpass(.4,.5,1,60),'equiripple','Systemobject',true);
dataIn = fi(randn(16,15),1,16,15);
dataOut = firFilt(dataIn); %#ok
[h,w] = freqrespest(firFilt); %#ok % This should be about the same as freqz.
release(firFilt);

%
% Continuing with the filter object firFilt, change the FullPrecisionOverride property to false and then specify the word lengths and precision (the fraction lengths) applied to the output from internal addition and multiplication operations. After you set the word and fraction lengths, use the freqrespest function to compute the frequency response estimate for the fixed-point filter.

firFilt.FullPrecisionOverride = false;
firFilt.ProductDataType = 'Custom';
firFilt.CustomProductDataType = numerictype(1,16,15);
firFilt.AccumulatorDataType = 'Custom';
firFilt.CustomAccumulatorDataType = numerictype(1,16,15);
firFilt.OutputDataType = 'Same as accumulator';
dataOut = firFilt(dataIn);
[h,w] = freqrespest(firFilt,2);
[h2,w2] = freqz(firFilt,512);
plot(w/pi,20*log10(abs([h,h2])))
legend('Frequency response estimated by filtering',...
'Freq. response computed by quantizing coefficients only');
xlabel('Normalized Frequency (\times\pi rad/sample)')
ylabel('Magnitude (dB)')

% Copyright 2012 The MathWorks, Inc.