Forum Discussion
Altera_Forum
Honored Contributor
15 years agotext to mif conversion
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 fra...
Altera_Forum
Honored Contributor
15 years agoHi,
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. :)