[MATRIX-SIG] [Q] casting double to char in NumPy1.0b3

Harald Singer singer@itl.atr.co.jp
Tue, 02 Dec 1997 13:28:42 +0900


Hi,

I am trying to map an array of doubles to an array of char (values
between 0 and 63) as entries into a colormap. In an older version
of NumPy this worked without any problems but in the latest version
I get:

>>> from Numeric import *
>>> x = array([1.,2.,3.])
>>> y = array(x,Int8)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: Array can not be safely cast to required type

>>> x = array([1,2,3])
>>> y = array(x,Float64)
>>> y = array(x,Float32)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
TypeError: Array can not be safely cast to required type

So what is an efficient way of doing this type of operation?

Up to now, I have been doing something like

    ScaleOffset = min(yy.flat)
    ScaleFactor = 63.0/(max(yy.flat)-ScaleOffset)
    yyy = array(((yy-ScaleOffset)*ScaleFactor)[:,:fftorder/2],Int8)

where yy is a 2-dimensional array of doubles (time, frequency bins)
and yyy is the same thing but mapped to 8bit values between 0 and 63,
which I use as entries into a colormap.

BTW, the moment I get a solution, I will post my spectrogram display
python script to the image-sig and matrix-sig mailing lists.

/harry

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________