-
Notifications
You must be signed in to change notification settings - Fork 3
/
sim.py
144 lines (136 loc) · 5.09 KB
/
sim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
OEvent: Oscillation event detection and feature analysis.
sim.py - runs signal simulation
References: Taxonomy of neural oscillation events in primate auditory cortex
https://doi.org/10.1101/2020.04.16.045021
"""
import numpy as np
from pylab import *
import scipy.signal as sps
from filter import bandstop
ion()
#
class PoissProc ():
# Poisson process event generation
def __init__ (self, seed, rate):
self.seed = seed
self.rate = rate
self.prng = np.random.RandomState(self.seed) # sets seeds for random num generator
# based on cdf for exp wait time distribution from unif [0, 1)
# returns in ms based on rate in Hz
def twait (self, rate):
return -1000. * np.log(1. - self.prng.rand()) / rate
# new external pois designation
def eventtimes (self, start, stop, gaptime=0.0):
leventt = []
if self.rate > 0.:
t_gen = start - self.rate * 2 # start before start to remove artifact of lower event rate at start of period
while t_gen < stop:
t_gen += self.twait(self.rate) + gaptime # move forward by the wait time plus any gap time
if t_gen >= start and t_gen < stop: # make sure event time is within the specified interval
# vals are guaranteed to be monotonically increasing, no need to sort
leventt.append(t_gen)
return np.array(leventt)
# get a triangle
def gettriang (sampr, tdur):
t = np.linspace(0,tdur,int(sampr*tdur/1e3))
y = list((sps.sawtooth(2 * np.pi * t / tdur, width=1)+1)/2)
y.reverse()
return t,np.array(y)
#
def placetriang (sampr, sigdur, triangdur, eventt, seed=0, noiseamp=0.0, usevoss=False):
sz = int(sampr*sigdur/1e3)
if usevoss: # pink noise
sig = voss(sz,amp=noiseamp,seed=seed)
else: # white noise
np.random.seed(seed)
sig = noiseamp*np.random.randn(sz)
ttriang, ytriang = gettriang(sampr, triangdur)
sztriang = int(sampr*triangdur/1e3)
for t in eventt:
tdx = int(sampr*t/1e3)
print(sig[tdx:tdx+sztriang].shape, tdx,tdx+sztriang, sztriang,ytriang.shape)
sig0 = sig[tdx:tdx+sztriang]
sig[tdx:tdx+sztriang] += ytriang[0:len(sig0)]
return np.linspace(0,sigdur,sz),sig
#
def voss (outsz, amp=1.0, nsrc=16, seed=0):
"""Generates pink noise using the Voss-McCartney algorithm.
outsz: number of values to generate
nsrc: number of random sources to add
returns: NumPy array
"""
import pandas as pd
np.random.seed(seed)
array = np.empty((outsz, nsrc))
array.fill(np.nan)
array[0, :] = np.random.random(nsrc)
array[:, 0] = np.random.random(outsz)
# the total number of changes is outsz
n = outsz
cols = np.random.geometric(0.5, n)
cols[cols >= nsrc] = 0
rows = np.random.randint(outsz, size=n)
array[rows, cols] = np.random.random(n)
df = pd.DataFrame(array)
df.fillna(method='ffill', axis=0, inplace=True)
total = df.sum(axis=1)
out = total.values
out = out - mean(out) # min(out)
mx = std(out) # max(out)
if mx == 0.0: mx=1.0
out = out / mx
out = amp * out
return out
#
def normarr (a, stdamp = None):
# normalize array a to have 0 mean and unit standard deviation
# if stdamp is not None multiply output by stdamp (sets std to that value)
sd = np.std(a)
out = (a - np.mean(a)) / sd
if stdamp is not None: out *= stdamp
return out
#
def makeburstysig (sampr, sigdur, burstfreq, burstdur, burstamp=1, noiseamp=1, seed=0, eventt=[1000,4000], smooth=True, raiseamp=0.25,\
usevoss=False,usegauss=False,bgsig=None,bandstoprng=None):
# make a signal with superimposed oscillation events/bursts
if bgsig is not None:
if bandstoprng is not None:
sig = bandstop(bgsig, bandstoprng[0], bandstoprng[1], sampr, zerophase=True)
sig = normarr(sig, stdamp = noiseamp)
else:
sig = normarr(np.array(bgsig), stdamp = noiseamp)
else:
if usevoss: # pink noise
sz = int(sampr*sigdur/1e3)
sig = voss(sz,amp=noiseamp,seed=seed)
else: # white noise
sz = int(sampr*sigdur/1e3)
np.random.seed(seed)
sig = noiseamp*np.random.randn(sz)
if bandstoprng is not None: sig = bandstop(sig, bandstoprng[0], bandstoprng[1], sampr, zerophase=True)
# Parameters for simulated signal
burstsamp = int(burstdur*sampr) # number of samples
# Design burst kernel
burstkernelt = np.linspace(0,burstdur,burstsamp)
burstkernel = burstamp * np.sin(burstkernelt*2*np.pi*burstfreq)
if smooth:
if usegauss:
wg = sps.gaussian(len(burstkernel),std=int(len(burstkernel)*0.3)) + raiseamp # raise to avoid zeroing out first/last cycles
burstkernel = np.multiply(burstkernel,wg)
else:
wb = blackman(len(burstkernel)) + raiseamp # raise to avoid zeroing out first/last cycles
burstkernel = np.multiply(burstkernel,wb)
print(burstkernel.shape,burstsamp)
# Generate random signal with bursts
times = np.linspace(0,sigdur,len(sig))
for t in eventt:
idx = int(sampr * t / 1e3)
sig[idx:idx+burstsamp] += burstkernel
return times, sig
#
def rmse (a1, a2):
# return root mean squared error between a1, a2; assumes same lengths, sampling rates
len1,len2 = len(a1),len(a2)
sz = min(len1,len2)
return np.sqrt(((a1[0:sz] - a2[0:sz]) ** 2).mean())