-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcomplex.py
72 lines (61 loc) · 1.63 KB
/
fcomplex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from copy import deepcopy
import numpy as np
def addToPhase(logger, data, p):
'''
Add to the phase of [data], and reform as a complex number.
'''
mag = getAmplitude(logger, data)
phase = getPhase(logger, data) + p
re = mag * np.cos(phase)
im = mag * np.sin(phase)
data = re + 1j * im
return data
def getAmplitude(logger, data, power=False, shift=False, normalise=False,
scale="linear"):
'''
Get the amplitude of [data], or convert to a variety of formats.
Returns a data array of the same shape as [data].
'''
d = deepcopy(np.abs(data))
if power:
d = d**2
if scale != "linear":
if scale == "log":
d = np.log10(d)
else:
logger.warning(" Unrecognised scale keyword, assuming linear")
if normalise:
d = (d-np.min(d))/(np.max(d)-np.min(d))
if shift:
d = np.fft.fftshift(d)
return d
def getPhase(logger, data, shift=False):
'''
Get the phase of [data].
Returns a data array of the same shape as [data].
'''
if shift:
return np.angle(np.fft.fftshift(data))
return np.angle(data)
def getRealComponent(logger, data, shift=False, normalise=False):
'''
Returns the real component of [data].
'''
d = deepcopy(data)
if shift:
d = np.fft.fftshift(d)
re = np.real(d)
if normalise:
re = (re-np.min(re))/(np.max(re)-np.min(re))
return re
def getImagComponent(logger, data, shift=False, normalise=False):
'''
Returns the imaginary component of [data].
'''
d = deepcopy(data)
if shift:
d = np.fft.fftshift(d)
im = np.imag(d)
if normalise:
im = (im-np.min(im))/(np.max(im)-np.min(im))
return im