cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

two's complement conversion

PhilipOakley
5-Regular Member

two's complement conversion

Just having a brain fade....

I'm looking for the quick and easy way of converting an array index (range variable), e.g 0...255, into it's two's complement equivalent i.e. 0...127;-128...-1.

This is for the FFT function (also applicable in many other places) so that I can pre-calculate some frequency spectra.

Alternatively, treat this as a suggestion that there should be a built in two's complement function, almost the same format as mod, whereby twoscomp(a,n) returns mod(a,n), or mod(a,n)-n if mod(a,n)>=n/2.

It would be good to have this as a standard function. Plus suggestions for the best name [two-scomp .v. twos-comp]

Philip

PS Having had to think as I write, I suppose I'll just have to declare a function tc(a,n) as above.

6 REPLIES 6
MikeArmstrong
5-Regular Member
(To:PhilipOakley)

Philip Oakley wrote:

Just having a brain fade....

I'm looking for the quick and easy way of converting an array index (range variable), e.g 0...255, into it's two's complement equivalent i.e. 0...127;-128...-1.

Philip,

Do you mean splitting the range variable into two arrays?

Mike

Philip Oakley wrote:

Just having a brain fade....

I'm looking for the quick and easy way of converting an array index (range variable), e.g 0...255, into it's two's complement equivalent i.e. 0...127;-128...-1.

Philip

.

How about mod(nbr,128)-128*(nbr>127) ? Not really much simpler than writing the function you suggested!!

Alan

MikeArmstrong
5-Regular Member
(To:PhilipOakley)

If you are after 2 single vectors have a look at the attached worksheet.

Mike

PhilipOakley
5-Regular Member
(To:MikeArmstrong)

In fact I was using a range variable (as the input to the function), so using the ravel funmction etc. wasn't quite what I was needing, but it is a reasonable solution.

What it does show is the need/desire for a few more generic functions (such as "tc" - if only it had a good short name),

And for the ability to create a range variable from a vector, i.e it would run through the arbitary sequence of values listed in the vector. Even better, in implementation and documentation terms if one could concatenate a list (vector) and a short range variable, into a larger range variable. The most common being when one needs fft values which have both twos complement and special case end points.

I still think that MathCAD's separation of range variables from MatLab's all inclusive 'vectors' is better, but there are times when I like the compactness of some of MatLab's notation.

Philip

MikeArmstrong
5-Regular Member
(To:PhilipOakley)

Philip Oakley wrote:

In fact I was using a range variable (as the input to the function), so using the ravel funmction etc. wasn't quite what I was needing, but it is a reasonable solution.

Philip,

You can just have ravel within each of the programs if you range variable to stay as a range variable.

Mike

There are several variantes of FFT.

What's more compact than s:=cfft(Y).

You will have to consider the complex Y as well.

Maybe recode the entire FFT +*.DLL

Xavier had explored a faster than the built-in,

his conclusion was minute gain + lot of bother.

Top Tags