31 Replies - 1747 Views - Last Post: 03 June 2012 - 06:47 AM
#16
Re: Complex number, imaginary part usage question
Posted 13 March 2012 - 12:46 PM
This goes to show you though that if you post a thread on here for help, after you make changes and still need more help, you should post your updated code so we can see what's going on.
#17
Re: Complex number, imaginary part usage question
Posted 13 March 2012 - 12:49 PM
#18
Re: Complex number, imaginary part usage question
Posted 13 March 2012 - 12:51 PM
#19
Re: Complex number, imaginary part usage question
Posted 13 March 2012 - 12:55 PM
#20
Re: Complex number, imaginary part usage question
Posted 13 March 2012 - 12:57 PM
I totally agree with r.stiltskin
This post has been edited by erkant: 13 March 2012 - 01:03 PM
#21
Re: Complex number, imaginary part usage question
Posted 17 March 2012 - 10:43 AM
short array[10];
int i;
for(i = 0; i < 10; i++)
{
if(i%2 == 0)
(complex)array[i] = +I;
else (complex)array[i] = -I;
}
Will the above code work? And can I also cast it at the function call, like:
short array[10]; function((complex)array);
Thank in advance.
This post has been edited by erkant: 17 March 2012 - 10:46 AM
#22
Re: Complex number, imaginary part usage question
Posted 17 March 2012 - 10:53 AM
If you cast the pointer to some other type it would be possible to overwrite the array with a different type of data, but you would essentially be trashing that memory -- afterwards there would be no way of knowing what's stored there.
A better approach would be to create an array of complex, since a complex number can have a 0 imaginary part. For the "shorts", just declare them as complex numbers with 0I.
Alternatively, you could create a union of a short with a complex, and declare an array of those unions, but I think that would be harder to deal with later.
#23
Re: Complex number, imaginary part usage question
Posted 17 March 2012 - 11:05 AM
void QPSKmapping(short* input, short* output, int nbits)
{
int i;
for(i = 0; i < nbits; i += 2)
{
if(input[i] == 0 && input[i+1] == 0)
{
output[i] = -1;
output[i+1] = -I;
}
else if(input[i] == 0 && input[i+1] == 1)
{
output[i] = -1;
output[i+1] = +I;
}
else if(input[i] == 1 && input[i+1] == 0)
{
output[i] = 1;
output[i+1] = -I;
}
else
{
output[i] = 1;
output[i+1] = +I;
}
}
}
This post has been edited by erkant: 17 March 2012 - 11:16 AM
#24
Re: Complex number, imaginary part usage question
Posted 17 March 2012 - 11:23 AM
Furthermore, even if you could do this, how would the other functions that try to use the array know which bytes are part of a short and which bytes are part of a complex?
I think the easiest way is to convert everything into complex, work with the data in that form, and then convert back to short at the end.
#25
Re: Complex number, imaginary part usage question
Posted 18 March 2012 - 01:50 PM
complex double test;
I want to ask whether a complex type can be used with another type apart from double, like complex int, or can a complex type be created alone, just by writing:
complex variable;
And one more question, is the array declaration with complex type the same as with the other types?
This post has been edited by erkant: 18 March 2012 - 01:52 PM
#26
Re: Complex number, imaginary part usage question
Posted 18 March 2012 - 02:15 PM
complex double c_arr[SIZE];
#27
Re: Complex number, imaginary part usage question
Posted 26 March 2012 - 01:42 PM
#28
Re: Complex number, imaginary part usage question
Posted 26 March 2012 - 03:27 PM
3.0I is 3.0 times I. I3.0 will be parsed as a variable name, I3, followed by a numerical constant .0, which will produce a compiler error assuming that no variable named "I3" has been declared, and another compiler error because the constant .0 is just "floating" there and the compiler doesn't know what to do with it.
And PS, aren't we getting a little lazy here? How hard would it be for you to figure this out, or test it, yourself?
This post has been edited by r.stiltskin: 26 March 2012 - 03:27 PM
#29
Re: Complex number, imaginary part usage question
Posted 27 March 2012 - 02:27 PM
error: lvalue required as left operand of assignment
#include <stdio.h>
#include <complex.h>
int main()
{
complex double cplx3 = 4.0 + 2.0j;
printf("%.0f %c %.0fj\n", creal(cplx3), cimag(cplx3) >= 0 ? '+' : '-', cimag(cplx3) >= 0 ? cimag(cplx3) : -cimag(cplx3));
if(cimag(cplx3) >= 0)
{
cimag(cplx3) = -cimag(cplx3);
}
printf("%.0f %c %.0fj\n", creal(cplx3), cimag(cplx3) >= 0 ? '+' : '-', cimag(cplx3) >= 0 ? cimag(cplx3) : -cimag(cplx3));
return 0;
}
I can change this one at the printing and make it change the sign:
cimag(cplx3) >= 0 ? cimag(cplx3) : -cimag(cplx3)
But I need some method to change all the imaginary part signs of the numbers in the array.
This post has been edited by erkant: 27 March 2012 - 02:37 PM
#30
Re: Complex number, imaginary part usage question
Posted 27 March 2012 - 03:13 PM
I gave you this link before: everything you need to work with the complex type is described on this page and the "next" page.
It tells you that the functions creal(), cimag(), and so on, return certain values associated with the complex number passed as an argument. They don't allow you to modify the argument. In colloquial terms, they are "getters", not "setters".
For assignment, you have the usual operators =, +=, -=, and so on.
Now, suppose you have an ordinary int n and n has any arbitrary positive value. What amount can you add to (or subtract from) n that will change n to -n?
And finally, remember that there is a function cimag() that lets you "get" the imaginary part of a complex number.
|
|

New Topic/Question
Reply




MultiQuote


|