-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_DSP_modules.py
More file actions
167 lines (117 loc) · 4.8 KB
/
Copy pathmy_DSP_modules.py
File metadata and controls
167 lines (117 loc) · 4.8 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import numpy as NP
import my_operations as OPS
def FT1D(inp, ax=-1, use_real=False, shift=False, verbose=True):
"""
---------------------------------------------------------------------
Compute FFT from Numpy.
Inputs:
inp: Input data (vector or array) to be Fourier transformed
Keyword Inputs:
ax: Axis (scalar integer) over which FFT is performed.
Default = -1 (last axis)
use_real: [Boolean scalar] If True, compute only the positive
frequency components using the real part of the data
oututs:
fftout: FFT of input data over the specified axes
-------------------------------------------------------------------
"""
try:
inp
except NameError:
raise NameError('inp not defined. Aborting FT1D().')
if not isinstance(inp, NP.ndarray): # type(inp) is numpy.ndarray
raise TypeError('Input array should be Numpy array data type')
if use_real:
inp = NP.real(inp)
if verbose:
print "Opted for FFT of real data. Hence performing numpy.rfft()."
print "numpy.rfft() returns only positive frequencies."
fftout = NP.fft.rfft(inp, axis=ax)
else:
fftout = NP.fft.fft(inp, axis=ax)
if shift:
fftout = NP.fft.fftshift(fftout, axes=ax)
return fftout
def spectral_axis(length, delx=1.0, shift=False, use_real=False):
"""
----------------------------------------------------------------
Compute spectral axis in the FFT
Inputs:
length: Length of vector to be Fourier transformed
Keyword Inputs:
delx: x-axis interval, used only in case of 1D inp.
Default = 1.0
shift: [Boolean scalar] True => Shift to center of frequencies
use_real: [Boolean scalar] True => Compute only positive
frequencies using numpy.fft.rfftfreq()
Output:
spaxis: Discrete spectral axis in the output FFT
---------------------------------------------------------------
"""
# try:
# size(length) == 1 and isinstance(length, int)
# print type(length)
# except:
# print "length has to be a scalar positive integer."
# print "Aborted execution in my_DSP_modules.frequencies()"
# SYS.exit(1) # Abort execution
if use_real:
spaxis = NP.fft.rfftfreq(length, d=delx)
else:
spaxis = NP.fft.fftfreq(length, d=delx)
if shift:
spaxis = NP.fft.fftshift(spaxis)
return spaxis
def rfft_append(inp, axis=0):
"""
------------------------------------------------------------------
Compute the negative frequency left out by numpy.rfft()
and append in the right order to the output from numpy.rfft().
Input:
inp Input data of any dimensions to which negative frequency
components have to be appended.
Keyword Input:
axis [scalar] Axis along which negative frequency components
are to be appended. It has to be a scalar in the range
0 to Ndim-1 where Ndim is the number of axes in the data.
Output:
Appended data along the axis specified.
-------------------------------------------------------------------
"""
try:
inp
except NameError:
raise NameError('inp undefined. Aborting rfft_append()')
if not isinstance(inp, NP.ndarray):
raise TypeError('inp should be Numpy array data type.')
if isinstance(axis, (list, tuple, str)):
raise TypeError('axis should be a scalar integer in the range 0 to Ndim-1')
axis = int(axis)
shp = NP.shape(inp)
ndim = len(shp)
if (axis < 0) or (axis >= ndim):
raise ValueError("Input data does not contain the axis specified. Aborted execution in reverse()")
if shp[axis] == 1:
return inp
return NP.append(inp, NP.conj(OPS.reverse(inp, axis=axis, ind_range=[1,shp[axis]-2])), axis=axis)
def rfftfreq_append(rfft_freqs):
"""
------------------------------------------------------------
Compute the negative frequencies for the output of
numpy.rfftfreq() and rearrange the frequencies in the correct
order.
Input:
rfft_freqs [Vector] Positive frequencies
Output:
Positive and negative frequencies computed from numpy.rfftfreq()
made equal to the output of numpy.fftfreq()
------------------------------------------------------------
"""
try:
rfft_freqs
except NameError:
raise NameError('Input rfft_freqs not specified. Aborting rfftfreq_append()')
if not isinstance(rfft_freqs, (list, NP.ndarray)):
raise TypeError('Input rfft_freqs should be a list or a 1D Numpy array')
rfft_freqs = NP.asarray(rfft_freqs)
return NP.append(rfft_freqs[:-1],-rfft_freqs[-1:0:-1],axis=0)