How To Break Sound

@stephencwelch

Originally featured at Welch Labs.

In [1]:
from scikits.audiolab import wavwrite
In [2]:
#Sampling Frequency:
Fs = 48000

#Create 1 second long vector of zeros:
pulse = np.zeros(Fs)

#Starting and ending points of pulse:
start = 12000
end = 24000

#Create pulse with amplitude of 0.9:
pulse[start:end] = 0.9*np.ones(end-start)
In [3]:
#plot pulse
plot(pulse, linewidth=2)
grid(1)
ylim([-1,1])
xlabel('Samples')
ylabel('Voltage')
Out[3]:
<matplotlib.text.Text at 0x106744850>
In [4]:
#Write pulse to wave file in sounds directory:
wavwrite(pulse, 'sounds/pulse.wav', Fs)

#Play in iPython Notebook:
from IPython.display import Audio
Audio('sounds/pulse.wav')
Out[4]:
In [5]:
from scikits.audiolab import wavread
In [6]:
#Import recorded pulse:
recordedPulse, Fs, enc = wavread('sounds/recordedPulse.wav')
In [7]:
#Create zero-padded version to line up with input pulse:
zeroPaddedSignal = np.zeros(Fs)
zeroPaddedSignal[start:start+recordedPulse.shape[0]] = \
    recordedPulse[:,0]
In [8]:
#Plot both Signals:
fig = figure(0, (12,8))
subplot(2,1,1)
plot(pulse, 'b', linewidth = 2)
grid(1)
ylim([-1, 1])
ylabel('Voltage')
xlabel('Samples')
title('Input Pulse')

subplot(2,1,2)
plot(zeroPaddedSignal, 'r', linewidth=2)
grid(1)
ylim([-1,1])
xlabel('Samples')
title('Recorded Pulse')
Out[8]:
<matplotlib.text.Text at 0x10666fa10>
In [9]:
#Plot both Signals:
fig = figure(0, (12,6))
plot(pulse, 'b', linewidth = 2)
grid(1)
ylim([-1, 1])
ylabel('Voltage')
Out[9]:
<matplotlib.text.Text at 0x10672e590>
In [10]:
#Plot both Signals:
fig = figure(0, (12,6))
plot(zeroPaddedSignal, 'r', linewidth=2)
grid(1)
ylim([-1, 1])
ylabel('Voltage')
xlabel('Samples')
Out[10]:
<matplotlib.text.Text at 0x10671bed0>