-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsim_avg.py
43 lines (29 loc) · 1.01 KB
/
sim_avg.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
#!/usr/bin/env python
# Simulate some large and varying chromatic background with a narrow-band signal
# Use a moving median to remove it
import matplotlib.pyplot as plt
import numpy as np
nchans = 100
x = np.linspace(0, nchans, nchans)
# Large background: a couple of different sin waves with different wavelengths
sin1 = np.random.rand()*np.sin(x/20.)
sin2 = np.random.rand()*np.sin((x-5)/10.)
sin3 = np.random.rand()*np.sin((x+3)/5.)
sin4 = np.random.rand()*np.sin((x-10)/30.)
# Add some noise
noise = np.random.rand(nchans)
bkg = sin1+sin2+sin3+sin4+noise
# Add a large signal
bkg[int(nchans*np.random.rand())] += 5.
# Calculate the moving average
binwidth = 20
med = np.zeros(nchans)
for i in range(binwidth/2, nchans - binwidth/2):
med[i] = np.median(bkg[i-binwidth/2:i+binwidth/2])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, bkg, label="signal + background")
ax.plot(x, med, label="moving median")
ax.plot(x, bkg-med, label="background-subtracted signal")
ax.legend()
fig.savefig("test.png")