{ "cells": [ { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import math\n", "import struct\n", "\n", "import numpy as np\n", "from scipy import signal, optimize\n", "from matplotlib import pyplot as plt\n", "\n", "import rocof_test_data" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "%matplotlib widget" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "fs = 1000 # Hz\n", "ff = 50 # Hz\n", "duration = 60 # seconds\n", "# test_data = rocof_test_data.sample_waveform(rocof_test_data.test_close_interharmonics_and_flicker(),\n", "# duration=20,\n", "# sampling_rate=fs,\n", "# frequency=ff)[0]\n", "# test_data = rocof_test_data.sample_waveform(rocof_test_data.gen_noise(fmin=10, amplitude=1),\n", "# duration=20,\n", "# sampling_rate=fs,\n", "# frequency=ff)[0]\n", "\n", "\n", "#gen = rocof_test_data.gen_noise(fmin=10, amplitude=1)\n", "# gen = rocof_test_data.gen_noise(fmin=60, amplitude=0.2)\n", "# gen = rocof_test_data.test_harmonics()\n", "# gen = rocof_test_data.gen_interharmonic(*rocof_test_data.test_interharmonics)\n", "# gen = rocof_test_data.test_amplitude_steps()\n", "# gen = rocof_test_data.test_amplitude_and_phase_steps()\n", "test_data = []\n", "test_labels = [ fun.__name__.replace('test_', '') for fun in rocof_test_data.all_tests ]\n", "for gen in rocof_test_data.all_tests:\n", " test_data.append(rocof_test_data.sample_waveform(gen(),\n", " duration=duration,\n", " sampling_rate=fs,\n", " frequency=ff)[0])\n", "# d = 10 # seconds\n", "# test_data = np.sin(2*np.pi * ff * np.linspace(0, d, int(d*fs)))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "spr_fmt = f'{fs}Hz' if fs<1000 else f'{fs/1e3:f}'.rstrip('.0') + 'kHz'\n", "for label, data in zip(test_labels, test_data):\n", " with open(f'rocof_test_data/rocof_test_{label}_{spr_fmt}.bin', 'wb') as f:\n", " for sample in data:\n", " f.write(struct.pack(' f_min), np.argmin(f < f_max))\n", " \n", "\n", " f_mean = np.zeros(Zxx.shape[1])\n", " for t in range(1, Zxx.shape[1] - 1):\n", " frame_f = f[bounds_f]\n", " frame_step = frame_f[1] - frame_f[0]\n", " time_step = f_t[1] - f_t[0]\n", " #if t == 10:\n", " # axs[-1].plot(frame_f, frame_Z)\n", " frame_Z = np.abs(Zxx[bounds_f, t])\n", " # frame_f = f[180:220]\n", " # frame_Z = np.abs(Zxx[180:220, 40])\n", " # frame_f = f[15:35]\n", " # frame_Z = np.abs(Zxx[15:35, 40])\n", " # plt.plot(frame_f, frame_Z)\n", "\n", " # peak_f = frame_f[np.argmax(frame)]\n", " # plt.axvline(peak_f, color='red')\n", "\n", "# def gauss(x, *p):\n", "# A, mu, sigma, o = p\n", "# return A*np.exp(-(x-mu)**2/(2.*sigma**2)) + o\n", " \n", " def gauss(x, *p):\n", " A, mu, sigma = p\n", " return A*np.exp(-(x-mu)**2/(2.*sigma**2))\n", " \n", " f_start = frame_f[np.argmax(frame_Z)]\n", " A_start = np.max(frame_Z)\n", " p0 = [A_start, f_start, 1.]\n", " try:\n", " coeff, var = optimize.curve_fit(gauss, frame_f, frame_Z, p0=p0)\n", " # plt.plot(frame_f, gauss(frame_f, *coeff))\n", " #print(coeff)\n", " A, mu, sigma, *_ = coeff\n", " f_mean[t] = mu\n", " except RuntimeError:\n", " f_mean[t] = np.nan\n", " ax.plot(f_t[1:-1], f_mean[1:-1])\n", " \n", "# b, a = signal.butter(3,\n", "# 1/5, # Hz\n", "# btype='lowpass',\n", "# fs=1/time_step)\n", "# filtered = signal.lfilter(b, a, f_mean[1:-1], axis=0)\n", "# ax.plot(f_t[1:-1], filtered)\n", " \n", " ax.set_title(label, pad=-20)\n", " ax.set_ylabel('f [Hz]')\n", " ax.grid()\n", " if not label in ['off_frequency', 'sweep_phase_steps']:\n", " ax.set_ylim([49.90, 50.10])\n", " var = np.var(f_mean[1:-1])\n", " ax.text(0.5, 0.1, f'σ²={var * 1e3:.3g} mHz²', transform=ax.transAxes, ha='center')\n", " ax.text(0.5, 0.25, f'σ={np.sqrt(var) * 1e3:.3g} mHz', transform=ax.transAxes, ha='center')\n", "# ax.text(0.5, 0.2, f'filt. σ²={np.var(filtered) * 1e3:.3g} mHz', transform=ax.transAxes, ha='center')\n", " else:\n", " f_min, f_max = min(f_mean[1:-1]), max(f_mean[1:-1])\n", " delta = f_max - f_min\n", " ax.set_ylim(f_min - delta * 0.1, f_max + delta * 0.3)\n", " \n", "ax.set_xlabel('simulation time t [s]')\n", "None" ] } ], "metadata": { "kernelspec": { "display_name": "labenv", "language": "python", "name": "labenv" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }