- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.Link Copied
10 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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...- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wow, this is great! Thanks a bunch Kaz.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I want to find a solution for converting images to .hex / .mif format. Can i change it by using the converter (please suggest to me) or do I need to make their own code by matlab or another software.?
thank you in advance for any help from members..- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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,'ADDRESS_RADIX=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); *****************- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
thanks kaz...this is really help me...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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\nADDRESS_RADIX=HEX;\nDATA_RADIX=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.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Zainab
If I understood you you want 16bits width and 4096 depth 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=4096;\n'); fprintf(fid,'ADDRESS_RADIX=UNS;\n'); fprintf(fid,'DATA_RADIX=DEC;\n'); fprintf(fid,'CONTENT BEGIN\n'); for k = 1:4096 fprintf(fid,'%i : %i;\n',k-1,data(k)); end fprintf(fid,'END;'); fclose(fid);- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
i'm sorry,kaz.
again, i try your suggestion.but then, i have this error. Error in ==> pingu2 at 10 fprintf(fid,'%i : %i;\n',k-1,data(k)); what is 'data' means here?is it the name of file? thank you.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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"- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I use the Fixed-Point toolbox that is embedded in Matlab to convert data to hex format. With this you can select rounding modes(ceil, floor, fix, convergent, nearest, round), define overflow behavior (wrap, saturate), word length, fraction length, signedness, and data types (fixed, the built-in single and double). Moreover, you can display your object in the equivalent hex, bin, oct and int of the stored bits. I understood that you can build objects with up to 65536-bit length.function generate_mif(filename, data, depth, width, frac, sign)
% data is a vector
fid = fopen(filename, 'w+');
fprintf(fid, 'WIDTH=%d;\n', width);
fprintf(fid, 'DEPTH=%d;\n\n', depth);
fprintf(fid, 'ADDRESS_RADIX=UNS;\n'); % UNS, BIN, HEX, ...
fprintf(fid, 'DATA_RADIX=HEX;\n\n');
fprintf(fid, 'CONTENT BEGIN\n');
fi_data = fi(data, sign, width, frac, 'RoundMode', 'nearest');
for i = 1:depth
fi_temp = fi_data(i);
fprintf(fid, '\t%-4d: %s;\n', i-1, fi_temp.hex);
end
fprintf(fid, 'END;\n');
fclose(fid);
Test the function with this code: filename = 'sin96.mif';
depth = 96;
width = 36;
frac = width-1; % range: [-1,+1)
signed = true;
data = sin(2*pi*0.05*(1:depth));
generate_mif(filename, data, depth, width, frac, signed);
plot(data); grid on;
Explore this toolbox. It's very useful. :)
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page