Repository navigation
Expand file tree
/
Copy pathchirp_plot.py
More file actions
164 lines (128 loc) · 3.95 KB
/
Copy pathchirp_plot.py
File metadata and controls
164 lines (128 loc) · 3.95 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""Create plots of signals generated by chirp() and sweep_poly()."""
import numpy as np
from numpy import poly1d
from pylab import (clf, figure, grid, plot, savefig, show, subplot, title,
xlabel, ylabel, yscale)
from scipy.signal.waveforms import chirp, sweep_poly
FIG_SIZE = (7.5, 3.75)
def make_linear(f0, t1, f1, filename=None, fig_size=FIG_SIZE):
t = np.linspace(0, t1, 5001)
w = chirp(t, f0=f0, f1=f1, t1=t1, method='linear')
figure(1, figsize=fig_size)
clf()
subplot(2, 1, 1)
plot(t, w)
tstr = "Linear Chirp, $f(0)={:g}, f({:g})={:g}$".format(f0, t1, f1)
title(tstr)
subplot(2, 1, 2)
plot(t, f0 + (f1 - f0) * t / t1, 'r')
grid(True)
ylabel('Frequency (Hz)')
xlabel('time (sec)')
if filename is None:
show()
else:
savefig(filename)
def make_quadratic(f0, t1, f1, filename=None, fig_size=FIG_SIZE):
t = np.linspace(0, t1, 5001)
w = chirp(t, f0=f0, f1=f1, t1=t1, method='quadratic')
figure(1, figsize=fig_size)
clf()
subplot(2, 1, 1)
plot(t, w)
tstr = "Quadratic Chirp, f(0)=%g, f(%g)=%g" % (f0, t1, f1)
title(tstr)
subplot(2, 1, 2)
plot(t, f0 + (f1 - f0) * t**2 / t1**2, 'r')
grid(True)
ylabel('Frequency (Hz)')
xlabel('time (sec)')
if filename is None:
show()
else:
savefig(filename)
def make_quadratic_v0false(f0, t1, f1, filename=None, fig_size=FIG_SIZE):
t = np.linspace(0, t1, 5001)
w = chirp(t, f0=f0, f1=f1, t1=t1, method='quadratic', vertex_zero=False)
figure(1, figsize=fig_size)
clf()
subplot(2, 1, 1)
plot(t, w)
tstr = "Quadratic Chirp, f(0)=%g, f(%g)=%g (vertex_zero=False)" % (f0, t1,
f1)
title(tstr)
subplot(2, 1, 2)
plot(t, f1 - (f1 - f0) * (t1 - t)**2 / t1**2, 'r')
grid(True)
ylabel('Frequency (Hz)')
xlabel('time (sec)')
if filename is None:
show()
else:
savefig(filename)
def make_logarithmic(f0, t1, f1, filename=None, fig_size=FIG_SIZE):
t = np.linspace(0, t1, 5001)
w = chirp(t, f0=f0, f1=f1, t1=t1, method='logarithmic')
figure(1, figsize=fig_size)
clf()
subplot(2, 1, 1)
plot(t, w)
tstr = "Logarithmic Chirp, f(0)=%g, f(%g)=%g" % (f0, t1, f1)
title(tstr)
subplot(2, 1, 2)
plot(t, f0 * (f1 / f0)**(t / t1), 'r')
# yscale('log')
grid(True)
ylabel('Frequency (Hz)')
xlabel('time (sec)')
if filename is None:
show()
else:
savefig(filename)
def make_hyperbolic(f0, t1, f1, filename=None, fig_size=FIG_SIZE):
t = np.linspace(0, t1, 5001)
w = chirp(t, f0=f0, f1=f1, t1=t1, method='hyperbolic')
figure(1, figsize=fig_size)
clf()
subplot(2, 1, 1)
plot(t, w)
tstr = "Hyperbolic Chirp, f(0)=%g, f(%g)=%g" % (f0, t1, f1)
title(tstr)
subplot(2, 1, 2)
plot(t, f0 * f1 * t1 / ((f0 - f1) * t + f1 * t1), 'r')
grid(True)
ylabel('Frequency (Hz)')
xlabel('time (sec)')
if filename is None:
show()
else:
savefig(filename)
def make_sweep_poly(filename=None, fig_size=FIG_SIZE):
p = poly1d([0.05, -0.75, 2.5, 5.0])
t = np.linspace(0, t1, 5001)
w = sweep_poly(t, p)
figure(1, figsize=fig_size)
clf()
subplot(2, 1, 1)
plot(t, w)
tstr = "Sweep Poly, $f(t) = 0.05t^3 - 0.75t^2 + 2.5t + 5$"
title(tstr)
subplot(2, 1, 2)
plot(t, p(t), 'r')
grid(True)
ylabel('Frequency (Hz)')
xlabel('time (sec)')
if filename is None:
show()
else:
savefig(filename)
if __name__ == "__main__":
f0 = 12.5
t1 = 10.0
f1 = 2.5
make_linear(f0, t1, f1, 'chirp_linear.png')
make_quadratic(f0, t1, f1, 'chirp_quadratic.png')
make_quadratic_v0false(f0, t1, f1, 'chirp_quadratic_v0false.png')
make_hyperbolic(f0, t1, f1, 'chirp_hyperbolic.png')
make_logarithmic(f0, t1, f1, 'chirp_logarithmic.png')
make_sweep_poly(filename='sweep_poly.png')