Skip to content

Instantly share code, notes, and snippets.

@rmcgibbo
Created October 18, 2012 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmcgibbo/3915184 to your computer and use it in GitHub Desktop.
Save rmcgibbo/3915184 to your computer and use it in GitHub Desktop.
Rectify a 1d binary signal with an HMM
import numpy as np
from sklearn.hmm import MultinomialHMM
import matplotlib.pyplot as pp
# sample run lengths from exponential distribution
run_lengths = np.array(np.random.exponential(100, size=10), dtype=np.int)
# make the signal from the run lengths
signal = []
for i, l in enumerate(run_lengths):
signal.extend([i % 2] * l)
signal = np.array(signal)
# flip a one tenth of the bits randomly
fraction_to_flip = 0.1
r = np.random.randint(len(signal), size=fraction_to_flip*len(signal))
fuzzy_signal = np.array(signal, copy=True)
fuzzy_signal[r] = np.logical_not(signal[r])
# parameterize a hidden markov model
metastability = 0.9
pcorrectemission = 0.9
transmat = np.array([[metastability, 1-metastability], [1-metastability, metastability]])
emission = np.array([[pcorrectemission, 1-pcorrectemission], [1-pcorrectemission, pcorrectemission]])
hmm = MultinomialHMM(n_components=2, startprob=[0.5, 0.5], transmat=transmat)
hmm.emissionprob_ = emission
# find the most likely sequence to have generated the signal
corrected = hmm.predict(fuzzy_signal)
# plot the original signal, the fuzzy signal and the rectified signal
pp.subplot(311)
pp.plot(signal, label='signal')
pp.xlabel('original')
pp.subplot(312)
pp.plot(fuzzy_signal, label='fuzzy')
pp.xlabel('fuzzy')
pp.subplot(313)
pp.plot(corrected, label='corrected')
pp.xlabel('corrected')
pp.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment