Skip to content

Instantly share code, notes, and snippets.

View WarrenWeckesser's full-sized avatar

Warren Weckesser WarrenWeckesser

View GitHub Profile
@WarrenWeckesser
WarrenWeckesser / animandala.py
Created September 23, 2019 01:57
Create an animated "mandala" using scipy.spatial.voronoi_plot_2d.
import numpy as np
from scipy import spatial
import matplotlib.pyplot as plt
from matplotlib import animation
from numpngw import AnimatedPNGWriter
def mandala(n_iter, n_points, radius, ax=None):
@WarrenWeckesser
WarrenWeckesser / compute_pvalues_for_assorted_samples.py
Last active June 13, 2019 00:23
Test utilities for numpy's hypergeometric distribution.
#
# Test utilities for numpy's hypergeometric distribution.
#
# Author: Warren Weckesser
# License: BSD 2 clause
#
import mpmath
import numpy as np
from numpy.random import Generator, Xoshiro256
@WarrenWeckesser
WarrenWeckesser / select.py
Last active December 12, 2018 02:26
An alternative to numpy.random.choice
import numpy as np
def random_select(items, nsample=None, p=None, size=None):
"""
Select random samples from `items`.
The function randomly selects `nsample` items from `items` without
replacement.
@WarrenWeckesser
WarrenWeckesser / xpearsonr.py
Last active November 2, 2018 17:36
Scratch work for rewriting how scipy.stats.pearsonr handles several edge cases.
# Scratch work for rewriting how scipy.stats.pearsonr handles several
# edge cases.
import warnings
import numpy as np
from numpy.testing import assert_equal, assert_allclose, assert_warns
from scipy import special
from scipy import linalg
@WarrenWeckesser
WarrenWeckesser / mpsig.py
Created September 5, 2017 00:31
Some signal functions using mpmath.
"""
Some signal functions implemented using mpmath.
"""
from __future__ import division
try:
import mpmath
except ImportError:
mpmath = None
@WarrenWeckesser
WarrenWeckesser / check_freqz.py
Created September 5, 2017 00:30
Checking the proposed new freqz function.
from __future__ import division
import numpy as np
from scipy.signal import freqz, butter, cheby1, firwin
import mpmath
import mpsig
import matplotlib.pyplot as plt
# oldfreqz is copied from freqz in the master branch of scipy.
# File: animated_gif_writer.py
# Author: Warren Weckesser
# License: BSD 2-Clause (http://opensource.org/licenses/BSD-2-Clause)
#
# Copyright (c) 2015, Warren Weckesser
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
from __future__ import print_function, division
import time
import numpy as np
from numpy import convolve as np_convolve
from scipy.signal import convolve as sig_convolve, fftconvolve, firwin
from scipy.ndimage import convolve1d
from pylab import grid, show, legend, loglog, xlabel, ylabel, figure
@WarrenWeckesser
WarrenWeckesser / remez_examples.py
Last active April 20, 2021 15:58
Examples of designing a FIR filter with scipy.signal.remez.
from __future__ import division, print_function
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
def plot_response(fs, w, h, title):
plt.figure()
plt.plot(0.5*fs*w/np.pi, 20*np.log10(np.abs(h)))
@WarrenWeckesser
WarrenWeckesser / pz_match.py
Created November 21, 2014 05:05
Pole-zero matching scratch work
import numpy as np
from scipy.signal.filter_design import _cplxreal
def pop_real(p):
"""Pop the first real value in the list p."""
k = 0
while k < len(p) and p[k].imag != 0:
k += 1
if k == len(p):