Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 7335
1
"""This file contains code used in "Think DSP",
2
by Allen B. Downey, available from greenteapress.com
3
4
Copyright 2014 Allen B. Downey
5
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
6
"""
7
8
from __future__ import print_function, division
9
10
import thinkdsp
11
import thinkplot
12
13
FORMATS = ['pdf', 'eps']
14
15
16
def plot_tuning(start=7.0, duration=0.006835):
17
"""Plots three cycles of a bell playing A4.
18
19
start: start time in seconds
20
duration: float
21
"""
22
period = duration/3
23
freq = 1/period
24
print(period, freq)
25
assert abs(freq - 438.917337235) < 1e-7
26
27
wave = thinkdsp.read_wave('18871__zippi1__sound-bell-440hz.wav')
28
29
segment = wave.segment(start, duration)
30
segment.normalize()
31
32
thinkplot.preplot(1)
33
segment.plot()
34
thinkplot.Save(root='sounds1',
35
xlabel='Time (s)',
36
axis=[start, start+duration, -1.05, 1.05],
37
formats=FORMATS,
38
legend=False)
39
40
41
def plot_violin(start=1.30245, duration=0.00683):
42
"""Plots three cycles of a violin playing A4.
43
44
duration: float
45
"""
46
period = duration/3
47
freq = 1/period
48
print(period, freq)
49
assert abs(freq - 439.238653001) < 1e-7
50
51
wave = thinkdsp.read_wave('92002__jcveliz__violin-origional.wav')
52
53
segment = wave.segment(start, duration)
54
segment.normalize()
55
56
thinkplot.preplot(1)
57
segment.plot()
58
thinkplot.Save(root='sounds2',
59
xlabel='Time (s)',
60
axis=[start, start+duration, -1.05, 1.05],
61
formats=FORMATS,
62
legend=False)
63
64
65
def segment_violin(start=1.2, duration=0.6):
66
"""Load a violin recording and plot its spectrum.
67
68
start: start time of the segment in seconds
69
duration: in seconds
70
"""
71
wave = thinkdsp.read_wave('92002__jcveliz__violin-origional.wav')
72
73
# extract a segment
74
segment = wave.segment(start, duration)
75
segment.normalize()
76
77
# plot the spectrum
78
spectrum = segment.make_spectrum()
79
80
thinkplot.preplot(1)
81
spectrum.plot(high=10000)
82
thinkplot.Save(root='sounds3',
83
xlabel='Frequency (Hz)',
84
ylabel='Amplitude',
85
formats=FORMATS,
86
legend=False)
87
88
# print the top 5 peaks
89
peaks = spectrum.peaks()
90
for amp, freq in peaks[:10]:
91
print(freq, amp)
92
assert abs(peaks[0][0] - 3762.382899) < 1e-7
93
94
95
def mix_cosines():
96
"""Plots three periods of a mix of cosines.
97
"""
98
99
# create a SumSignal
100
cos_sig = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0)
101
sin_sig = thinkdsp.SinSignal(freq=880, amp=0.5, offset=0)
102
103
mix = sin_sig + cos_sig
104
105
# create a wave
106
wave = mix.make_wave(duration=1.0, start=0, framerate=11025)
107
print('Number of samples', len(wave))
108
print('Timestep in ms', 1000 / wave.framerate)
109
assert len(wave) == wave.framerate
110
111
# select a segment
112
period = mix.period
113
segment = wave.segment(start=0, duration=period*3)
114
115
# plot the segment
116
thinkplot.preplot(1)
117
segment.plot()
118
thinkplot.Save(root='sounds4',
119
xlabel='Time (s)',
120
axis=[0, period*3, -1.55, 1.55],
121
formats=FORMATS,
122
legend=False)
123
124
125
def main():
126
plot_tuning()
127
plot_violin()
128
segment_violin()
129
mix_cosines()
130
131
132
if __name__ == '__main__':
133
main()
134
135