Skip to content

Instantly share code, notes, and snippets.

@amarvutha
amarvutha / inverse_transform_probability_distribution.py
Created August 21, 2018 01:05
Fast Python implementation of Inverse Transform Sampling for an arbitrary probability distribution
import numpy as np
from numpy.random import random
from scipy import interpolate
import matplotlib.pyplot as plt
import cProfile
def f(x):
# does not need to be normalized
return np.exp(-x**2) * np.cos(3*x)**2 * (x-1)**4/np.cosh(1*x)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# Monitor and plot
from __future__ import division, print_function
import numpy as np
from numpy import sin, cos, random
from scipy.constants import c,e,h,hbar,u,pi
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
@amarvutha
amarvutha / sine_fitting - known frequency.py
Last active August 29, 2015 14:04
Extraction of a sine wave's parameters (especially phase) with a known frequency. A demonstration of linear least squares.
# Least squares fitting
# IEEE algorithm version for known frequency
from __future__ import division
import numpy as np
from numpy import pi
from numpy import fft
from numpy import linalg as lg
import pylab as plt
import matplotlib.gridspec as gridspec
// Arduino Due simple synthesizer
// arbitrary waveforms on DAC0
// using Timer library to run off internal clock
#include <DueTimer.h>
int led = 13;
int sensorPin = A0;
int triggerPin = 4;
int outputValue = 0;
@amarvutha
amarvutha / characteristicImpedanceTEM.py
Last active August 29, 2015 14:01
Characteristic impedance & field pattern in arbitrary transmission lines
# Transmission line characteristic impedance
# Amar, 12 July 2012
# version 3
# All lengths in cm, E-fields in V/cm, time in ns
import pylab as plt
import numpy as np
from numpy import pi
from scipy.weave import blitz
import cPickle, time
@amarvutha
amarvutha / beamCamera.py
Last active August 29, 2015 14:00
Acquiring a webcam image and slicing it, for an atomic beam camera
# Display webcam image from beamline monitor
# version 3, 2014-04-25
# Amar
import numpy as np
import cv2
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import matplotlib.cm as cm
@amarvutha
amarvutha / oscillatorAnimation.py
Last active August 29, 2015 13:59
Oscillator animation
# Animation of harmonic oscillator, with Verlet integration
from __future__ import division
import numpy as np
from numpy import sin, cos
from scipy.constants import c,e,h,hbar,u,pi
import matplotlib.animation as animation
delta = 1e-2 # spatial discretization
x = np.arange(-2,2,delta)
@amarvutha
amarvutha / fourier-ellipse.py
Created April 3, 2014 18:51
Animated reconstruction of a random ellipse from fourier coefficients
# Fourier reconstruction of random ellipse
# Amar Vutha
# 2014-04-03
import pylab as plt
import numpy as np
from numpy import pi
import matplotlib.pyplot as plt
import matplotlib.animation as animation
@amarvutha
amarvutha / rigolScope-spectrumAnalyzer.py
Last active August 29, 2015 13:56
Spectrum analyzer with Rigol scope
# Get waveforms from Rigol scope and do spectrum analysis, fast version
# Amar Vutha
# version 1, 2013-12-30
from __future__ import division
import pylab as plt
import numpy as np
from scipy import signal
from scipy.constants import e
import os