<jupyter_start><jupyter_text>[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/haribharadwaj/notebooks/blob/main/BME511/RandomProcesses.ipynb) Random processesSo far, we have been loose with our definitions of random signals, correlations, etc. Let's try and make formalize some of those ideas.<jupyter_code>import numpy as np
import pylab as pl<jupyter_output><empty_output><jupyter_text>White Noise Let's begin with a white noise example. White noise is essentially a sequence of IID normal random numbers. Why is it called white noise?<jupyter_code>N = 500
for k in range(5):
    x = np.random.randn(N)  # Generate 500 sample white noise
    pl.plot(x)
pl.xlabel('Sample Number')<jupyter_output><empty_output><jupyter_text>"Moving Average" (MA) processHere, let's start with IID draws, but then pass it out using a moving average filter. What does that do to the signal?<jupyter_code>n_ma = 50  ## Do 100 sample moving average
h = np.ones(n_ma) / (n_ma ** 0.5)
from scipy import signal
for k in range(5):
    y = signal.lfilter(h, 1, np.random.randn(N))
    pl.plot(y)
pl.xlabel('Sample Number')<jupyter_output><empty_output><jupyter_text>It's clear from above that the white-noise process and the MA process are fundamentally different in some way. How can we characterize this difference? Autocorrelation function<jupyter_code>rxx = signal.correlate(x, x, 'full')
lags_x = signal.correlation_lags(x.shape[0], x.shape[0], 'full')
pl.plot(lags_x, rxx)
pl.xlabel('Lag (samples)')
nrep = 1000
ryy_ave = 0
for k in range(nrep):
    y = signal.lfilter(h, 1, np.random.randn(N))
    ryy = signal.correlate(y, y, 'full')
    ryy_ave += ryy
lags_y = signal.correlation_lags(y.shape[0], y.shape[0], 'full')
pl.plot(lags_y, ryy_ave/nrep)
pl.xlabel('Lag (samples)')<jupyter_output><empty_output><jupyter_text>Cross correlation function<jupyter_code>N = 500
nreps = 1
rxx_ave = 0
rxy_ave = 0
for k in range(nreps):
    x = np.random.randn(N)  # Generate 500 sample white noise
    y = np.random.randn(N)
    rxx = signal.correlate(x, x, 'full')
    rxx_ave += rxx
    rxy = signal.correlate(x, y, 'full')
    rxy_ave += rxy

lags = signal.correlation_lags(x.shape[0], y.shape[0], 'full')
pl.plot(lags, rxx_ave/nreps)
pl.plot(lags, rxy_ave/nreps)
pl.xlabel('Lag (samples)')
n_ma = 50  ## Do 100 sample moving average
h = np.ones(n_ma) / (n_ma ** 0.5)

nreps = 50
rxx_ave = 0
rxy_ave = 0
for k in range(nreps):
    x = signal.lfilter(h, 1, np.random.randn(N))
    y = signal.lfilter(h, 1, np.random.randn(N))
    rxx = signal.correlate(x, x, 'full')
    rxx_ave += rxx
    rxy = signal.correlate(x, y, 'full')
    rxy_ave += rxy
lags = signal.correlation_lags(x.shape[0], y.shape[0], 'full')
pl.plot(lags, rxx_ave/nreps)
pl.plot(lags, rxy_ave/nreps)
pl.xlabel('Lag (samples)')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13722067039106145, 'fraction_numerical': 0.01920391061452514, 'mean_word_length': 4.7645875251509056, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '203110', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1112, 'n_words': 281, 'path': 'BME511/RandomProcesses.ipynb', 'repo_name': 'Eric-HYQ/notebooks', 'size': '2864'}