image thumbnail

EXPO 2011 Signal Processing Workflow Demo Files

by

 

MATLAB EXPO 2011 Signal Processing Workflow Demo Files

fi_nts_demo.m
%% Œ菬_IIRtB^݌v 
% Œ菬_ASY̊Jƌ
%
% Copyright 2004-2010 The MathWorks, Inc.
% $Revision: 1.1.6.12 $


%% ASYJ̊ȒPȗ
% 
% ̗ł́AȒPȌŒ菬_tB^[ ASY̊Jƌ؂ɂ‚ĐĂ܂B ȉ̎菇ɏ]܂B
% 
% 1) 2 tB^[ ASYA{x_ŃV~[g܂B
% 
% 2) R[hCXggAo͂ƏԂ̃_Ci~bN WŽ܂B
% 
% 3) ϐ̃f[^^ύX邱ƂɂAASYŒ菬_ɕϊ܂BASŶ͕ς܂B
% 
% 4) Œ菬_ƕ_̌ʂrAvbg܂B

%% _ϐ̒`
% {x_ŃASYJ܂B 
% 2 [pX tB^[gpāA͐M̍g܂B

b = [ 0.25 0.5      0.25    ]; % Numerator coefficients
a = [ 1    0.09375  0.28125 ]; % Denominator coefficients
% Random input that has both high and low frequencies.
rstream = RandStream.create('mt19937ar','seed',2009);
reset(rstream);
x = randn(rstream,1000,1);
% Pre-allocate the output and state for speed.
y = zeros(size(x));
z = [0;0];

%% f[^^ˑASY
% ́AW̍ 2 tB^[łB
%  
%  y(n) = b(1)*x(n) + b(2)*x(n-1) + b(3)*x(n-2) - a(2)*y(n-1) - a(3)*y(n-2)
%
for k=1:length(x)
    y(k) =  b(1)*x(k) + z(1);
    z(1) = (b(2)*x(k) + z(2)) - a(2)*y(k);
    z(2) =  b(3)*x(k)         - a(3)*y(k);
end

% Save the Floating-Point Result
ydouble = y;

%% _Ci~bN W̉Ž
% 
% Œ菬_ɕϊɂ́Aϐ͈̔͂mKv܂B
% ASY̕GɂāÃ^XN͊ȒPȏꍇ
% ɓꍇ܂B̗ł́A͒l͈̔͂킩
% Ă邽߁AK؂ȌŒ菬_f[^^I邱Ƃ͊ȒPłB
% o (y) Ə (z) ͈̔͂͂킩Ȃ߁A 2 ‚ɒڂ܂B
% o͂ƏԂ̃_Ci~bNW\ɂ́AR[h
% ύXĊmF܂B 
% 2 ‚ NumericTypeScope IuWFNg쐬A
% o (y) Ə (z) ̃_Ci~bN W𓯎ɕ\܂B


%% _R[hŃf[^͈͂̊mF
% _̌vZʂNumericTypeScope\
% œKȌŒ菬_ݒ

hscope1 = NumericTypeScope;
hscope2 = NumericTypeScope;
for k=1:length(x)
    y(k) =  b(1)*x(k) + z(1);
    z(1) = (b(2)*x(k) + z(2)) - a(2)*y(k);
    z(2) =  b(3)*x(k)         - a(3)*y(k);
    % process the data and update the visual.
    step(hscope1,z);
end
step(hscope2,y);

%% XR[v̏̉
% 
% ܂Aϐ z () ɂ‚ĕ\Ă͂܂傤B
% qXgOA_Ci~bN W $2^{1}$  $2^{-20}$ 
% Ԃɂ邱Ƃ킩܂B
% ̐ݒł́AXR[v 16 rbǧ꒷gpAI[o[t[
% e܂B ɂAI[o[t[邽߂ɏȂ
% Ƃ 1 rbgKvƂ邽߁Af[^^ numerictype(true,16, 14) 
% ƂȂ܂BĂꂽ̃f[^^gpƁA14 rbgȉ
% ł͂ꎩ̂\Ȃl̏ꍇAA_[t[܂A
% ̏ꍇA0.1% łAł͈͂łB [Input Data]  
% [Resulting Type] plɂ́Avf[^ɂ‚Ă̏ڍ׏񂪕\܂B
% [Input Data] plł́Af[^ɐ̒lƕ̒l̗܂܂ĂA
% t̗ʂł邱ƁAĂ炪Ăꂽ numerictype ɔf
% Ă邱Ƃ킩܂B ܂Af[^̍ől 1.8 łA͒
% ꂽf[^^ŕ\ł邱Ƃ킩܂B
% 
% ɁAϐ y (o) Ă݂܂傤B qXgO vbgA
% _Ci~bN W $2^{2}$  $2^{-11}$ ̊Ԃɂ邱Ƃ킩܂B
% 
% ̐ݒł́AXR[v 16 rbǧ꒷gpAI[o[t[
% e܂B ɂAI[o[t[邽߂ɏȂƂ
% 2 rbgKvƂ邽߁Af[^^ numerictype(true,16, 13) 
% ƂȂ܂BĂꂽ̃f[^^gpƁAI[o[t[܂
% A_[t[\܂BeI[o[t[킸
% Ɖ肷ƁA[h TCYɍœKł܂B 
% [Bit Allocation] pl [Maximum Overflow] p[^[ 0.5% 
% ݒ肷ƁA̒ 1 rbg炵Đxグ邱Ƃł܂B

%% Œ菬_ϐ̒`
% ϐŒ菬_ɕϊAASYēxs܂B 
% OIɂAIf[^^ɂI[o[t[ƃA_[t[
% mF܂B

% Turn on logging to see overflows/underflows.
fp = fipref;
default_loggingmode = fp.LoggingMode;
fp.LoggingMode = 'On';
% Capture the present state of and reset the global fimath to the factory
% settings.
globalFimathAtStartOfThisDemo = fimath;
resetglobalfimath;
% Define the fixed-point types for the variables in the below format:
%   fi(Data, Signed, WordLength, FractionLength)
b = fi(b, 1, 8, 6);
a = fi(a, 1, 8, 6);

x = fi(x, 1, 16, 13);
y = fi(zeros(size(x)), 1, 16, 13);
z = fi([0;0], 1, 16, 14);

%% f[^^ˑASY
for k=1:length(x)
    y(k) =  b(1)*x(k) + z(1);
    z(1) = (b(2)*x(k) + z(2)) - a(2)*y(k);
    z(2) =  b(3)*x(k)         - a(3)*y(k);
end
% Reset the logging mode.
fp.LoggingMode = default_loggingmode;

%%
% ̗ł́AASY R[hCCĂ킩₷邽߂ɁA
% Œ菬_ϐ𕂓_ƓOōĒ`Ă܂B
% A_ϐƌŒ菬_ϐ̂ǂłĂяo
% MATLAB t@C֐ŃASYR[h͂ނƂ߂܂B
% f[^^̕sȃASY̋LqƎgṕ̗Afilimitcycledemo.m 
% QƂĂB

%% _ƌŒ菬_̌ʂ̔rƃvbg   
% _ƌŒ菬_̌ʂ̃QCƃtB^[̉vbgA
% Œ菬_ւ̕ϊɃtB^[҂ǂ̓Ă邩ǂ
% mF܂B

n = length(x);
f = linspace(0,0.5,n/2);
x_response = 20*log10(abs(fft(double(x))));
ydouble_response = 20*log10(abs(fft(ydouble)));
y_response = 20*log10(abs(fft(double(y))));
plot(f,x_response(1:n/2),'c-',...
    f,ydouble_response(1:n/2),'bo-',...
    f,y_response(1:n/2),'gs-');
ylabel('Magnitude in dB');
xlabel('Normalized Frequency');
legend('Input','Floating point output','Fixed point output','Location','Best');
title('Magnitude response of Floating-point and Fixed-point results');
%%
h = freqz(double(b),double(a),n/2);
clf
hax = axes;
plot(hax,f,20*log10(abs(h)));
set(hax,'YLim',[-40 0]);
title('Magnitude response of the filter');
ylabel('Magnitude in dB')
xlabel('Frequency');
%%
% ͐M̍g̓[pX tB^[ŌĂA
% ͊҂ǂ̓łB

%% 덷̃vbg
clf
n = (0:length(y)-1)';
e = double(lsb(y));
plot(n,double(y)-ydouble,'.-r', ...
     [n(1) n(end)],[e/2 e/2],'c', ...
     [n(1) n(end)],[-e/2 -e/2],'c')
text(n(end),e/2,'+1/2 LSB','HorizontalAlignment','right','VerticalAlignment','bottom')
text(n(end),-e/2,'-1/2 LSB','HorizontalAlignment','right','VerticalAlignment','top')
xlabel('n (samples)'); ylabel('error')

%% Simulink(R)
%
% Simulink  Simulink Fixed Point ꍇÃfsł܂B
% ͏L̃ASYɑ܂B o y_sim ́AL 
% MATLAB R[hŌvZꂽϐ y ɓŒ菬_ϐłB
% 
% MATLAB R[hƓlɁAubŇŒ菬_p[^[͎ۂ
% VXeɍ悤ɕύXł܂B́AL̗ł MATLAB R[h
% ɍ킹Đݒ肳Ă܂B ubN_uNbNĐݒmF܂B

if fidemo.hasSimulinkFixedPointLicense
    
    % Set up the From Workspace variable
    x_sim.time = n;
    x_sim.signals.values = x;
    x_sim.signals.dimensions = 1;
    
    % Run the simulation
    simopts = simset('SrcWorkspace','current');
    sim('fitdf2filter_demo',[],simopts)
    
    % Open the model
    fitdf2filter_demo
    
    % Verify that the Simulink results are the same as the MATLAB file
    isequal(y, y_sim)
    
end

%% ̗̑O
%
% ȒPɂ邽߂ɁA̐wp[^[łAł߂lւ̊ۂ߁A
% I[o[t[ŖOaAtx̏ZAјagpĂ܂B
% ̃p[^[͂Aۂ̃VXeɍ킹ĕύXł܂B
% 
% ̐ݒ́AASYJ̏o_ƂđIĂ܂B
%  MATLAB t@C̃Rs[ۑÃp[^[gpĂ݂āA
% o͂ւ̉emF܂B͂قȂꍇAASY̓͂ǂȂ
% ł傤B ̑̃p[^[ (ۂ߃[hI[o[t[ [hȂ)
% ̐ݒ@̏ڍׂ́AfiAfimathA numerictype ̃wvQƂĂ
% B

close all force;
bdclose all;
% 
% EOF

Contact us