5

I have the following script in Python. I am calculating the Fourier Transform of an array. When I want to plot the results (Fourier transform) I am using the absolute value of that calculation. However, I do not know how the absolute value of complex numbers is being produced. Does anyone know how it calculates? I need this to reproduce in Java.

import numpy as np
import matplotlib.pyplot as plt
from numpy import fft

inp = [1,2,3,4]

res = fft.fft(inp)

print(res[1]) # returns (-2+2j) complex number

print(np.abs(res[1])) # returns 2.8284271247461903
SleuthEye
  • 14,379
  • 2
  • 32
  • 61
jAdex
  • 472
  • 1
  • 5
  • 15

4 Answers4

10

np.abs gives magnitude of complex number i.e. sqrt(a^2 + b^2) in your case it's sqrt(8).

https://numpy.org/doc/stable/reference/generated/numpy.absolute.html

Ankush
  • 470
  • 4
  • 9
8
sqrt(Re(z)**2 + Im(z)**2)

for z = a + ib this becomes:

sqrt(a*a + b*b)

It's just the euclidean norm. You have to sum the square of real part and imaginary part (without the i) and do the sqrt of it.

absolute of complex number

https://www.varsitytutors.com/hotmath/hotmath_help/topics/absolute-value-complex-number

Lukr
  • 659
  • 3
  • 14
3

From numpy.absolute(arr, out = None, ufunc ‘absolute’) documentation:

This mathematical function helps user to calculate absolute value of each element. 

For a complex number a+ib, the absolute value is sqrt(a^2 + b^2).

Mahdi
  • 3,188
  • 2
  • 20
  • 33
Leeds Leeds
  • 51
  • 13
0

For complex valued pairs, a+ib, you can consider using the java Math static method hypot:

Math.hypot(a, b)

The method is an implementation of the Pythagorean theorem, sqrt(a*a + b*b) but additionally provides underflow and overflow protection.

ctpenrose
  • 1,467
  • 2
  • 18
  • 28