| Philipp Le | c2a590b | 2022-04-26 21:31:47 +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, Listbox, 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 | from dcs.frames.base import BaseFrame, Window |
| 14 | from dcs.frames.groups import Ch03Group |
| 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 | @ui_create |
| 24 | class Function(ConfigObject): |
| 25 | freq_mult: conint(ge=0, lt=3) = 0 |
| 26 | amplitude: confloat(ge=0.0, lt=10.0, multiple_of=0.01) = 5.0 |
| 27 | phase: confloat(ge=-180.0, le=180.0, multiple_of=0.1) = 0.0 |
| 28 | |
| 29 | offset: confloat(ge=-5.0, lt=5.0, multiple_of=0.01) = 0.0 |
| 30 | |
| 31 | sigma: confloat(ge=0, le=3.0, multiple_of=0.01) = 1.0 |
| 32 | |
| 33 | def make_config_widget(self, parent: ttk.Widget) -> ConfigControlFrame: |
| 34 | frm = ConfigControlFrame(parent, borderwidth=1, relief='raised') |
| 35 | |
| 36 | ttk.Label(frm, text='Frequency:').grid(row=0, column=0) |
| 37 | self.ui_create_freq_mult(frm).grid(row=0, column=1) |
| 38 | |
| 39 | ttk.Label(frm, text='Amplitude:').grid(row=1, column=0) |
| 40 | self.ui_create_amplitude(frm).grid(row=1, column=1) |
| 41 | |
| 42 | ttk.Label(frm, text='Phase:').grid(row=2, column=0) |
| 43 | self.ui_create_phase(frm).grid(row=2, column=1) |
| 44 | ttk.Label(frm, text='°').grid(row=2, column=2) |
| 45 | |
| 46 | ttk.Label(frm, text='Offset:').grid(row=3, column=0) |
| 47 | self.ui_create_offset(frm).grid(row=3, column=1) |
| 48 | |
| 49 | ttk.Label(frm, text='Standard Deviation:').grid(row=4, column=0) |
| 50 | self.ui_create_sigma(frm).grid(row=4, column=1) |
| 51 | |
| 52 | return frm |
| 53 | |
| 54 | def calc_signal(self, t: np.ndarray | int = 0) -> np.ndarray: |
| 55 | phas = self.amplitude * np.exp(1j * self.phase * np.pi / 180) |
| 56 | phi = np.exp(1j * 2 * np.pi * self.freq_mult * t) |
| 57 | if type(t) == int: |
| 58 | ran_vec = np.random.normal(self.offset, self.sigma) |
| 59 | else: |
| 60 | ran_vec = np.random.normal(self.offset, self.sigma, len(t)) |
| 61 | return (phas * phi) + ran_vec |
| 62 | |
| 63 | def make_title(self): |
| 64 | return f'n={self.freq_mult}, {self.amplitude}, {self.phase}°, sigma={self.sigma}' |
| 65 | |
| 66 | |
| 67 | class ConfigCh03ErgodicFrame(ConfigControlFrame): |
| 68 | wdg_seed: ttk.Spinbox |
| 69 | wdg_funcs: Listbox |
| 70 | |
| 71 | |
| 72 | @ui_create |
| 73 | class ConfigCh03Ergodic(ConfigObject): |
| 74 | _KEY = 'ch03_ergodic' |
| 75 | |
| 76 | random_seed: conint(ge=0, lt=10000) = 1000 |
| 77 | functions: List[Function] = [ |
| 78 | Function(), |
| 79 | Function(), |
| 80 | Function(), |
| 81 | Function(), |
| 82 | Function(), |
| 83 | Function(), |
| 84 | ] |
| 85 | |
| 86 | def make_config_widget(self, parent: ttk.Widget) -> ConfigControlFrame: |
| 87 | frm = ConfigCh03ErgodicFrame(parent, borderwidth=1, relief='raised') |
| 88 | |
| 89 | frm1 = ttk.Frame(frm) |
| 90 | frm1.pack() |
| 91 | |
| 92 | ttk.Label(frm1, text='Random Seed:').grid(row=0, column=0) |
| 93 | frm.wdg_seed = self.ui_create_random_seed(frm1) |
| 94 | frm.wdg_seed.grid(row=0, column=1) |
| 95 | |
| 96 | ttk.Label(frm, text='Functions:').pack() |
| 97 | frm.wdg_funcs = self.ui_create_functions_list(frm, lambda e: e.make_title()) |
| 98 | frm.wdg_funcs.pack() |
| 99 | |
| 100 | return frm |
| 101 | |
| 102 | |
| 103 | class Ch03ErgodicFrame(BaseFrame): |
| 104 | def __init__(self, *args, **kwargs): |
| 105 | super().__init__(*args, **kwargs) |
| 106 | |
| 107 | self._config: ConfigCh03Ergodic = default_store().get_config(ConfigCh03Ergodic) |
| 108 | |
| 109 | ctrl_frm = self._create_control() |
| 110 | ctrl_frm.pack(side=LEFT) |
| 111 | |
| 112 | signal_frm = self._create_signal_canvas() |
| 113 | signal_frm.pack(expand=True, fill=BOTH) |
| 114 | |
| 115 | def _create_control(self) -> ConfigCh03ErgodicFrame: |
| 116 | frm: ConfigCh03ErgodicFrame = self._config.make_config_widget(self) |
| 117 | frm.wdg_seed.listen_change(self._on_change) |
| 118 | frm.wdg_funcs.listen_change(self._on_change) |
| 119 | return frm |
| 120 | |
| 121 | def _on_change(self, _, __, ___): |
| 122 | default_store().save() |
| 123 | self.draw_signal() |
| 124 | |
| 125 | def _create_signal_canvas(self) -> ttk.Widget: |
| 126 | frm = ttk.Frame(self) |
| 127 | |
| 128 | self._signal_fig = Figure(figsize=(12, 6), dpi=100) |
| 129 | |
| 130 | self._signal_canvas = FigureCanvasTkAgg(self._signal_fig, frm) |
| 131 | self._signal_canvas.get_tk_widget().pack(expand=True, fill=BOTH) |
| 132 | |
| 133 | self.draw_signal() |
| 134 | |
| 135 | return frm |
| 136 | |
| 137 | def draw_signal(self): |
| 138 | LENGTH = 200 |
| 139 | |
| 140 | np.random.seed(self._config.random_seed) |
| 141 | |
| 142 | self._signal_fig.clear() |
| 143 | ax = self._signal_fig.add_subplot(1, 1, 1) |
| 144 | ax.set_xlim(-1.2, 1.2) |
| 145 | ax.set_xlabel('time') |
| 146 | ax.set_ylabel('value') |
| 147 | ax.set_title('Signal') |
| 148 | |
| 149 | t = np.linspace(-1.0, 1.0, LENGTH) |
| 150 | |
| 151 | x = np.zeros((len(self._config.functions), LENGTH)) |
| 152 | temp_mean = np.zeros((len(self._config.functions), )) |
| 153 | for index, func in enumerate(self._config.functions): |
| 154 | x[index, :] = func.calc_signal(t) |
| 155 | temp_mean[index] = np.mean(x[index, :]) |
| 156 | |
| 157 | stoch_mean = np.mean(x, axis=0) |
| 158 | |
| 159 | for index, func in enumerate(self._config.functions): |
| 160 | ax.plot(t, x[index, :], label=f'{func.make_title()}', linestyle='solid', linewidth=1) |
| 161 | ones = np.ones(len(t)) |
| 162 | ax.plot(t, ones*temp_mean[index], label=f'Temp. Mean {func.make_title()}', linestyle='solid', linewidth=2) |
| 163 | ax.plot(t, stoch_mean, label='Stochastic Mean', linestyle='solid', linewidth=3) |
| 164 | ax.legend() |
| 165 | |
| 166 | self._signal_canvas.draw() |
| 167 | |
| 168 | |
| 169 | class Ch03ErgodicWindow(Window): |
| 170 | GROUP = Ch03Group |
| 171 | TITLE = 'Ergodic Process' |
| 172 | FRAME = Ch03ErgodicFrame |
| 173 | |
| 174 | |
| 175 | if __name__ == '__main__': |
| 176 | Ch03ErgodicWindow.main() |
| 177 | |