Reputation: 23
Is there a way to compute efficiently
where x and y are complex vectors ?
My goal is to reconstruct the signal y, but I can only do it up to a complex number of modulus 1, and so this quantity would be the reconstruction error.
I have looked a bit on the internet and found nothing, the best I can think about is to take random complex numbers of modulus 1 and take the minimum among these values.
Upvotes: -1
Views: 82
Reputation: 11297
This actually looks like a relatively straightforward math (not Python) question.
I apologize. Other forums let you use latex. This one doesn't seem to. I'll do my best since I don't know how to enter formulas.
In polar coordinates, let x = (r1, theta1)
and y = (r2, theta2).
The possible values of cx
are just points on the zero-centered unit circle of radius r1
. You want the point on this circle closest to $y$, which clearly happens to be the point with the same angle as y
, namely (r1, theta2)
. So you want c
to be (1, theta2-theta1)
in polar coordinates.
In Python:
import cmath
_, theta1 = cmath.polar(x)
_, theta2 = cmath.polar(y)
c = cmath.rect(1, theta2 - theta1)
numpy has similar functions.
Actually, this can be simplified even further. c
is just the unit vector of y/x
. So this can all be done without polar coordinates.
t = y/x
c = t / abs(t)
or if you're playing Python golf:
c=(t:=y/x)/abs(t)
This works with both complex numbers and numpy arrays.
Upvotes: 0
Reputation: 6745
Are x and y just complex numbers, or are they whole vectors of values? This answer assumes that they are vectors of values with multiple complex components.
Does this work? Check it against your random trials. (Working at the bottom).
import numpy as np
import math
def minimiseNorm( x, y ):
a, b = np.real( x ), np.imag( x )
p, q = np.real( y ), np.imag( y )
S1 = np.sum( a * p + b * q )
S2 = np.sum( a * q - b * p )
A = math.sqrt( S1 ** 2 + S2 ** 2 )
phi = math.atan2( S2, S1 )
minNorm = math.sqrt( np.sum( a * a + b * b + p * p + q * q ) - 2 * A )
c = math.cos( phi ) + 1j * math.sin( phi )
return minNorm, c
x = np.array( [ 1+2j, 3+ 4j, 5+ 6j ] )
y = np.array( [ 7+8j, 9+10j, 11+12j ] )
N, c = minimiseNorm( x, y )
print( "Minimum norm is", N, " occurring at c=", c )
Somebody had better check my algebra.
Upvotes: 1