[Speex-dev] converting unsigned short sample to signed short sample

Blaise Potard bpotard at gmail.com
Thu Mar 15 03:41:56 PDT 2012


Hi Mashal,

2012/3/14 Mashal al-shboul <shboul8989 at yahoo.com>:
> Hi Blaise Potard,
> Thank you for help.
> Actually,i am sure that my samples are unsigned  , specifically, of type
> uint16_t which is unsigned 2-bytes, these samples are provided by an audio
> driver for an audio subsystem (microphone ADC...) in a wireless sensor
> node(intelmote2 ), following is the function that captures the samples and
> returns them:
>
>             /* reads one audio sample from the I2S RXFIFO buffer */
>         uint16_t WmReadSample()
>        {
>           while( (SASR0&2)==0 );
>            return (SADR)&0xFFFF;
>        }
>
> you can notice its return type uint16_t which i must adapt to signed 2-bytes
> !

Thanks, I was not doubting that the samples returned by the "driver"
are unsigned short; there could be several reasons for that, the first
one being that they are easier to manipulate at a lower level. At the
level of the driver, what they represent does not really matter; it
really matters to the final user, that wants to play the audio. What I
mean is that even though the data returned from the "driver" appears
to be unsigned, it could perfectly represent some signed sample. Have
you ever tried playing the audio data returned from your controller?
You should know if they are signed or unsigned samples then.

By the way, I am not really familiar with audio drivers, but your
"WmReadSample" looks a bit strange, as it seems that it does active
waiting for an interrupt (or something), then return the lower 16 bits
of the SADR register.

As I said, there are two likely reasons these samples are unsigned...
Have you read my email? Have you tried the subtraction that several
people recommended? If they really are unsigned in the normal way,
then that should work just fine.

>
> i hope i explained it.
> Best Regards,
> Mash'al Shboul
>
> ________________________________
> From: Blaise Potard <bpotard at gmail.com>
> To: Mashal al-shboul <shboul8989 at yahoo.com>
> Sent: Wednesday, March 14, 2012 5:32 PM
>
> Subject: Re: [Speex-dev] converting unsigned short sample to signed short
> sample
>
> Hi,
>
> 2012/3/14 Mashal al-shboul <shboul8989 at yahoo.com>:
>> Thanks for replying.
>> actually 2^15 in decimal is the same as 0x8000 in hex.
>> the idea is clear for me that i should scale down each unsigned sample by
>> 0x8000 for becoming signed. I hope that will work correctly as input raw
>> samples to Speex codec. do you think so ?
>
> What Steve and the rest are trying to explain, is that an input "raw"
> sample is usually a signed short, as it is the most common and
> sensible representation for a sound wave. If it is not a signed short,
> then there are two likely explanations:
> 1) the programmer (or you) made a mistake, and the signal is actually
> a signed short that was cast as an unsigned short by mistake; then
> casting to signed short is what you should do.
> 2) the wave signal has actually been shifted to fit the unsigned
> short range; then shifting the samples down (using the subtraction
> mentioned above, not "scaling") would solve the issue.
> But it is also possible that your "raw samples" represent something
> entirely different; we cannot guess what they represent for you. Read
> the documentation of your recorder, and do the appropriate conversion
> (which is most likely explained at great length, if your recorded
> actually gives you some exotic samples).
>
> Regards,
> Blaise
>
>>
>> Regards,
>> Mash'al
>>
>> ________________________________
>> From: Steve Checkoway <s at pahtak.org>
>> To: Mashal al-shboul <shboul8989 at yahoo.com>
>> Cc: Arthurc <arthurc99 at yahoo.com>; "speex-dev at xiph.org"
>> <speex-dev at xiph.org>
>> Sent: Wednesday, March 14, 2012 9:01 AM
>>
>> Subject: Re: [Speex-dev] converting unsigned short sample to signed short
>> sample
>>
>> I don't understand why you're subtracting 2^15 - 1.  Assuming you really
>> do
>> have unsigned samples (which is very odd) that have 0x8000 as the mid
>> point
>> of the range, then you need to subtract 0x8000, as I believe I pointed out
>> already.
>>
>> By mid point, I mean if you have a pure tone (i.e., a sine wave) that has
>> unsigned range 0 to 0xffff, then the mid point is 0x8000. The mid point
>> should be translated to zero.
>>
>> If your samples are represented some other way, you have to tell us.
>>
>>
>> --
>> Stephen Checkoway
>>
>>
>>
>> On Mar 13, 2012, at 23:28, Mashal al-shboul <shboul8989 at yahoo.com> wrote:
>>
>> Thanks a lot for replying,
>> The question now is , after subtracting 32767 from each unsigned sample,
>> will the new signed samples represent the same original voice ?
>>      For explanation, assume "Hi" is said in 8000 unsigned sample ,if i
>> subtract 32767 from each sample and play the resulting 8000 sample as
>> signed
>> PCM samples,will they be "Hi" ?!
>>
>> Forgive me about my questions , but it's important for me and i am new to
>> the stuff
>> Best Regards,
>> Mash'al
>>
>> ________________________________
>> From: Arthurc <arthurc99 at yahoo.com>
>> To: Mashal al-shboul <shboul8989 at yahoo.com>
>> Sent: Wednesday, March 14, 2012 5:58 AM
>> Subject: Re: [Speex-dev] converting unsigned short sample to signed short
>> sample
>>
>> I assumed that you have unsigned input from your sound card / codec, etc.
>> Actually, it is unlikely in today's hardware platform for audio
>> application.
>>
>> If that is really the case, you can just do..
>>
>> short output = ((long) input) - 32767
>>
>> Well, maybe it is possible to do some bit manipulation to achieve the same
>> thing.
>>
>> Regards
>> Arthur
>>
>> On 2012年3月14日, at 上午11:21, Mashal al-shboul <shboul8989 at yahoo.com> wrote:
>>
>>     Hi All,
>> I would like to get your help in my problem,
>>
>> we know that Speex codec takes samples of type short (signed), range:-2^15
>> to2^15 -1 , my problem is that i have samples of type
>> unsigned short ,range: 0 to 2^16 -1. so, what should i do in order to
>> apply
>> speex on my unsigned samples ?.
>>      There is  a proposed solution to subtract 2^15-1 from each unsigned
>> short to get it signed short, my question: is this method safe in keeping
>> the samples meaningful for speex's processing algorithms?.
>> i hope i receive help from you. Thanks in advance
>>
>> Regards,
>> Mash'al
>>
>> _______________________________________________
>> Speex-dev mailing list
>> Speex-dev at xiph.org
>> http://lists.xiph.org/mailman/listinfo/speex-dev
>>
>>
>>
>> _______________________________________________
>> Speex-dev mailing list
>> Speex-dev at xiph.org
>> http://lists.xiph.org/mailman/listinfo/speex-dev
>>
>>
>>
>>
>> _______________________________________________
>> Speex-dev mailing list
>> Speex-dev at xiph.org
>> http://lists.xiph.org/mailman/listinfo/speex-dev
>>
>
>


More information about the Speex-dev mailing list