I want to convert the resulting 8 bit binary bits back to quantization levels

I want to convert 256 level quantized signal to 8 bit bits and I want to convert the resulting 8 bit binary bits back to quantization levels can you help me?

3 Comments

Won't that just give you the original signal back unchanged?
yes I want to get the original signal again after these process
If your signal is of class uint8, like most digital images, it's already quantized into 8 bits (256 gray levels). Are you saying that you want to take the number and get a string with 0's and 1's out, like
binaryString = dec2bin(yourInteger);
OR do you have a floating point number and you still need to know how to do the quantization, like with discretize()?

Sign in to comment.

Answers (3)

s=randi(256,1,1000)-1;%quantized signal
b=dec2bin(s',8)';
b=b(:)';%binary character signal
B=b-'0';%binary array signal
r=reshape(b,8,[])';
ss=bin2dec(r)';%quantized signal back

6 Comments

The signal I quantized consists of numbers between -1 and 1, not an integer. What should I do?
s=2*rand(1,1000)-1;
[~,~,bins]=histcounts(s,linspace(-1,1,257));
bins=bins-1;%this will be the same as above signal 0-255
First of all, thank you very much for your help. I am quantizing an voice signal with 256 levels of lloyd max. Unfortunately, the "dec2bin" code is not giving proper results because the values of my quantized signal are non-integer values between -1 and 1. Is there any other method I can use?
Did you try binning them into 0:255 regions using the above code?
s=2*rand(1,1000)-1;%example signal in -1:1 range
[~,~,bins]=histcounts(s,linspace(-1,1,257));%this puts your signal into bins from 1:256
bins=bins-1;%this will be the same as above signal 0-255 where you can use dec2bin from the above code
x=x'/xmax;%I am assuming that this is your signal stream in the range from -1 to 1 in a row vector
[~,~,bins]=histcounts(x,linspace(-1,1,257));%this will convert signal (-1:1) to (1:256)
bins=bins-1;%this will convert signal to (0:255)
b=dec2bin(bins',8)';%need to perform dec2bin on a column vector, then transpose again
b=b(:)';%binary character array of the signal
B=b-'0';%binary array of signal
bb=num2str(B);%convert binary array of signal back to character array
bb=bb(bb~=' ');%remove spaces in the character array (this will be the same as b)
r=reshape(bb,8,[])';%reshape to 8 bits and transpose
ss=bin2dec(r)';%quantized signal back (0:255), this will be the same as bins
xx=(ss+1)/128 -1;%this will be approximately the same signal stream (same as x)

Sign in to comment.

b=dec2bin(x',8)';
okay, assuming that x is uint8, that code will return 8 rows and however many columns are needed
b=b(:)';%binary character signal
That converts the 8 rows into a column and then flips over to a row vector. A reshape(b, 1, []) might have been faster or clearer perhaps
B=b-'0';%binary array signal
That converts the character row vector into numeric 0 and numeric 1, okay
r=reshape(b,8,[])';
You have gone back to the character row vector... it is not clear why you bothered calculating B ?
Anyhow, you reform the row vector into 8 rows and then transpose, so you are back to 8 columns. Why did you bother going through the (:)' and reshape() process to get back what you already had?
ss=bin2dec(r)';%quantized signal back
That looks like it would restore the values back to double precision, but not to uint8()
... But were the x values uint8 to start with?
myVoice = getaudiodata(recObj);
That gives double precision values in the potential range -1 to +1 -- but it might not use the full range if the sound is not loud.
xmax=max(abs(x));
x=x'/xmax;xmax=1;
dividing by max(abs(x)) rescales the data so that either the upper bound is +1 or the lower bound is -1 (occasionally, both). Effectively if necessary the sound is amplified to get full volume.
So now x is double precision, either going all the way down to -1 or going all the way up to +1, and sometimes -1 to +1 exactly.
And this is the point that we pick up the dec2bin(x',8) . But x is double precision, [-1,+1] . And when you dec2bin(x,8) with x in that range, there are only three possible outcomes:
  • 11111111 -- if x was at all negative
  • 0000001 -- if x was exactly +1
  • 0000000 -- otherwise; in particular if x was exactly 0 or was positive less than 1
You now have a small number of choices:
  1. Before doing the dec2bin() you can rescale x to be an integer in the range 0 to 255; or
  2. Before doing the dec2bin() you can rescale x to be an integer in the range 0 to 65535 and use dec2bin() with 16 instead of 8; or
  3. you can use typecast(x, 'uint8') to decompose the 64 bit double precision values into 8 uint8 bytes; or
  4. you can typecast(single(x), 'uint8') to convert to single precision (since the sound recorded does not have more precision than a single precision can represent) and then decompse the single precision into 4 uint8 bytes.
It depends: how important is it for your purposes that your sound is more than 8 bit?
How do we get the original signal after bin2dec? I will be grateful if you could help me.

4 Comments

x=2*rand(1,10)-1%stream between -1:1
x = 1×10
0.2899 -0.5526 0.7252 0.5963 -0.6531 -0.1549 0.3232 -0.8749 0.2649 0.3748
[~,~,bins]=histcounts(x,linspace(-1,1,257));
bins=bins-1
bins = 1×10
165 57 220 204 44 108 169 16 161 175
b=dec2bin(bins',8)'
b = 8×10 char array
'1011001011' '0011010000' '1100111011' '0110000100' '0111111001' '1011110001' '0000000001' '1100001011'
b=b(:)'
b = '10100101001110011101110011001100001011000110110010101001000100001010000110101111'
B=b-'0'
B = 1×80
1 0 1 0 0 1 0 1 0 0 1 1 1 0 0 1 1 1 0 1 1 1 0 0 1 1 0 0 1 1
bb=num2str(B)
bb = '1 0 1 0 0 1 0 1 0 0 1 1 1 0 0 1 1 1 0 1 1 1 0 0 1 1 0 0 1 1 0 0 0 0 1 0 1 1 0 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 1 1 0 1 0 1 1 1 1'
bb=bb(bb~=' ')
bb = '10100101001110011101110011001100001011000110110010101001000100001010000110101111'
r=reshape(bb,8,[])'
r = 10×8 char array
'10100101' '00111001' '11011100' '11001100' '00101100' '01101100' '10101001' '00010000' '10100001' '10101111'
ss=bin2dec(r)'
ss = 1×10
165 57 220 204 44 108 169 16 161 175
xx=(ss+1)/128 -1
xx = 1×10
0.2969 -0.5469 0.7266 0.6016 -0.6484 -0.1484 0.3281 -0.8672 0.2656 0.3750
"xx=(ss+1)/128 -1" Why do we divide by 128 in this line?
you are converting the 0:256 signal back to -1:1. You want 0 to equal -1, 128 to equal 0, and 256 to equal 1.

Sign in to comment.

Asked:

on 17 Mar 2022

Commented:

on 19 Mar 2022

Community Treasure Hunt

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

Start Hunting!