-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdebug_Alia_bare_metal.py
343 lines (281 loc) · 11.6 KB
/
debug_Alia_bare_metal.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Dennis van Gils
29-05-2021
"""
# pylint: disable=invalid-name, missing-function-docstring
import time as Time
import numpy as np
from dvg_ringbuffer import RingBuffer
from dvg_ringbuffer_fir_filter import (
RingBuffer_FIR_Filter,
RingBuffer_FIR_Filter_Config,
)
from dvg_fftw_welchpowerspectrum import FFTW_WelchPowerSpectrum
from pyinstrument import Profiler
RUN_PYINSTRUMENT = False
TEST_POWERSPECTRA = True
# Main parameters to test for
BLOCK_SIZE = 2000
N_BLOCKS = 21
Fs = 20000 # [Hz]
FFTW_THREADS_CONVOLVE = 5 # sweet spot seems to be 5
FFTW_THREADS_SPECTRUM = 5 # sweet spot seems to be 5
# Simulation vars
T_total = 120 # [s]
ref_freq_Hz = 250 # [Hz]
ref_V_offset = 1.5 # [V]
sig_I_phase = 10 # [deg]
sig_I_noise_ampl = 0.04
class State:
def __init__(self, block_size, N_blocks):
"""Reflects the actual readings, parsed into separate variables, of
the lock-in amplifier. There should only be one instance of the
State class.
"""
# fmt: off
self.block_size = block_size
self.N_blocks = N_blocks
self.rb_capacity = block_size * N_blocks
self.blocks_received = 0
# Arrays to hold the block data coming from the lock-in amplifier
# Keep `time` as `dtype=np.float64`, because it can contain `np.nan`
self.time = np.full(block_size, np.nan, dtype=np.float64) # [ms]
self.ref_X = np.full(block_size, np.nan, dtype=np.float64)
self.ref_Y = np.full(block_size, np.nan, dtype=np.float64)
self.sig_I = np.full(block_size, np.nan, dtype=np.float64)
self.time_1 = np.full(block_size, np.nan, dtype=np.float64) # [ms]
self.filt_I = np.full(block_size, np.nan, dtype=np.float64)
self.mix_X = np.full(block_size, np.nan, dtype=np.float64)
self.mix_Y = np.full(block_size, np.nan, dtype=np.float64)
self.time_2 = np.full(block_size, np.nan, dtype=np.float64) # [ms]
self.X = np.full(block_size, np.nan, dtype=np.float64)
self.Y = np.full(block_size, np.nan, dtype=np.float64)
self.R = np.full(block_size, np.nan, dtype=np.float64)
self.T = np.full(block_size, np.nan, dtype=np.float64)
self.sig_I_min = np.nan
self.sig_I_max = np.nan
self.sig_I_avg = np.nan
self.sig_I_std = np.nan
self.filt_I_min = np.nan
self.filt_I_max = np.nan
self.filt_I_avg = np.nan
self.filt_I_std = np.nan
self.X_avg = np.nan
self.Y_avg = np.nan
self.R_avg = np.nan
self.T_avg = np.nan
# Ring buffers (rb) for performing FIR filtering and power spectra
_p = {'capacity': self.rb_capacity, 'dtype': np.float64}
# Stage 0: unprocessed data
self.rb_time = RingBuffer(**_p)
self.rb_ref_X = RingBuffer(**_p)
self.rb_ref_Y = RingBuffer(**_p)
self.rb_sig_I = RingBuffer(**_p)
# Stage 1: AC-coupling and band-stop filter and heterodyne mixing
self.rb_time_1 = RingBuffer(**_p)
self.rb_filt_I = RingBuffer(**_p)
self.rb_mix_X = RingBuffer(**_p)
self.rb_mix_Y = RingBuffer(**_p)
# Stage 2: apply low-pass filter and signal reconstruction
self.rb_time_2 = RingBuffer(**_p)
self.rb_X = RingBuffer(**_p)
self.rb_Y = RingBuffer(**_p)
self.rb_R = RingBuffer(**_p)
self.rb_T = RingBuffer(**_p)
# fmt: on
# ------------------------------------------------------------------------------
# main
# ------------------------------------------------------------------------------
if __name__ == "__main__":
state = State(BLOCK_SIZE, N_BLOCKS)
# Create FIR filters
# --------------------
# AC-coupling & band-stop filter on sig_I
firf_1_config = RingBuffer_FIR_Filter_Config(
Fs=Fs,
block_size=BLOCK_SIZE,
N_blocks=N_BLOCKS,
firwin_cutoff=[2.0, 48.0, 52.0],
firwin_window="blackmanharris",
firwin_pass_zero=False,
fftw_threads=FFTW_THREADS_CONVOLVE,
)
firf_1_sig_I = RingBuffer_FIR_Filter(
config=firf_1_config, name="firf_1_sig_I"
)
# Low-pass filter on mix_X and mix_Y
roll_off_width = 5 # [Hz]
firf_2_config = RingBuffer_FIR_Filter_Config(
Fs=Fs,
block_size=BLOCK_SIZE,
N_blocks=N_BLOCKS,
firwin_cutoff=2 * ref_freq_Hz - roll_off_width,
firwin_window="blackmanharris",
firwin_pass_zero=True,
fftw_threads=FFTW_THREADS_CONVOLVE,
)
firf_2_mix_X = RingBuffer_FIR_Filter(
config=firf_2_config, name="firf_2_mix_X"
)
firf_2_mix_Y = RingBuffer_FIR_Filter(
config=firf_2_config, name="firf_2_mix_Y"
)
# Create power spectrum FFTW objects
# ------------------------------------
if TEST_POWERSPECTRA:
p = {
"len_data": BLOCK_SIZE * N_BLOCKS,
"fs": Fs,
"nperseg": Fs,
"fftw_threads": FFTW_THREADS_SPECTRUM,
}
# fmt: off
fftw_PS_sig_I = FFTW_WelchPowerSpectrum(**p)
fftw_PS_filt_I = FFTW_WelchPowerSpectrum(**p)
fftw_PS_mix_X = FFTW_WelchPowerSpectrum(**p)
fftw_PS_mix_Y = FFTW_WelchPowerSpectrum(**p)
fftw_PS_R = FFTW_WelchPowerSpectrum(**p)
# fmt: on
# Generate artificial time series ahead of time
# -----------------------------------------------
time = np.linspace(0, T_total, T_total * Fs, endpoint=False) # [s]
ref_X = ref_V_offset + np.cos(2 * np.pi * ref_freq_Hz * time)
ref_Y = ref_V_offset + np.sin(2 * np.pi * ref_freq_Hz * time)
prng = np.random.RandomState(1234567890) # pylint: disable=no-member
sig_I = (
ref_V_offset
+ np.cos(2 * np.pi * ref_freq_Hz * time - sig_I_phase / 180 * np.pi)
+ sig_I_noise_ampl * prng.randn(len(time))
)
"""
# Lower memory consumption by using a look-up table (LUT)
N_LUT = int(np.round(Fs / ref_freq_Hz))
ref_freq = Fs / N_LUT
# Arduino C++ style using for-loop
lut_cos = np.full(N_LUT, np.nan)
for i in range(N_LUT):
# -- Cosine
# N_LUT even: [ 0, 1]
# N_LUT odd : [>0, 1]
lut_cos[i] = 0.5 * (1 + np.cos(2 * np.pi * i / N_LUT))
"""
# Simulate incoming blocks on the fly
# -------------------------------------
if RUN_PYINSTRUMENT:
profiler = Profiler()
profiler.start()
# DEV NOTE: The use of a `numba.njit(nogil=True)`` decorator on numpy
# functions operating on the relatively small `state` time series will not
# significantly improve the calculation speed. Simply refrain from using it
# in the upcoming block of code.
tick = Time.perf_counter()
N_sim_blocks = int(len(time) / BLOCK_SIZE)
for idx_sim_block in range(N_sim_blocks):
sim_slice = slice(
BLOCK_SIZE * idx_sim_block, BLOCK_SIZE * (idx_sim_block + 1)
)
# Stage 0
# -------
state.time = time[sim_slice]
state.ref_X = ref_X[sim_slice]
state.ref_Y = ref_Y[sim_slice]
state.sig_I = sig_I[sim_slice]
state.sig_I_min = np.min(state.sig_I)
state.sig_I_max = np.max(state.sig_I)
state.sig_I_avg = np.mean(state.sig_I)
state.sig_I_std = np.std(state.sig_I)
state.rb_time.extend(state.time)
state.rb_ref_X.extend(state.ref_X)
state.rb_ref_Y.extend(state.ref_Y)
state.rb_sig_I.extend(state.sig_I)
# Stage 1
# -------
# Apply filter 1 to sig_I
state.filt_I = firf_1_sig_I.apply_filter(state.rb_sig_I)
if firf_1_sig_I.filter_has_settled:
# Retrieve the block of original data from the past that aligns with
# the current filter output
valid_slice = firf_1_sig_I.rb_valid_slice
state.time_1 = state.rb_time[valid_slice]
old_sig_I = state.rb_sig_I[valid_slice]
old_ref_X = state.rb_ref_X[valid_slice]
old_ref_Y = state.rb_ref_Y[valid_slice]
# Heterodyne mixing
# Equivalent to:
# mix_X = (old_ref_X - c.ref_V_offset) * filt_I # SLOW code
# mix_Y = (old_ref_Y - c.ref_V_offset) * filt_I # SLOW code
np.subtract(old_ref_X, ref_V_offset, out=old_ref_X)
np.subtract(old_ref_Y, ref_V_offset, out=old_ref_Y)
np.multiply(old_ref_X, state.filt_I, out=state.mix_X)
np.multiply(old_ref_Y, state.filt_I, out=state.mix_Y)
else:
state.time_1.fill(np.nan)
old_sig_I = np.full(BLOCK_SIZE, np.nan)
state.mix_X.fill(np.nan)
state.mix_Y.fill(np.nan)
state.filt_I_min = np.min(state.filt_I)
state.filt_I_max = np.max(state.filt_I)
state.filt_I_avg = np.mean(state.filt_I)
state.filt_I_std = np.std(state.filt_I)
state.rb_time_1.extend(state.time_1)
state.rb_filt_I.extend(state.filt_I)
state.rb_mix_X.extend(state.mix_X)
state.rb_mix_Y.extend(state.mix_Y)
# Stage 2
# -------
# Apply filter 2 to the mixer output
state.X = firf_2_mix_X.apply_filter(state.rb_mix_X)
state.Y = firf_2_mix_Y.apply_filter(state.rb_mix_Y)
if firf_2_mix_X.filter_has_settled:
# Retrieve the block of time data from the past that aligns with
# the current filter output
valid_slice = firf_1_sig_I.rb_valid_slice
state.time_2 = state.rb_time_1[valid_slice]
# Signal amplitude and phase reconstruction
np.sqrt(np.add(np.square(state.X), np.square(state.Y)), out=state.R)
# NOTE: Because `mix_X` and `mix_Y` are both of type `numpy.ndarray`, a
# division by `mix_X = 0` is handled correctly due to `numpy.inf`.
# Likewise, `numpy.arctan(numpy.inf)`` will result in pi/2. We suppress
# the RuntimeWarning: divide by zero encountered in true_divide.
np.seterr(divide="ignore")
np.divide(state.Y, state.X, out=state.T)
np.arctan(state.T, out=state.T)
np.multiply(state.T, 180 / np.pi, out=state.T) # [rad] to [deg]
np.seterr(divide="warn")
else:
state.time_2.fill(np.nan)
state.R.fill(np.nan)
state.T.fill(np.nan)
state.X_avg = np.mean(state.X)
state.Y_avg = np.mean(state.Y)
state.R_avg = np.mean(state.R)
state.T_avg = np.mean(state.T)
state.rb_time_2.extend(state.time_2)
state.rb_X.extend(state.X)
state.rb_Y.extend(state.Y)
state.rb_R.extend(state.R)
state.rb_T.extend(state.T)
# Power spectra
# -------------
if TEST_POWERSPECTRA:
if state.rb_sig_I.is_full:
fftw_PS_sig_I.compute_spectrum_dB(state.rb_sig_I)
if state.rb_filt_I.is_full:
fftw_PS_filt_I.compute_spectrum_dB(state.rb_filt_I)
if state.rb_mix_X.is_full:
fftw_PS_mix_X.compute_spectrum_dB(state.rb_mix_X)
if state.rb_mix_Y.is_full:
fftw_PS_mix_Y.compute_spectrum_dB(state.rb_mix_Y)
if state.rb_R.is_full:
fftw_PS_R.compute_spectrum_dB(state.rb_R)
if RUN_PYINSTRUMENT:
profiler.stop()
profiler.open_in_browser()
# print(profiler.output_text(unicode=True, color=True))
print("%5.3f %5.3f" % (state.sig_I_avg, state.filt_I_avg))
print("%5.3f %5.3f" % (state.R_avg, state.T_avg))
tock = Time.perf_counter()
print("Number of blocks simulated: %i" % N_sim_blocks)
print("Avg time per block: %.1f ms" % ((tock - tick) / N_sim_blocks * 1000))