Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to convert floating point to bits in MATLAB?

Status
Not open for further replies.

testing test

Member level 3
Joined
Mar 3, 2010
Messages
65
Helped
5
Reputation
10
Reaction score
3
Trophy points
1,288
Activity points
1,656
Hello,

I am reading a .wav file using wavread command and then selecting the first column to select the left channel only. But the values of the channels are in floating point like 0.000349 but I need these values to be in bits. How to convert these values to bits? Is there any built-in command or any algorithm that you developed?

Thank you.

Code:
>> x=wavread('new2.wav');
>> y=x(:,1);
 

I do not have a .wav file with me but according to matlab help:
Code:
[Y,FS,NBITS]=WAVREAD(FILE) returns the sample rate (FS) in Hertz
and the number of bits per sample (NBITS) used to encode the
data in the file.
You just need to multiply your vector by 2^(NBITS-1)
 

JoannesPaulus said:
I do not have a .wav file with me but according to matlab help:
Code:
[Y,FS,NBITS]=WAVREAD(FILE) returns the sample rate (FS) in Hertz
and the number of bits per sample (NBITS) used to encode the
data in the file.
You just need to multiply your vector by 2^(NBITS-1)
What is NBits here? Also, if i give you a wav file, can you give me a sample code?
 

NBITS is one of the 3 outputs of the wavread function. You need to write the following code:
Code:
[x,fs,nbits]=wavread('new2.wav');
y=floor(2^(nbits-1)*x(:,1));
 

It just converts to positive and negative decimal numbers.
 

Oh... now I see what you mean... Add the following:
Code:
y=dec2bin(floor(2^(nbits-1)*x(:,1))+2^(nbits-2));
 
JoannesPaulus said:
Oh... now I see what you mean... Add the following:
Code:
y=dec2bin(floor(2^(nbits-1)*x(:,1))+2^(nbits-2));

That works for me. Much appreciated.:D
 

JoannesPaulus said:
Oh... now I see what you mean... Add the following:
Code:
y=dec2bin(floor(2^(nbits-1)*x(:,1))+2^(nbits-2));
Thanks. It was helpful.
 

Can you please elaborate what you are trying to do in this line?
 

There is a "quantize" function available in MATLAB which will directly convert the floating point numbers into fixed-point number of desired word length.
 

There is a "quantize" function available in MATLAB which will directly convert the floating point numbers into fixed-point number of desired word length.

True,
use the following Matlab instructions

q= quantizer[x,y] % set the Fixed point format to X bit with Y decimal digits
h=num2hex(q, number) % h is the string which represents the hex value in FP

example
q = quantizer ([16,15]); %FP 16 bit Q15 format
h=num2hex(q,-0.5);

returns the string h = c000

regards
 

    V

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top