| Philipp Le | 692c01e | 2022-04-19 22:02:52 +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 |
| 11 | from dcs.config import default_store, ConfigObject, ui_create, ConfigControlFrame |
| 12 | import numpy as np |
| 13 | from dcs.frames.base import BaseFrame, Window |
| 14 | from dcs.frames.groups import Ch02Group |
| 15 | from typing import List |
| 16 | |
| 17 | import matplotlib |
| 18 | matplotlib.use('TkAgg') |
| 19 | from matplotlib.figure import Figure |
| 20 | from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg |
| 21 | |
| 22 | |
| 23 | class ConfigCh02FourierFrame(ConfigControlFrame): |
| 24 | wdg_abs_input: List[ttk.Spinbox] |
| 25 | wdg_phas_input: List[ttk.Spinbox] |
| 26 | |
| 27 | |
| 28 | @ui_create |
| 29 | class ConfigCh02Fourier(ConfigObject): |
| 30 | _KEY = 'ch02_fourier' |
| 31 | |
| 32 | n0_abs: confloat(ge=-5.0, le=5.0, multiple_of=0.01) = 0.0 |
| 33 | |
| 34 | n1_abs: confloat(ge=0.0, le=5.0, multiple_of=0.01) = 1.0 |
| 35 | n1_phas: confloat(ge=-180.0, le=180.0, multiple_of=0.1) = 0.0 |
| 36 | |
| 37 | n2_abs: confloat(ge=0.0, le=5.0, multiple_of=0.01) = 0.0 |
| 38 | n2_phas: confloat(ge=-180.0, le=180.0, multiple_of=0.1) = 0.0 |
| 39 | |
| 40 | n3_abs: confloat(ge=0.0, le=5.0, multiple_of=0.01) = 0.0 |
| 41 | n3_phas: confloat(ge=-180.0, le=180.0, multiple_of=0.1) = 0.0 |
| 42 | |
| 43 | n4_abs: confloat(ge=0.0, le=5.0, multiple_of=0.01) = 0.0 |
| 44 | n4_phas: confloat(ge=-180.0, le=180.0, multiple_of=0.1) = 0.0 |
| 45 | |
| 46 | n5_abs: confloat(ge=0.0, le=5.0, multiple_of=0.01) = 0.0 |
| 47 | n5_phas: confloat(ge=-180.0, le=180.0, multiple_of=0.1) = 0.0 |
| 48 | |
| 49 | n6_abs: confloat(ge=0.0, le=5.0, multiple_of=0.01) = 0.0 |
| 50 | n6_phas: confloat(ge=-180.0, le=180.0, multiple_of=0.1) = 0.0 |
| 51 | |
| 52 | n7_abs: confloat(ge=0.0, le=5.0, multiple_of=0.01) = 0.0 |
| 53 | n7_phas: confloat(ge=-180.0, le=180.0, multiple_of=0.1) = 0.0 |
| 54 | |
| 55 | n8_abs: confloat(ge=0.0, le=5.0, multiple_of=0.01) = 0.0 |
| 56 | n8_phas: confloat(ge=-180.0, le=180.0, multiple_of=0.1) = 0.0 |
| 57 | |
| 58 | n9_abs: confloat(ge=0.0, le=5.0, multiple_of=0.01) = 0.0 |
| 59 | n9_phas: confloat(ge=-180.0, le=180.0, multiple_of=0.1) = 0.0 |
| 60 | |
| 61 | def calc_vec(self, n: int, t: np.ndarray | int = 0) -> complex: |
| 62 | if n > 0: |
| 63 | phas = getattr(self, f'n{n}_abs') * np.exp(1j * getattr(self, f'n{n}_phas') * np.pi / 180) |
| 64 | else: |
| 65 | phas = getattr(self, f'n{n}_abs') |
| 66 | t = np.exp(1j * 2 * np.pi * n * t) |
| 67 | return phas * t |
| 68 | |
| 69 | def ui_create_abs(self, n: int, frm): |
| 70 | ui_create = getattr(self, f'ui_create_n{n}_abs') |
| 71 | return ui_create(frm) |
| 72 | |
| 73 | def ui_create_phas(self, n: int, frm): |
| 74 | ui_create = getattr(self, f'ui_create_n{n}_phas') |
| 75 | return ui_create(frm) |
| 76 | |
| 77 | def make_config_widget(self, parent: ttk.Widget) -> ConfigControlFrame: |
| 78 | frm = ConfigCh02FourierFrame(parent, borderwidth=1, relief='raised') |
| 79 | |
| 80 | frm.wdg_abs_input = [] |
| 81 | frm.wdg_phas_input = [] |
| 82 | |
| 83 | for idx in range(10): |
| 84 | ttk.Label(frm, text=f'n = {idx}:').grid(row=idx, column=0) |
| 85 | |
| 86 | wdg_abs = self.ui_create_abs(idx, frm) |
| 87 | wdg_abs.grid(row=idx, column=1) |
| 88 | frm.wdg_abs_input.append(wdg_abs) |
| 89 | |
| 90 | if idx > 0: |
| 91 | ttk.Label(frm, text='Phase:').grid(row=idx, column=2) |
| 92 | |
| 93 | wdg_phas = self.ui_create_phas(idx, frm) |
| 94 | wdg_phas.grid(row=idx, column=3) |
| 95 | frm.wdg_phas_input.append(wdg_phas) |
| 96 | |
| 97 | ttk.Label(frm, text='°').grid(row=idx, column=4) |
| 98 | |
| 99 | return frm |
| 100 | |
| 101 | |
| 102 | class Ch02FourierFrame(BaseFrame): |
| 103 | def __init__(self, *args, **kwargs): |
| 104 | super().__init__(*args, **kwargs) |
| 105 | |
| 106 | self._config = default_store().get_config(ConfigCh02Fourier) |
| 107 | |
| 108 | self.ctrl_frm = self._create_control() |
| 109 | self.ctrl_frm.pack(side=LEFT) |
| 110 | |
| 111 | signal_frm = self._create_signal_canvas() |
| 112 | signal_frm.pack(expand=True, fill=BOTH) |
| 113 | |
| 114 | def _create_control(self) -> ConfigCh02FourierFrame: |
| 115 | frm = self._config.make_config_widget(self) |
| 116 | for wdg in frm.wdg_abs_input: |
| 117 | wdg.listen_change(self._on_change) |
| 118 | for wdg in frm.wdg_phas_input: |
| 119 | wdg.listen_change(self._on_change) |
| 120 | return frm |
| 121 | |
| 122 | def _on_change(self, _, __, ___): |
| 123 | default_store().save() |
| 124 | self.draw_signal() |
| 125 | |
| 126 | def _create_signal_canvas(self) -> ttk.Widget: |
| 127 | frm = ttk.Frame(self) |
| 128 | |
| 129 | self._signal_fig = Figure(figsize=(12, 6), dpi=100) |
| 130 | |
| 131 | self._signal_canvas = FigureCanvasTkAgg(self._signal_fig, frm) |
| 132 | self._signal_canvas.get_tk_widget().pack(expand=True, fill=BOTH) |
| 133 | |
| 134 | self.draw_signal() |
| 135 | |
| 136 | return frm |
| 137 | |
| 138 | def draw_signal(self): |
| 139 | LENGTH = 200 |
| 140 | |
| 141 | self._signal_fig.clear() |
| 142 | ax = self._signal_fig.add_subplot(1, 1, 1) |
| 143 | ax.set_xlim(-1.2, 1.2) |
| 144 | ax.set_xlabel('time') |
| 145 | ax.set_ylabel('value') |
| 146 | ax.set_title('Signal') |
| 147 | |
| 148 | t = np.linspace(-1.0, 1.0, LENGTH) |
| 149 | |
| 150 | x = np.zeros((10, LENGTH)) |
| 151 | for n in range(10): |
| 152 | x[n, :] = self._config.calc_vec(n, t) |
| 153 | |
| 154 | s = np.sum(x, axis=0) |
| 155 | |
| 156 | ax.plot(t, s, label='Sum', linestyle='solid', linewidth=3) |
| 157 | for n in range(10): |
| 158 | ax.plot(t, x[n, :], label=f'n = {n}', linestyle='solid', linewidth=1) |
| 159 | ax.legend() |
| 160 | |
| 161 | self._signal_canvas.draw() |
| 162 | |
| 163 | |
| 164 | class Ch02FourierWindow(Window): |
| 165 | GROUP = Ch02Group |
| 166 | TITLE = 'Fourier Series' |
| 167 | FRAME = Ch02FourierFrame |
| 168 | |
| 169 | |
| 170 | if __name__ == '__main__': |
| 171 | Ch02FourierWindow.main() |
| 172 | |