Skip to content

Commit 94b5f97

Browse files
committed
Updatign aliasing and noise
1 parent 85e2027 commit 94b5f97

3 files changed

Lines changed: 27 additions & 22 deletions

File tree

code/aliasing.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def triangle_example(freq):
3737
thinkplot.subplot(2)
3838
spectrum.plot()
3939
thinkplot.config(ylim=[0, 500],
40-
xlabel='Frequency (Hz)',
41-
ylabel='Amplitude')
40+
xlabel='Frequency (Hz)')
4241

4342
thinkplot.save(root='triangle-%d-2' % freq)
4443

@@ -70,27 +69,26 @@ def aliasing_example(offset=0.000003):
7069
"""Makes a figure showing the effect of aliasing.
7170
"""
7271
framerate = 10000
73-
thinkplot.preplot(num=2)
7472

75-
freq1 = 4500
76-
signal = thinkdsp.CosSignal(freq1)
77-
duration = signal.period*5
78-
segment = signal.make_wave(duration, framerate=framerate)
79-
thinkplot.Hlines(0, 0, duration, color='gray')
73+
def plot_segment(freq):
74+
signal = thinkdsp.CosSignal(freq)
75+
duration = signal.period*4
76+
thinkplot.Hlines(0, 0, duration, color='gray')
77+
segment = signal.make_wave(duration, framerate=framerate*10)
78+
segment.plot(linewidth=0.5, color='gray')
79+
segment = signal.make_wave(duration, framerate=framerate)
80+
segment.plot_vlines(label=freq, linewidth=4)
8081

81-
segment.shift(-offset)
82-
segment.plot_vlines(label=freq1, linewidth=3)
82+
thinkplot.preplot(rows=2)
83+
plot_segment(4500)
84+
thinkplot.config(axis=[-0.00002, 0.0007, -1.05, 1.05])
8385

84-
freq2 = 5500
85-
signal = thinkdsp.CosSignal(freq2)
86-
segment = signal.make_wave(duration, framerate=framerate)
87-
segment.shift(+offset)
88-
segment.plot_vlines(label=freq2, linewidth=3)
86+
thinkplot.subplot(2)
87+
plot_segment(5500)
88+
thinkplot.config(axis=[-0.00002, 0.0007, -1.05, 1.05])
8989

9090
thinkplot.save(root='aliasing1',
91-
xlabel='Time (s)',
92-
axis=[-0.00002, duration, -1.05, 1.05]
93-
)
91+
xlabel='Time (s)')
9492

9593

9694
def main():

code/noise.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,21 @@ def process_noise(signal, root='white'):
5151

5252
# 3: log-log power spectrum
5353
spectrum.hs[0] = 0
54+
thinkplot.preplot(cols=2)
5455
spectrum.plot_power(linewidth=1, alpha=0.5)
55-
thinkplot.save(root=root+'noise3',
56-
xlabel='Frequency (Hz)',
57-
ylabel='Power',
56+
thinkplot.config(xlabel='Frequency (Hz)',
57+
ylabel='Power',
58+
xlim=[0, framerate/2])
59+
60+
thinkplot.subplot(2)
61+
spectrum.plot_power(linewidth=1, alpha=0.5)
62+
thinkplot.config(xlabel='Frequency (Hz)',
5863
xscale='log',
5964
yscale='log',
6065
xlim=[0, framerate/2])
6166

67+
thinkplot.save(root=root+'noise3')
68+
6269

6370
def plot_gaussian_noise():
6471
"""Shows the distribution of the spectrum of Gaussian noise.

code/thinkplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class _Brewer(object):
4646
# lists that indicate which colors to use depending on how many are used
4747
which_colors = [[],
4848
[1],
49-
[1, 3],
49+
[0, 4],
5050
[0, 2, 4],
5151
[0, 2, 4, 6],
5252
[0, 2, 3, 5, 6],

0 commit comments

Comments
 (0)