| Philipp Le | 1538427 | 2022-05-02 23:40:25 +0200 | [diff] [blame^] | 1 | # SPDX-License-Identifier: MPL-2.0 |
| 2 | # Copyright (c) 2022 Philipp Le <philipp@philipple.de>. |
| 3 | # This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | from tkinter import ttk, LEFT, BOTH |
| 10 | from pydantic import confloat, conint |
| 11 | from dcs.config import default_store, ConfigObject, ui_create, ConfigControlFrame |
| 12 | import numpy as np |
| 13 | import scipy.signal.windows |
| 14 | from dcs.frames.base import BaseFrame, Window |
| 15 | from dcs.frames.groups import Ch04Group |
| 16 | from dcs.utils import swap_freq, abs_log_fft |
| 17 | |
| 18 | import matplotlib |
| 19 | matplotlib.use('TkAgg') |
| 20 | from matplotlib.figure import Figure |
| 21 | from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg |
| 22 | |
| 23 | |
| 24 | @ui_create |
| 25 | class ConfigCh04QuantizationNoise(ConfigObject): |
| 26 | _KEY = 'ch04_quantization_noise' |
| 27 | |
| 28 | freq: confloat(ge=0.0, lt=128.0, multiple_of=1.0) = 10.0 |
| 29 | amplitude: confloat(ge=0.0, lt=10.0, multiple_of=0.01) = 5.0 |
| 30 | |
| 31 | adc_bits: conint(ge=1, lt=25) = 8 |
| 32 | adc_min: confloat(ge=-50.0, lt=0.0, multiple_of=0.1) = -5.0 |
| 33 | adc_max: confloat(ge=0.0, lt=50.0, multiple_of=0.1) = 5.0 |
| 34 | |
| 35 | def make_config_widget(self, parent: ttk.Widget) -> ConfigControlFrame: |
| 36 | frm = ConfigControlFrame(parent, borderwidth=1, relief='raised') |
| 37 | |
| 38 | frm1 = ttk.Frame(frm, borderwidth=1, relief='raised') |
| 39 | frm1.pack() |
| 40 | |
| 41 | ttk.Label(frm1, text='Signal Frequency:').grid(row=0, column=0) |
| 42 | w = self.ui_create_freq(frm1) |
| 43 | frm.add_widget(w) |
| 44 | w.grid(row=0, column=1) |
| 45 | |
| 46 | ttk.Label(frm1, text='Signal Amplitude:').grid(row=1, column=0) |
| 47 | w = self.ui_create_amplitude(frm1) |
| 48 | frm.add_widget(w) |
| 49 | w.grid(row=1, column=1) |
| 50 | |
| 51 | frm2 = ttk.Frame(frm, borderwidth=1, relief='raised') |
| 52 | frm2.pack() |
| 53 | |
| 54 | ttk.Label(frm2, text='ADC Bits:').grid(row=0, column=0) |
| 55 | w = self.ui_create_adc_bits(frm2) |
| 56 | frm.add_widget(w) |
| 57 | w.grid(row=0, column=1) |
| 58 | |
| 59 | ttk.Label(frm2, text='ADC Min. Cut-off:').grid(row=1, column=0) |
| 60 | w = self.ui_create_adc_min(frm2) |
| 61 | frm.add_widget(w) |
| 62 | w.grid(row=1, column=1) |
| 63 | |
| 64 | ttk.Label(frm2, text='ADC Max. Cut-off:').grid(row=2, column=0) |
| 65 | w = self.ui_create_adc_max(frm2) |
| 66 | frm.add_widget(w) |
| 67 | w.grid(row=2, column=1) |
| 68 | |
| 69 | return frm |
| 70 | |
| 71 | def calc_signal(self, t: np.ndarray) -> np.ndarray: |
| 72 | center_volt = (self.adc_max + self.adc_min) / 2 |
| 73 | step = (self.adc_max - self.adc_min) / np.power(2, self.adc_bits) |
| 74 | volt = np.real(center_volt + (self.amplitude * np.exp(1j * 2 * np.pi * self.freq * t))) |
| 75 | discrete = np.round(volt / step) + np.power(2, self.adc_bits - 1) |
| 76 | discrete = np.where(discrete < 0, 0, discrete) |
| 77 | upper_limit = np.power(2, self.adc_bits) - 1 |
| 78 | discrete = np.where(discrete > upper_limit, upper_limit, discrete) |
| 79 | return discrete |
| 80 | |
| 81 | |
| 82 | class Ch04QuantizationNoiseFrame(BaseFrame): |
| 83 | def __init__(self, *args, **kwargs): |
| 84 | super().__init__(*args, **kwargs) |
| 85 | |
| 86 | self._config: ConfigCh04QuantizationNoise = default_store().get_config(ConfigCh04QuantizationNoise) |
| 87 | |
| 88 | ctrl_frm = self._create_control() |
| 89 | ctrl_frm.pack(side=LEFT) |
| 90 | |
| 91 | signal_frm = self._create_signal_canvas() |
| 92 | signal_frm.pack(expand=True, fill=BOTH) |
| 93 | |
| 94 | def _create_control(self) -> ConfigControlFrame: |
| 95 | frm: ConfigControlFrame = self._config.make_config_widget(self) |
| 96 | frm.widgets_on_change(self._on_change) |
| 97 | return frm |
| 98 | |
| 99 | def _on_change(self, _, __, ___): |
| 100 | default_store().save() |
| 101 | self.draw_signal() |
| 102 | |
| 103 | def _create_signal_canvas(self) -> ttk.Widget: |
| 104 | frm = ttk.Frame(self) |
| 105 | |
| 106 | self._signal_fig = Figure(figsize=(12, 6), dpi=100) |
| 107 | |
| 108 | self._signal_canvas = FigureCanvasTkAgg(self._signal_fig, frm) |
| 109 | self._signal_canvas.get_tk_widget().pack(expand=True, fill=BOTH) |
| 110 | |
| 111 | self.draw_signal() |
| 112 | |
| 113 | return frm |
| 114 | |
| 115 | def draw_signal(self): |
| 116 | LENGTH = 2048 |
| 117 | |
| 118 | self._signal_fig.clear() |
| 119 | ax_td = self._signal_fig.add_subplot(2, 1, 1) |
| 120 | ax_td.set_xlim(0.0, 1.0) |
| 121 | ax_td.set_xlabel('time') |
| 122 | ax_td.set_ylabel('value') |
| 123 | ax_td.set_title('Time Domain') |
| 124 | ax_fd = self._signal_fig.add_subplot(2, 1, 2) |
| 125 | ax_fd.set_xlim(0, int(LENGTH/4)) |
| 126 | ax_fd.set_ylim(-180, 0) |
| 127 | ax_fd.set_xlabel('frequency') |
| 128 | ax_fd.set_ylabel('value (logarithmic)') |
| 129 | ax_fd.set_title('Frequency Domain') |
| 130 | |
| 131 | t = np.arange(0, LENGTH, 1) / LENGTH |
| 132 | f = swap_freq(np.fft.fftfreq(t.shape[-1], 1.0/LENGTH)) |
| 133 | |
| 134 | sig_td = self._config.calc_signal(t) |
| 135 | sig_td_norm = 2 * np.sqrt(2) * ((sig_td / np.power(2, self._config.adc_bits)) - 0.5) |
| 136 | sig_fd = abs_log_fft(sig_td_norm * scipy.signal.windows.blackman(len(t))) |
| 137 | |
| 138 | ax_td.plot(t, sig_td, label='Quantized Signal', linestyle='solid', color='blue', linewidth=1) |
| 139 | ax_td.legend() |
| 140 | |
| 141 | ax_fd.plot(f, sig_fd, label='Quantized Signal', linestyle='solid', color='blue', linewidth=1) |
| 142 | ax_fd.legend() |
| 143 | |
| 144 | self._signal_fig.tight_layout() |
| 145 | self._signal_canvas.draw() |
| 146 | |
| 147 | |
| 148 | class Ch04QuantizationNoiseWindow(Window): |
| 149 | GROUP = Ch04Group |
| 150 | TITLE = 'Quantization Noise' |
| 151 | FRAME = Ch04QuantizationNoiseFrame |
| 152 | |
| 153 | |
| 154 | if __name__ == '__main__': |
| 155 | Ch04QuantizationNoiseWindow.main() |
| 156 | |