How to convert signed integer (from -8 to 7) into 4 bit signed binary

I wanted to convert a vector (approximately 270k entries) of integers ranged from -8 to 7 into 4 bit signed binary, and I used
dec2bin((typecast(int8(Data), 'uint8')), 8)
to find the 8 bit signed binary for my data. However I need to take either the upper 4 bits of the 8 bits, or convert them into 4 bit signed binary directly from the integers as they can be expressed as 2's complement 4 bit signed binary. Could anyone teach me how to do that?
My current situation: I have all of them as 8 bit signed binary, but some entries only appear as '10', '1', '11', etc. i.e. not 8 bits even though I tried to use
dec2bin(d, n)
So when I tried to convert the numbers to strings and take the first 4 bits with
num2str
all I would get are spaces.
Any help would be greatly appreciated!!

Risposte (1)

int2bin4 = dec2bin([15:-1:8,0:7],4) - '0'; %or leave out the '0' if you want character output;
translated_vector = int2bin4(YourVector(:) + 9, :);

4 Commenti

Thank you, could you explain what "int2bin4" does? I typed the first row and it gave me the signed binary of -8 to 7, and I tried to replace "YourVector" with my data it gave me the "Subscript indices must either be real positive integers or logicals." error.
What is min() of your data? Your question says that it is -8 but the error message implies that it might be more negative.
Or possibly there are non-integer values or NaN or inf values:
nnz(isnan(YourVector))
nnz(isinf(YourVector))
nnz(YourVector ~= floor(YourVector))
All three of those should come out as 0 if the data is in the range you indicated.
All three gave me 0, but I just realized the max and min are 15 and -13 respectively... so there must be a mistake when I'm normalizing the decimal value from -1 to 1 to integer values.
My goal is to take that vector of about 270k elements generated by Audacity that range from -1 to 1 (which should be 16 bits) and convert (or normalize) them into 4 bit signed binary values directly, or 8/16bit and take the 4 most significant bits. Do you have any suggestions for to me to approach this? My original command to get (what I thought would be) -8 to 7 integers was
Data_int = round(Data_original.*(2^4-1)./max(Data_original));
but apparently the max/min elements turn out to be more than what I expected. Thank you so much!!
Data_int = int2bin4(floor(Data_original * 15/2) + 9, :) ;
int2bin4 is acting as a lookup table to provide an efficient way to do the data conversion

Accedi per commentare.

Richiesto:

il 27 Nov 2017

Commentato:

il 27 Nov 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by