text to mif conversion

text to mif conversion  

  By: johnludhi on Feb. 22, 2021, 2:50 a.m.

Hello,
I am looking for something that would convert a text file (with fractional entries), with values to be stored in a LUT, into a mif file with hexadecimal entries.

For example, let a fractional value be 0.123456
For a 16-bit result, taking dec2hex( 0.123456* 2^14 ) would give out the hex conversion of decimal.
Overall, positive fractional values get converted into range 0 to 2^14, and 2^16 is added to the negative fractional values to get them in range 2^14 to 2^15.

If I now have both decimal AND fractional in a value, for example -15.123456 and I need to convert it to 16-bit-binary/4-bit-hexadecimal, how would I convert these to hex and maintain the range in which positive and negative values land?

Thank you in advance for your help.

Re: text to mif conversion  

  By: abhiphull on Feb. 22, 2021, 2:52 a.m.

I assume you use Matlab, here is short cut:

convert your data to signed integers:
data = round(data *(2^15-1)/max(data)); %for 16 bits mif

then display as column:
data'

copy and paste the column to a mif in quartus as follows:
open a new mif with 16 bits width. change data view to signed then copy the data directly, save and enjoy...

Re: text to mif conversion  

  By: abhiphull on Feb. 22, 2021, 2:53 a.m.

Hi,

You better use a program to write the mif files as copy/paste is suitable for small size data.

Here is an example

Using Matlab to write 256 signed data to 16 bit mif:

fid = fopen('filename.mif','w');
fprintf(fid,'--MIF data generated by MATLAB\n');
fprintf(fid,'--Date: %s \n\n', date);
fprintf(fid,'WIDTH=16;\n');
fprintf(fid,'DEPTH=256;\n');
fprintf(fid,'ADDRESSRADIX=UNS;\n');
fprintf(fid,'DATA
RADIX=DEC;\n');
fprintf(fid,'CONTENT BEGIN\n');
for k = 1:256
fprintf(fid,'%i : %i;\n',k-1,data(k));
end
fprintf(fid,'END;');
fclose(fid);


Re: text to mif conversion  

  By: abhiphull on Feb. 22, 2021, 2:54 a.m.

i have this full program,its work.but then when i want to change the spesific size,it have the error.

name = 'image';
img = imread(strcat(name,'.gif'));
g = im2double(img);
g = g/max(max(g));
rgb = cat(3,g,g,g);
imshow(rgb);
out = ones(480,640);
out(10+(1:460),90+(1:460)) = img;
imshow(out);

x = out';
x = uint8(x();
n = length(x);
y = reshape(x,8,n/8)';
z = y(:,8);
for i=1:7
z = bitor(z,bitshift(y(:,i),8-i));
end

m = 0;
n = 1;
v = z(1);
for i=2:length(z)
if (z(i)~=v)
m = m + 1;
value(m) = v;
runlen(m) = n;
n = 1;
v = z(i);
else
n = n + 1;
end
end
m = m + 1;
value(m) = v;
runlen(m) = n;

fprintf('total runs %d\n',sum(runlen));
fprintf('number of runs %d\n',length(runlen));
nz = length(find(value==0));
nf = length(find(value==255));
fprintf('zeros %d all ones %d\n',nz,nf);

dfv = 255;
fid = fopen(strcat(name,'.mif'),'w');
str = 'WIDTH=8;\nDEPTH=38400;\n\nADDRESSRADIX=HEX;\nDAT ARADIX=HEX;\n\n';
fprintf(fid,str);
str = 'CONTENT BEGIN\n [0000..%04X] : %X;\n';
fprintf(fid,str,sum(runlen)-1,dfv);
n = 0;
for k=1:length(runlen)
if (runlen(k)==1)
str = sprintf(' %04X : %X;\n', n, value(k));
else
str = sprintf(' [%04X..%04X] : %X;\n', n, n+runlen(k)-1, value(k));
end
if (value(k) ~= dfv)
fprintf(fid,str);
end
n = n + runlen(k);
end
fprintf(fid,'END;\n');

fclose(fid);

can you show me how to make the image 16bits width and 4096bits depth.
which part should i alter?
thank you.

Re: text to mif conversion  

  By: abhiphull on Feb. 22, 2021, 2:54 a.m.

Hi Zainab,

data is the vector(array) in Matlab that contains your data to be written into named mif file. It is meant to be signed integer (no fractions) in the code (since data radix is made decimal).

if your data is not scaled then the following statement creates signed integers scaled to 10 bits signed:
data = randn(1,2048); %unscaled floating
data = round((2^9-1) * data/max(abs(data)));

in this case your array length is 2048 to replace "k"