# bin2signed function for MATLAB (alternatively Octave)
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; |