MATLAB, Octave @ 30 October 2010, “No Comments”

Write binary numbers to a file. This is helpful if you want to test your VHDL code with numbers generated by Matlab.

function [] = bin2text(a,hedef)
% bin2text(a,hedef)
% a : the array of binary numbers
% hedef : the target file name
fid = fopen(hedef,'wt');
len = length(a(:,1));
for ii = 1:len
    fprintf(fid, '%s\n',a(ii,:));
end
fclose(fid);
MATLAB, Octave @ 30 October 2010, “No Comments”

Again Matlab does something that is not processable when you convert signed numbers to binary numbers. This function is for this purpose. Compatible with R2007 some small retouches needed for R2009

function [result] = signed2bin(a,len)
% signed2bin(a,len)
% a : an array of the signed decimal number
% len : the length of the bits you want to represent the signed numbers
 
b = dec2bin(a,len);
slas = find(b == '/');
if isempty(slas)
    result = b;
else
    b(slas) = '1';
    result = b;
end
MATLAB, Octave @ 30 October 2010, “No Comments”

As I am doing some signal processing stuff on FPGA I needed a function showing me the signed version of the binary numbers. In simulator it is easy but in Matlab it is not. I hope you find it useful.

function [result] = bin2signed(a)
% bin2signed(a)
% a is a matrix of binary numbers and result is signed representation of a
 
birler = find(a == '1');
sifirlar = find(a == '0');
len = length(a(1,:));
cikan = 2^(len-1)-1;
b = bin2dec(a);
 
cokbuyukler = find(b>cikan);
if isempty(cokbuyukler)
else
    b(cokbuyukler) = b(cokbuyukler)-2^len;
end
 
eksitamlar = find(b == 2^(len-1));
if isempty(eksitamlar)
else
    b(eksitamlar) = -2^(len-1);
end
 
result = b;