I want to convert the resulting 8 bit binary bits back to quantization levels
14 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Image Analyst
il 18 Mar 2022
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()?
Risposte (3)
David Hill
il 17 Mar 2022
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 Commenti
David Hill
il 18 Mar 2022
Modificato: David Hill
il 18 Mar 2022
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)
Walter Roberson
il 18 Mar 2022
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:
- Before doing the dec2bin() you can rescale x to be an integer in the range 0 to 255; or
- 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
- you can use typecast(x, 'uint8') to decompose the 64 bit double precision values into 8 uint8 bytes; or
- 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?
Vecihi He
il 18 Mar 2022
Modificato: Vecihi He
il 18 Mar 2022
How do we get the original signal after bin2dec? I will be grateful if you could help me.
4 Commenti
David Hill
il 19 Mar 2022
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.
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!