This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# swampme.sh | |
# Create a 16GB swap space in current file system | |
dd if=/dev/zero of=/var/myswap bs=1M count=16384 | |
mkswap /var/myswap | |
swapon /var/myswap | |
echo "/var/myswap swap swap defaults 0 0" >> /etc/fstab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def freq_from_CEPS(sig, fs): | |
N = len(sig) | |
windowed = sig * blackmanharris(N) | |
spectrum = np.fft.rfft(windowed, fs) | |
spectrum[spectrum == 0] = EPSILON | |
log_spectrum = np.log(np.abs(spectrum)) | |
ceps = np.fft.irfft(log_spectrum) | |
start = int(fs / HIGH_FREQ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def freq_from_HPS(sig, fs): | |
N = len(sig) | |
windowed = sig * blackmanharris(N) | |
X = np.abs(rfft(windowed)) | |
hps = X | |
n_harmonic_partials = 6 | |
for h in range(2, n_harmonic_partials): | |
# downsample the spectra | |
dec = scipy.signal.decimate(X, h) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def freq_from_FFT(sig, fs): | |
# Compute Fourier transform of windowed signal | |
N = len(sig) | |
windowed = sig * blackmanharris(N) | |
X = np.abs(np.fft.rfft(windowed)) | |
# Find the peak and interpolate | |
i = np.argmax(abs(X)) # Just use this for less-accurate, naive version | |
X[X == 0] = epsilon # Circumvent division by 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def freq_from_AC(sig, fs): | |
corr = np.correlate(sig, sig, mode='full') | |
corr = corr[corr.size/2:] | |
# Find the first low point | |
d = diff(corr) | |
# first point with pos. | |
start = find(d > 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def freq_from_ZCR(sig, fs): | |
# Find all indices right before a rising-edge zero crossing | |
indices = find((sig[1:] >= 0) & (sig[:-1] < 0)) | |
crossings = interpolate(indices, sig) | |
return fs / np.mean(np.diff(crossings)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A script to remind me how to check if an | |
# array contains a zero. If it does | |
# we should mask it with some tiny number | |
from sys import float_info | |
epsilon = float_info.epsilon | |
# X is input variable, f.x. FFT | |
X[X == 0] = epsilon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KnightsTour(): | |
'''Knights Tour using Warnsdorf's rule as heuristics''' | |
def __init__(self, N, start_point=(0,0)): | |
self.N = N | |
self.initial_state = (start_point, ) | |
# all relative moves expressed as displacement in 2D | |
self._actions = [(1,2), (1,-2), (2,1), (2,-1),\ | |
(-1,2), (-1,-2), (-2,1), (-2,-1)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _viterbi(self, observations): | |
'''Generate the most likely sequence of words given the observations''' | |
transitions = self.transition_prob; emissions = self.emission_prob | |
V = {}; V[(0, X0)] = log10(1) # initial probabilities | |
path = {}; path[X0] = [X0] # path to most likely seq | |
current_words = [[] for i in range(len(observations)+1)] | |
current_words[0] = [X0] # list of words that have an entry in bigram table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'This is an example of list comprehension control variables being leaked to their outer scope' | |
for i in range(10): | |
a = [i for i in range(43)] | |
print i # will print 42 every time! |