.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_temporalTimeSeriesAnalysis/plot_samplesHarmonicProcess.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_temporalTimeSeriesAnalysis_plot_samplesHarmonicProcess.py: Plotting samples from harmonic process ====================================== .. GENERATED FROM PYTHON SOURCE LINES 7-9 Import requirements ------------------- .. GENERATED FROM PYTHON SOURCE LINES 9-14 .. code-block:: Python import os import numpy as np import plotly.graph_objects as go .. GENERATED FROM PYTHON SOURCE LINES 15-17 Define variables ---------------- .. GENERATED FROM PYTHON SOURCE LINES 17-29 .. code-block:: Python min_freq = 1.0 # Hertz max_freq = 50.0 # Hertz srate = int(10 * max_freq) T = 2 # sec K = 20 min_A = 0.0 max_A = 20.0 n_time_series_to_plot = 100 cb_width = 5 .. GENERATED FROM PYTHON SOURCE LINES 30-33 Set harmonic process constants ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 33-39 .. code-block:: Python A = np.linspace(start=min_A, stop=max_A, num=K) f = np.linspace(start=min_freq, stop=max_freq, num=K) w = 2*np.pi*f phi = np.random.uniform(low=-np.pi, high=np.pi, size=[K, n_time_series_to_plot]) .. GENERATED FROM PYTHON SOURCE LINES 40-43 Create samples -------------- .. GENERATED FROM PYTHON SOURCE LINES 43-51 .. code-block:: Python time = np.arange(0, T, 1.0/srate) x = np.zeros(shape=[n_time_series_to_plot, len(time)], dtype=np.double) for i in range(n_time_series_to_plot): # generate the ith time series for k in range(K): x[i, :] += A[k] * np.cos(w[k] * time + phi[k, i]) .. GENERATED FROM PYTHON SOURCE LINES 52-55 Calculate analytical variance and 95% confidence band ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 55-61 .. code-block:: Python mean = np.zeros(shape=len(time), dtype=np.double) var = np.sum(A**2) / 2.0 cb_up = mean + 1.96 * np.sqrt(var) cb_down = mean - 1.96 * np.sqrt(var) .. GENERATED FROM PYTHON SOURCE LINES 62-65 Plot time series with mean and 95% confidence band --------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 65-95 .. code-block:: Python fig = go.Figure() for j in range(n_time_series_to_plot): trace = go.Scatter(x=time, y=x[j, :], mode="lines+markers", name=f"sample {j}", showlegend=True) fig.add_trace(trace) fig.update_layout(xaxis=dict(title="Time (sec)"), yaxis=dict(title="x")) trace = go.Scatter(x=time, y=mean, line=dict(color="black", width=cb_width), mode="lines", showlegend=False) fig.add_trace(trace) trace = go.Scatter(x=time, y=cb_down, line=dict(color="black", dash="dash", width=cb_width), mode="lines", showlegend=False) fig.add_trace(trace) trace = go.Scatter(x=time, y=cb_up, line=dict(color="black", dash="dash", width=cb_width), mode="lines", showlegend=False) fig.add_trace(trace) fig.update_layout(xaxis=dict(title="Time (sec)"), yaxis=dict(title="x")) if not os.path.exists("figures"): os.mkdir("figures") fig.write_html("figures/harmonicSamples.html") fig.write_image("figures/harmonicSamples.png") fig .. raw:: html


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 7.684 seconds) .. _sphx_glr_download_auto_examples_01_temporalTimeSeriesAnalysis_plot_samplesHarmonicProcess.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_samplesHarmonicProcess.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_samplesHarmonicProcess.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_samplesHarmonicProcess.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_