Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96174
License: OTHER
Kernel: Python 3

ThinkDSP

This notebook contains an example from Chapter 10: Signals and Systems

Copyright 2015 Allen Downey

License: Creative Commons Attribution 4.0 International

from __future__ import print_function, division %matplotlib inline import thinkdsp import thinkplot

This file contains one of the coolest examples in Think DSP. It uses LTI system theory to characterize the acoustics of a recording space and simulate the effect this space would have on the sound of a violin performance.

I'll start with a recording of a gunshot:

response = thinkdsp.read_wave('180960__kleeb__gunshot.wav') start = 0.12 response = response.segment(start=start) response.shift(-start) response.plot() thinkplot.config(xlabel='time (s)', ylabel='amplitude', ylim=[-1.05, 1.05], legend=False)

If you play this recording, you can hear the initial shot and several seconds of echos.

response.make_audio()

This wave records the "impulse response" of the room where the gun was fired.

Now let's load a recording of a violin performance:

wave = thinkdsp.read_wave('92002__jcveliz__violin-origional.wav') wave.truncate(len(response)) wave.normalize() wave.plot() thinkplot.config(xlabel='time (s)', ylabel='amplitude', ylim=[-1.05, 1.05])

And listen to it:

wave.make_audio()

Now we can figure out what the violin would sound like if it was played in the room where the gun was fired. All we have to do is convolve the two waves:

output = wave.convolve(response) output.normalize()

Here's what it looks like:

wave.plot(label='original') output.plot(label='convolved') thinkplot.config(xlabel='time (s)', ylabel='amplitude', ylim=[-1.05, 1.05])

And here's what it sounds like:

output.make_audio()

If you think this example is black magic, you are not alone. But there is a good reason why this works, and I do my best to explain it in Chapter 10. So stay tuned.

I'd like to thanks jcveliz and kleeb for making these recordings available from freesound.org.