.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_temporalTimeSeriesAnalysis/plot_harmonicAutocovariance.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_harmonicAutocovariance.py: Harmonic process analytical and estimated autocovariance ======================================================== .. 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 = 30.0 # Hertz srate = int(10 * max_freq) # T = 1.2 # sec T = 50 # sec K = 20 # K = 1 max_A = 20.0 min_A = 0.0 lags_samples = np.arange(srate) # samples .. 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) .. GENERATED FROM PYTHON SOURCE LINES 40-43 Create samples -------------- .. GENERATED FROM PYTHON SOURCE LINES 43-63 .. code-block:: Python time = np.arange(0, T, 1.0/srate) x = np.zeros(shape=len(time), dtype=np.double) for k in range(K): x += A[k] * np.cos(w[k] * time + phi[k]) fig = go.Figure() trace = go.Scatter(x=time, y=x, mode="lines+markers", showlegend=False) fig.add_trace(trace) fig.update_layout(title=f"N={T*srate}", xaxis=dict(title="Time (sec)"), yaxis=dict(title="x")) if not os.path.exists("figures"): os.mkdir("figures") fig.write_html(f"figures/harmonicAutoCovN{T*srate}x.html") fig.write_image(f"figures/harmonicAutoCovN{T*srate}x.png") fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 64-67 Estimate autocovariance ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 67-82 .. code-block:: Python estAcov = np.zeros(len(lags_samples)) anaAcov = np.zeros(len(lags_samples)) xMu = np.mean(x) for h in lags_samples: if h > 0: xs = x[h:] - xMu xt = x[:-h] - xMu elif h == 0: xs = x - xMu xt = x - xMu estAcov[h] = np.mean(xs * xt) lag_secs = h/srate anaAcov[h] = .5 * np.sum(A**2 * np.cos(w * lag_secs)) .. GENERATED FROM PYTHON SOURCE LINES 83-86 Plot analytical and estimated autocovariance -------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 86-105 .. code-block:: Python lags_secs = lags_samples/srate fig = go.Figure() trace = go.Scatter(x=lags_secs, y=anaAcov, mode="lines+markers", name="analytical") fig.add_trace(trace) trace = go.Scatter(x=lags_secs, y=estAcov, mode="lines+markers", name="estimated") fig.add_trace(trace) fig.update_layout(title=f"N={T*srate}", xaxis=dict(title="Lag (sec)"), yaxis=dict(title="Autocovariance")) if not os.path.exists("figures"): os.mkdir("figures") fig.write_html(f"figures/harmonicAutoCovN{T*srate}.html") fig.write_image(f"figures/harmonicAutoCovN{T*srate}.png") fig .. raw:: html


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