/
IDHT.py
415 lines (312 loc) · 15.9 KB
/
IDHT.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""
This module generates Inverse Discrete Hartley Transform matrix (IDHT). |br|
Frequencies represented by the rows of the generated IDHT matrix:
freq. (cos) (sin)
^ /. /.
| / . / .
| / . / .
| / . / .
| / . / .
| / . / .
| / . / .
| / . / .
|/ . / .
|1-------N----------2N---> indices of columns
.
N + 1
where N is the number of tones in the dictionary.
OR:
if the **bFreqSym** [frequency symmetrical] flag is set, then the frequencies
are organized like this:
freq. (cos) (sin)
^ /. \
| / . \
| / . \
| / . \
| / . \
| / . \
| / . \
| / . \
|/ . \
|1-------N----------2N---> indices of columns
.
N + 1
(the **bFreqSym** flag was added in v2.1, 14 January 2016).
*Examples*:
Please go to the *examples/dictionaries* directory for examples on how to
use the dictionary generator. |br|
*Settings*:
Parameters of the generator are described below.
Take a look on '__parametersDefine' function for more info on the
parameters.
Parameters of the dictionary generator are attributes of the class which
must/can be set before the generator is run.
Required parameters:
- a. **tS** (*float*): time of input signals
- b. **fR** (*float*): input signals' representation sampling frequency
- c. **fDelta** (*float*): the frequency separation between tones
- d. **nTones** (*float*): the number of tones in the dictionary
Optional parameters:
- e. **tStart** (*float*): the time shift of starting time point [default = 0]
- f, **fFirst** (*float*): the first frequency in the spectrum [default = fDelta]
- g. **bMute** (*int*): mute the console output from the sampler [default = 0]
*Output*:
Description of the dictionary generator output is below.
This is the list of attributes of the generator class which are available
after calling the 'run' method:
- a. **mDict** (*Numpy array 2D*): the generated dictionary, one tone in a row
- b. **vT** (*Numpy array 1D*): time vector for the dictionary
- c. **vF** (*Numpy array 1D*): frequency vector for the dictionary
Additional parameters of the generated dictionary:
- d. **Tg** (*float*): dictionary time representation period
- e. **nSamp** (*int*): the number of time representation samples
- f. **bFreqSym** (*int*): symmetrical/non-symmetrical frequency distribution flag
*Author*:
Jacek Pierzchlewski, Aalborg University, Denmark. <jap@es.aau.dk>
*Version*:
1.0 | 13-JAN-2015 : * Initial version. |br|
1.0r1 | 15-JAN-2015 : * Improvements in code comments |br|
2,0 | 20-AUG-2015 : * Version 2.0 released |br|
2.0r1 | 25-AUG-2015 : * Improvements in code comments and in headers |br|
2.1 | 14-JAN-2016 : * Frequencies of tones may be organized symetrical |br|
2.1r1 | 15-JAN-2016 : * Bug in entering the silent mode is repaired |br|
2.2 | 18-JAN-2016 : * Function 'freqRange' which gives indices of columns corresponding to a given frequency
range is added |br|
*License*:
BSD 2-Clause
"""
from __future__ import division
import rxcs
import numpy as np
class IDHT(rxcs._RxCSobject):
def __init__(self, *args):
rxcs._RxCSobject.__init__(self) # Make it a RxCS object
self.strRxCSgroup = 'Dictionary generator' # Name of group of RxCS modules
self.strModuleName = 'IDHT' # Module name
self.__parametersDefine() # Define the parameters
# Define parameters
def __parametersDefine(self):
# Time of the signal [s]
self.paramAddMan('tS', 'Time of the signal', unit='s')
self.paramType('tS', (int, float))
self.paramH('tS', 0)
self.paramL('tS', np.inf)
# The dictionary representation sampling freuqency [Hz]
self.paramAddMan('fR', 'The dictionary representation sampling freuqency', unit='Hz')
self.paramType('fR', (int, float))
self.paramH('fR', 0)
self.paramL('fR', np.inf)
# The optional time shift of starting time point
self.paramAddOpt('tStart', 'The time shift of starting time point', unit='s', default=0)
self.paramType('tStart', (int, float))
self.paramH('tStart', -np.inf)
self.paramL('tStart', np.inf)
# The frequency separation between tones [Hz]
self.paramAddMan('fDelta', 'The frequency separation between tones', unit='Hz')
self.paramType('fDelta', (int, float))
self.paramH('fDelta', 0)
self.paramL('fDelta', np.inf)
# The number of tones
self.paramAddMan('nTones', 'The number of tones')
self.paramType('nTones', int)
self.paramH('nTones', 0)
self.paramL('nTones', np.inf)
# The first frequency in the spectrum
self.paramAddOpt('fFirst', 'The first frequency in the spectrum', unit='Hz', default='$$fDelta')
self.paramType('fFirst', (int, float))
self.paramH('fFirst', 0)
self.paramL('fFirst', np.inf)
# The 'symmetrical frequency distribution' flag
self.paramAddOpt('bFreqSym', 'Symmetrical frequency distribution', default=0)
self.paramType('bFreqSym', (int))
self.paramAllowed('bFreqSym',[0, 1]) # It can be either 1 or 0
# 'Mute the output' flag
self.paramAddOpt('bMute', 'Mute the output', noprint=1, default=0)
self.paramType('bMute', int) # Must be of int type
self.paramAllowed('bMute',[0, 1]) # It can be either 1 or 0
# Run
def run(self):
self.parametersCheck() # Check if all the needed partameters are in place and are correct
self.parametersPrint() # Print the values of parameters
self.__engine() # Run the engine
return self.__dict__ # Return dictionary with the parameters
# Engine of the function
def __engine(self):
# Check of the configuration make sense
self._checkConf()
# Compute time and frequency parameters of dictionaries
(self.Tg, self.nSamp, self.tEnd) = self._computeParamT(self.tS, self.fR, self.tStart)
(self.fFirstHigh, self.fHigh) = self._computeParamF(self.fDelta, self.nTones, self.fFirst)
# Print some additional time and frequency parameters of the dictionary
self._printExtraParam()
self.engineStartsInfo() # Info that the engine starts
self.vF = self._generateFVector(self.fFirstHigh, self.fDelta, self.nTones) # Frequency vector
self.vT = self._generateTVector(self.Tg, self.nSamp, self.tStart) # Time vector
(self.mDict, self.vF) = self._generateIDHT(self.vT, self.vF) # The dicionary matrix
self.engineStopsInfo() # Info that the engine ends
return
# Check configuration
def _checkConf(self):
"""
This function checks if the configuration for the generator is correct
"""
# Check if the first frequency in the spectrum is compatible with the
# frequency separation between tones
nTonesStart = self.fFirst / self.fDelta
if not self.isequal(nTonesStart, np.round(nTonesStart), 1e-6):
strE = 'The first frequency in the spectrum (fFirst) is '
strE = strE + 'incompatible with the frequency separation between tones (fDelta)!'
raise ValueError(strE)
# Check if the time represented by dictionary is compatible
# with the representation sampling period
nSmp = self.tS * self.fR # The number of signal samples
if not self.isequal(nSmp, np.round(nSmp), 1e-6):
strE = 'Time represented by dictionary (tS) is incompatible with '
strE = strE + 'the dictionary representation sampling freuqency (fS)!'
raise ValueError(strE)
# Check if the optional time shift of starting time point is compatible
# with the representation sampling period
nSmptStart = self.tStart * self.fR
if not self.isequal(nSmptStart, np.round(nSmptStart), 1e-6):
strE = 'Time shift of starting time point (tS) is incompatible with '
strE = strE + 'the dictionary representation sampling frequency (fS)!'
raise ValueError(strE)
# Check Nyquist
fMax = self.fFirst + self.fDelta * (self.nTones - 1)
if not (self.fR > 2 * fMax):
strW = 'WARNING! The representation sampling frequency (fR) is to low! '
strW = strW + '(Ever heard about Nyqist principle?)'
rxcs.console.newline()
rxcs.console.warning(strW)
# -----------------------------------------------------------------
return
# Compute time parameters of dictionary
def _computeParamT(self, tS, fR, tStart):
"""
This function computes additional time parameters of the dictionary.
Args:
tS (float): time of input signals
fR (float): input signals' representation sampling frequency
tStart (float): the time shift of starting time point
Returns:
Tg (float): dictionary time representation period
nSamp (int): the number of time representation samples
tEnd (float): dictionary representation time end
"""
# The signal representation period
Tg = 1/fR
# The number of signal samples
nSamp = int(np.round(tS / Tg))
# Signal time end
tEnd = tStart + tS
return (Tg, nSamp, tEnd)
# Compute frequency parameters of dictionaries
def _computeParamF(self, fDelta, nTones, fFirst):
"""
This function computes additional frequency parameters of the dictionary.
Args:
fDelta (float): the frequency separation between tones
nTones (int): the number of tones in the dictionary
fFirst (float): the first frequency in the spectrum
Returns:
fFirstHigh (float): the positive low frequency limit of the dictionary
fHigh (float): the positive high frequency limit of the dictionary
"""
# The positive low frequency limit of the dictionary
fFirstHigh = np.floor(fFirst/fDelta) * fDelta
# The positive high frequency limit of the dictionary
fHigh = fFirstHigh + fDelta * (nTones - 1)
return (fFirstHigh, fHigh)
# Print some additional time parameters of the dictionary
def _printExtraParam(self):
if not self.bMute == 1:
rxcs.console.bullet_param('The last time moment represented by the dictionary',
self.tEnd, '-', 'seconds')
rxcs.console.bullet_param('The signal representation sampling period',
self.Tg, '-', 'seconds')
rxcs.console.param('The number of signal samples', self.nSamp, '-', 'samples')
rxcs.console.bullet_param('The maximum frequency represented by the dictionary',
self.fHigh, '-', 'Hz')
return
# Generate the frequency vector
def _generateFVector(self, fFirstHigh, fDelta, nTones):
"""
This function generates the frequency vector of the dictionary.
Args:
fFirstHigh (float): the positive low frequency limit of the dictionary
fDelta (float): the frequency separation between tones
nTones (int): the number of tones in the dictionary
Returns:
vF (Numpy array 1D): frequency vector for the dictionary
"""
# -----------------------------------------------------------------
# Generate the frequency vector
vF = np.arange(fFirstHigh, fFirstHigh + (fDelta * nTones), fDelta)
vF = np.hstack((vF, vF))
return vF
# Generate the time vector
def _generateTVector(self, Tg, nSamp, tStart):
"""
This function generates the time vector of the dictionary.
Args:
Tg (float): dictionary time representation period
nSamp (float): the number of time representation samples
tStart (int): the time shift of starting time point
Returns:
vT (Numpy array 1D): time vector for the dictionary
"""
# -----------------------------------------------------------------
# Generate the time vector
vT = Tg * np.arange(nSamp) + tStart
vT.shape = (vT.size, )
return vT
# Generate the IDHT dictionary
def _generateIDHT(self, vT, vF):
"""
This function generates the IDHT dictionary.
Args:
vT (Numpy array 1D): time vector for the dictionary
vF (Numpy array 1D): frequency vector for the dictionary
Returns:
mDict (Numpy array 2D): the generated dictionary
vF (Numpy array 1D): frequency vector for the dictionary
"""
# Change shape of the vectors, so that they can be multiplied
vT.shape = (1, vT.size)
vF_ = vF[0: vF.size / 2] # Take only half of the frequency vector
vF_.shape = (vF_.size, 1)
# -----------------------------------------------------------------
# Generate the Dictionary matrix
mFT = np.dot(vF_, vT) # Frequency / time matrix
mFT = 2*np.pi*mFT # ^
mCos = np.cos(mFT) # Cosine part of the matrix
mSin = np.sin(mFT) # Sinus part of the matrrix
mDict = np.vstack((mCos, mSin)) # IDHT matrix
# -----------------------------------------------------------------
# Reorganise the columns of the dictionary matrix,
# if the flag 'bFreqSym' is switched on
if self.bFreqSym == 1:
(nRows, _) = mDict.shape
mDict[np.arange(int(nRows/2), nRows), :] = mDict[np.arange(int(nRows) - 1, int(nRows/2) - 1, -1), :]
vF[np.arange(int(nRows/2), int(nRows))] = vF[np.arange(int(nRows) - 1, int(nRows/2) - 1, -1)]
# -----------------------------------------------------------------
vT.shape = (vT.size, ) # Restore shape of the time vector
return (mDict, vF)
def freqRange(self, iFMin, iFMax):
"""
Find indices of cols of the dictionary which correspond to a frequency range <iFMin, iFMax>
"""
if not 'vF' in self.__dict__:
raise RuntimeError('Dictionary generator did not generate a dictionary yet!')
if iFMin > iFMax:
raise RuntimeError('Low frequency defining the frequency range can not be higher than the high frequency!')
if (iFMin < 0) or (iFMax < 0):
raise RuntimeError('Frequencies which define the frequency range can not be lower than zero!')
if (iFMin > self.fHigh):
raise RuntimeError('Requested frequency range is not in the dictionary!')
iNf = self.vF.size # The number of frequencies in the dictionary
vInx = np.arange(iNf) # All the indices of frequencies in the dictionary
vFiltInx_p = vInx[np.multiply(self.vF >= iFMin, self.vF <= iFMax)]
vFiltInx_n = vInx[np.multiply(self.vF <= -iFMin, self.vF >= -iFMax)]
vFiltInx = np.hstack((vFiltInx_n, vFiltInx_p))
return vFiltInx