Skip to content

Instantly share code, notes, and snippets.

View ptweir's full-sized avatar

Peter Weir ptweir

View GitHub Profile
@ptweir
ptweir / loadBag.py
Created December 7, 2012 00:16
Script to load strokelitude data from a .bag file
import sys, os
import numpy as np
import roslib
roslib.load_manifest('rosbag')
import rosbag
def loadBag(inFileName):
"""
Script to import strokelitude bag file
usage: outDict = loadBag(inFileName)
@ptweir
ptweir / import_edr.m
Created October 31, 2012 03:57
Imports .edr file to MATLAB matrix
function [data, h] = import_edr(file)
% function [data, h] = import_edr(file)
%
% Imports .edr file to matrix
% for use with files output by WinEDR,
% http://spider.science.strath.ac.uk/sipbs/page.php?show=software_ses
% but created independently
%
% ---------------inputs------------------------------
% file .edr file name ( e.g. 'File_001.EDR')
@ptweir
ptweir / serialLEDControl.ino
Last active October 9, 2015 05:18
Arduino code to control the intensity of an LED
// control intensity of LED using serial communication
int incomingByte = 0; // for incoming serial data
int outVal = 0; // for signal out
int pwmPin = 6; // white wire (pin 5 and 6 have pwm frequency ~1000 Hz, faster than pin 9)
int statusLED = 13;
int delaymsec = 100;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
@ptweir
ptweir / polarization_rotator.pde
Created August 24, 2012 01:10
Arduino code to control polarization rotator (switcher) based on USB commands.
// polarization rotator experiment
int incomingByte = 1; // for incoming serial data
int pwmPin = 9; // white wire
int dirPin = 11; // green wire
int statusLED = 13;
int delaymsec = 100;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
@ptweir
ptweir / findClosestSampleTimes.py
Created June 28, 2012 00:05
Find closest dense samples to sparsely sampled data
def findClosestSampleTimes(denseSamples,sparseSamples,debug=False):
"""inds, samples = findClosestSampleTimes(denseSamples,sparseSamples)
NOTE: SHOULD CLEAN ADC SIGNAL BEFORE DOWNSAMPLING"""
inds = np.empty(sparseSamples.shape)
for sparseInd, sparseSample in enumerate(sparseSamples):
inds[sparseInd] = np.argmin(abs(denseSamples - sparseSample))
if debug:
f = pylab.figure()
ax = f.add_subplot(111)
ax.plot(denseSamples,denseSamples*0,'.b')
@ptweir
ptweir / selectRectangle.py
Created June 28, 2012 00:04
function to select a rectangular region of interest of a pylab figure
def selectRectangle(fig):
pts = fig.ginput(2)
pts = np.array(pts)
pts.sort(axis=0)
pts = np.round(pts).astype(int)
return pts
"""
#example usage:
axROI.imshow(frames.mean(2),cmap='gray',origin='lower')
@ptweir
ptweir / binPlots.py
Created June 27, 2012 23:58
functions for plotting mean or mean and sem of numpy array
def binMeanPlot(X,Y,ax=None,numBins=8,xmin=None,xmax=None):
if xmin is None:
xmin = X.min()
if xmax is None:
xmax = X.max()
if ax is None:
fig = pylab.figure()
ax = fig.add_subplot(111)
bins = np.linspace(xmin,xmax,numBins+1)
XX = np.array([np.mean((bins[binInd], bins[binInd+1])) for binInd in range(numBins)])
@ptweir
ptweir / circ.py
Created June 26, 2012 04:35
Two functions for working with circular data
import numpy as np
def circmean(alpha,axis=None):
mean_angle = np.arctan2(np.mean(np.sin(alpha),axis),np.mean(np.cos(alpha),axis))
return mean_angle
def circvar(alpha,axis=None):
if np.ma.isMaskedArray(alpha) and alpha.mask.shape!=():
N = np.sum(~alpha.mask,axis)
else: