uencode -signal processing matlab - matlab

i need to quantize and encode an input signal using matlab so i will use uencode function . The problem is that i am confused about its process , the description says that it quantize and encode the input as integer and then he has displayed an example :
u = -1:0.01:1;
y = uencode(u,3);
plot(u,y,'.')
The output is just integers , can somebody just explain what this integers exactly are ?? and if i need the binary codes of the input u what i must do to get them ?

uencode takes the range of floating point numbers between -1.0 and 1.0, and maps it to the integers from 0 to (2^n)-1.
For example, with n=8, the possible integers are 0 to 255. -1.0 gets mapped to 0, +1.0 gets mapped to 255, and all decimal values in between get mapped to the closest integer.
In the code example you gave, n=3, so it is mapping to the integers 0 to 7. The plot shows horizontal lines because with so few integers available to map to, many floating point values map to the same integer.
To convert a base 10 integer to a base 2 binary string, use the function dec2bin.
>> dec2bin(5)
ans =
101
>> dec2bin(17)
ans =
10001
If you wanting leading zeros, say so that they are always 8 bits long, use the minimum length as a second argument:
>> dec2bin(5, 8)
ans =
00000101

Related

MATLAB: playing 16-bit audio

I have a matrix (n,16) filled with audio samples of 16 bits each.
So the matrix could look like:
[0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1; 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1]
and so on. How can i actually play this as sound? sound() is using double floating points (from -1 to 1 values). If i could just convert it to those values, I guess it would work, but im not sure. Any suggestions?
#AnderBiguri pretty much nailed it. Take your array, convert it to signed double, normalize and play it.
As you have a n x 16 matrix, where each row is a sample and each column is a bit for that number, first we need to convert that matrix into double format. We can use bin2dec to help us to do that. However, bin2dec takes in a string. As such, we can convert our matrix into a string array like so. Let's assume that inSound contains your sound matrix that you specified before.
I will also assume that your bits are in Big-Endian format, so the most significant bit is the left most bit, following down to the right most bit.
According to your comments, the numbers are signed 1's compliment. This means that should the most significant bit be 1, we simply invert the entire number. As such, we can figure out which rows are negative by checking the entire first column to see if there is a 1 or 0. We find the rows with 1 and simply take 1 and subtract every single element in the array. As such:
checkSign = inSound(:,1) == 1;
inSound(checkSign,:) = 1 - inSound(checkSign,1);
Therefore, when we convert our number to decimal, we simply find those rows again that should be negative and simply negate them. Let's convert our numbers first.
inSoundString = char(inSound + '0');
char converts each number in a matrix or vector into its string representation, assuming that the input into char is an ASCII code. As such, when we add with 0, we are adding each number in inSound with the numerical ASCII code that is representative of what 0 is in ASCII. This is our baseline to start with. After that, inSound will add either 0 or 1 to this numerical code so that when we convert this whole matrix into a string, we will finally get the string representation of those numbers.
Now convert the binary strings into actual numbers:
outSoundDec = bin2dec(inSoundString);
This will take each row and convert it into its decimal equivalent. Now, if your bits are in Little-Endian, you can use swapbytes to flip the bits so that you can transform the bits to Big-Endian before proceeding. Now that we have converted our numbers, we need to negate those rows that were supposed to be negative.
As such:
outSoundDec(checkSign) = -outSoundDec(checkSign);
Now we need to normalize this so that the range is between -1 to 1. Given that you have specified that the range is between [-32768, 32767], we simply divide all of our numbers by 32768. Therefore:
outSound = outSoundDec / 32768;
The above code will now normalize your signal so that it goes from -1 to 1. You can use sound(outSound, Fs); to play your sound where Fs is the sampling frequency your signal was obtained at.
Good luck!

Results not correct when using diff function in Matlab

I'm using the diff Matlab function to get the difference between two successive values. And as shown here in this vector nz in this link as shown in nz the difference between col 261 and 260 is -1342 but when I use this script the result of difference between this coloumns don't appear in the result dnz. So if anyone could advise why this is not working?
This is my attempt:
load('nz.mat');
dnz = diff(nz);
If you type class(nz) you see that your data is unit16. And MATLAB saturates the results when dealing with integer values, i.e. since 0 - 1342 is lower than zero (the smallest value in uint16) it returns zero:
>> dnz=diff(nz);
>> dnz(260)
ans =
0
If you convert it to a class that can accomodate -1342 like int16 you get
>> dnz = diff(int16(nz));
>> dnz(260)
ans =
-1342

Convert matrix elements from decimal to binary

I have a matrix with decimal elements. I want to convert every element in this matrix to binary with a resolution of 8. What function should I use?
Use dec2bin:
>> dec2bin([10; 100; 255], 8)
ans =
00001010
01100100
11111111
The second parameter tells it to use at least N bits (where N==8 here).

change dec2bin in matlab to get a logical vector

I'm looking for a quick way to get a variat of dec2bin in Matlab such that it'll return a logical variable vector. For example given a number n=8 the output will be [1,0,0,0].
How can I do that?
The simplest way is to simply explode the binary representation returned by dec2bin (which is already a string!):
dec2bin(n) == '1'
For n = 8 this returns a logical vector
1 0 0 0
This will also work if n is a vector of numbers.
Since string '1' is char(49) and string '0' is char(48) you can use:
bin = dec2bin(dec) - 48;
This will output the result as an array of doubles, since you are performing an arithmetic operation over an array (the string coming from «dec2bin» is considered an array in Matlab)
If you want to convert very high integers to binary, I recommend using this code:
bin = mod(floor(dec.*2.^-(floor(log2(dec)):-1:0)),2);
In order to convert binary vector to decimal:
dec = sum(bin.*2.^((length(bin)-1):-1:0));
Don't use dec2bin, use bitget instead:
bitget(n, fix(log2(n)) + 1:-1:1)
P.S:
If you have an array of numbers and you want its binary representation as matrix of bits, you might want to take a look at this related question.
Example
n = 8
b = bitget(n, fix(log2(n)) + 1:-1:1)
This results in:
b =
1 0 0 0

Performing Bit modification on Floating point numbers in Matlab

I'm working in Matlab using Non-negative Matrix factorization to decompose a matrix into two factors. Using this I get from A two double precision floating point matrices, B and C.
sample results are
B(1,1) = 0.118
C(1,1) = 112.035
I am now trying to modify specific bits within these values but using the bitset function on either values I get an error because bitset requires unsigned integers.
I have also tried using dec2bin function, which I assumed would convert decimals to binary but it returns '0' for B(1,1).
Does anyone know of any way to deal with floats at bit level without losing precision?
You should look into the typecast and bitset functions. (Doc here and here respectively). That lets you do stuff like
xb = typecast( 1.0, 'uint64' );
xb = bitset( xb, 10, 1 );
typecast( xb, 'double' );
The num2hex and hex2num functions are your friends. (Though not necessarily very good friends; hexadecimal strings aren't the best imaginable form for working on binary floating-point numbers. You could split them into, say, 8-nybble chunks and convert each to an integer.)
From the MATLAB docs:
num2hex([1 0 0.1 -pi Inf NaN])
returns
ans =
3ff0000000000000
0000000000000000
3fb999999999999a
c00921fb54442d18
7ff0000000000000
fff8000000000000
and
num2hex(single([1 0 0.1 -pi Inf NaN]))
returns
ans =
3f800000
00000000
3dcccccd
c0490fdb
7f800000
ffc00000