How to Generate Memory Initiation Using Matlab

Nataniel

Nataniel

Photographer, content designer and trader

. . .

I want to share some trick how to write mif memory (memory initiation in Altera ) from using MATLAB. If you are working on image compression (DCT)  project now, you will need to extract image coefficients into memory. As we know, DCT need three coefficient from an image (JPEG) to be processed. They are Y Cr and Cb ( Luminace Red-Chromium and Blue-Chromium).  JPEG has a standard as known as RGB ( Red Green Blue).  To convert RGB into YCrCb, we can use scrip below ( in MATLAB ) :

%Read file into a matrix
array_input_rgb = imread(‘blue.jpg’);

%Reads file info
array_info = imfinfo(‘blue.jpg’);
width=array_info.Width;
height=array_info.Height;

% Colour conversion from RGB to YCbCr
array_input=rgb2ycbcr(array_input_rgb);


Now, we create matrix that will be filled by image coefficient.

% Create matrix which its height and width are divisible by 8.
% If is not, fill up with zeros
W=ceil(width/8);
H=ceil(height/8);
input=zeros(H*8,W*8,3,’uint8′);
for I=1:3
input(1:height,1:width,I)=array_input(1:height,1:width,I);
end

% Divide input matrix into WxH 8×8 matrices
X=zeros(H,W,8,8,3,’uint8′);

Generate matrix into divided memory ( Y Cr Cb )

fid = fopen(‘dmemory1.mif’,’w’);
fprintf(fid,’WIDTH=8;n’);
fprintf(fid,’DEPTH=25344;n’);
fprintf(fid,’ADDRESS_RADIX=UNS;n’);
fprintf(fid,’DATA_RADIX=UNS;n’);
fprintf(fid,’CONTENT BEGIN;n’);
a=0;
for J=1:H
for K=1:W
for row=1:8
for col=1:8
X(J,K,row,col,I)=input((J-1)*8+row,(K-1)*8+col,1);
fprintf(fid,’%i : %i ;n’,a,input((J-1)*8+row,(K-1)*8+col,1)); % 1 for Y
a=a+1;
end
end
end
end
% end
fprintf(fid,’END;n’);

fid = fopen(‘dmemory1.mif’,’w’);
fprintf(fid,’WIDTH=8;n’);
fprintf(fid,’DEPTH=25344;n’);
fprintf(fid,’ADDRESS_RADIX=UNS;n’);
fprintf(fid,’DATA_RADIX=UNS;n’);
fprintf(fid,’CONTENT BEGIN;n’);
a=0;
for J=1:H
for K=1:W
for row=1:8
for col=1:8
X(J,K,row,col,I)=input((J-1)*8+row,(K-1)*8+col,1);
fprintf(fid,’%i : %i ;n’,a,input((J-1)*8+row,(K-1)*8+col,2)); % 2 for Cr
a=a+1;
end
end
end
end
% end
fprintf(fid,’END;n’);

fid = fopen(‘dmemory1.mif’,’w’);
fprintf(fid,’WIDTH=8;n’);
fprintf(fid,’DEPTH=25344;n’);
fprintf(fid,’ADDRESS_RADIX=UNS;n’);
fprintf(fid,’DATA_RADIX=UNS;n’);
fprintf(fid,’CONTENT BEGIN;n’);
a=0;
for J=1:H
for K=1:W
for row=1:8
for col=1:8
X(J,K,row,col,I)=input((J-1)*8+row,(K-1)*8+col,1);
fprintf(fid,’%i : %i ;n’,a,input((J-1)*8+row,(K-1)*8+col,2)); % 2 for Cr
a=a+1;
end
end
end
end
% end
fprintf(fid,’END;n’);

Leave a Reply

Archives