?? demo_accelerate.m
字號(hào):
%% In this demo we show how we accelerate substantially
% the execution speed of a fixed-point algorithm in MATLAB
% with the new Fixed-Point Toolbox acceleration function called
% EMLMEX
% The idea is to resize a color image matrix. This operation is
% a very useful operation with widespread use all over image processing
% First, Read a large color image:
I = imread('peppers.png');
Nr=size(I,1);
Nc=size(I,2);
N3=size(I,3);
figure;imagesc(I);
%% The idea is to resize the image to 1/4 of its originl size
% half rows and half the columns
% Note that any resizing factor is allowed and works as well,
% just for simplicity we stick with half.
Iout=uint8(zeros([Nr/2 Nc/2 size(I,3)]));
whos I Iout
%% We have 3 color channels: red, green and blue
% Turn each of these channels to fixed-point using fi() operator
% They become unsigned 8-bit integer data types as expected.
R=fi(I(:,:,1));
G=fi(I(:,:,2));
B=fi(I(:,:,3));
whos R G B
B.numerictype
%% Initilize output resized matrices for each of these color channels
Rout=fi(Iout(:,:,1));
Gout=fi(Iout(:,:,2));
Bout=fi(Iout(:,:,3));
whos Rout Gout Bout
Rout.numerictype
%% Take a look at minimum and maximum values of each input matrix
% as expected between 0 and 255 (dynamic range of uint8)
% note that minimum of red is 5 a little higher,
% there is a little more red in this particular picture
max_min=[max(max(R)) max(max(G)) max(max(B));
min(min(R)) min(min(G)) min(min(B))]
%% Let's do the resizing with and without acceleration
% and let's note how long it takes
% I wrote a function hz_resize.m to do that,
% Algorithm is essentially a sweep through all 2x2 pixel elements of
% the image, replacing each 2x2 pixel elements with its average
% I use the same average function I developed in last demo, except
% I make it a little simpler, chaanging a divide by 4 operator with a
% simpler shift right by 2 bits
% Note that in fixed-point processing it is customary to replace
% all divides by powers of 2 with more efficient bit-shift operations
% How amny 2x2 pixel elements we go through for the whole image?
No_of_averages=size(R,1)*size(R,2)/4
which hz_resize
%% Now resize the red channel by factor of 1/4
% without using fixed-point acceleration
% Observe how muchtime is spent
tic;
Rout=hz_resize(R,Rout,Nr,Nc);
toc;
%% Now using Fixed-Point Toolbox new function emlmex
% automatically generate a comiled C version of hz_resize.m funcion
% Note that without need to writing algorithm in C for fast simulation, you
% can run the algorithm at compiled C code speed while you are in MATLAB
% and you have not written a line of C code by hand!
emlmex -eg {R, Rout, Nr, Nc} hz_resize
%% Let's run the accelerated mex version of the same resize function for
% the red channel and see how much it takes
tic;
Rout=hz_resize(R,Rout,Nr,Nc);
toc;
%% Do the same operation for the green channel
tic;Gout=hz_resize(G,Gout,Nr,Nc);toc;
%% and the blue channel
tic;Bout=hz_resize(B,Bout,Nr,Nc);toc;
%% Put them all in Iout, reconstruct the 3-dimensional color matrix
Iout(:,:,1)=uint8(Rout);
Iout(:,:,2)=uint8(Gout);
Iout(:,:,3)=uint8(Bout);
figure;imagesc(Iout)
%% You see how this myth that fixed-point simulation in MATLAB is slow and
% can never reach C code speed is dispelled. As of R2007a the fixed-point
% toolbox contains this automatic Matlab to MEX generators. It guarantees
% execution speed at C code speeds so no need for and no excuse to leave MATLAB
% and have another C code version of your design just for achieving fast
% simulation
%% How about if you want to have access to fixed-point C source code for
% downstream deployment onto fixed-poit DSP or HDL (FPGA/ASIC?
% For automatic C code generation you need RTW (Real-Time Workshop)
% which needs Simulink
% Good news your MATLAB code developed so far is seamlessly integrated into
% Simulink environment with a special Simulink block called
% Embedded MATLAB function block
% block. How to envoke it? shortcut:
emlnew
%% Now put the fixed-point M-code in that block, hook the input and output ports
% to your choice of source and sink blocks,
% I did all that for you all in the Simulink model file called
% hz_C_code.mdl, then simulate,
% it is as fast as it was with acceleration in MATLAB.
% Now touch Incremental Build button
% or Ctrl-B (shortcut) and observe the fixed-point C sourde code for your application
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -