%pylab inline rcParams['figure.figsize'] = (10, 4) #wide graphs by default from __future__ import print_function from __future__ import division impulse = r_[1,zeros(15)] stem(arange(16),impulse) xlim((-0.5, 16)) ylim((-1.1, 1.1)) ir = 2*(random.random(3) -0.5) stem(arange(3),ir) xlim((-0.5, 2.5)) ylim((-1.1, 1.1)) stem(arange(18),convolve(impulse, ir)) xlim(-1,18) impulse2 = r_[zeros(5), -1, zeros(10)] stem(arange(16),impulse2) xlim((-0.5, 16)) ylim((-1.1, 1.1)) stem(arange(18),convolve(impulse2, ir)); subplot(121) added_after = convolve(impulse, ir) + convolve(impulse2, ir) stem(arange(18),added_after) added_before = convolve(impulse + impulse2, ir) subplot(122) stem(arange(18),added_before); f = [1,2,3,0,0,0] g = [-0.5, 0.5, 0.1] subplot(121) stem(arange(6),f) xlim((-0.5, 4.5)) subplot(122) stem(arange(3),g) xlim((-0.5, 4.5)) markerline, stemlines, baseline = stem(arange(5),convolve(array([1,0,0]),g)) markerline, stemlines, baseline = stem(arange(5),convolve(array([0,2,0]),g), 'g', 'og') markerline, stemlines, baseline = stem(arange(5),convolve(array([0,0 ,3]),g), 'r', 'or') markerline.set_color('r') grid() xlim((-0.5, 4.5)) ylim((-2, 2)) f = array([0.3, 0.2, -0.5, 0, -0.1, 0.1, 0.25 ]) stem(f) g = array([0.1, 0.2, -0.1]) stem(g) fg0 = f[0] * g subplot(211) stem(fg0) subplot(212) stem(g) fg1 = f[1] * g stem(fg1) fg1 = r_[0, fg1] stem(fg1) fg2 = f[2] * g fg2 = r_[0, 0, fg2] stem(fg2) def hide_axes(ax): ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["left"].set_visible(False) tick_params(axis="both", which="both", bottom="off", top="off", labelbottom="off", left="off", right="off", labelleft="on") subplot(511) stem(fg0) xlim(0,6) ylim(-0.1, 0.1) hide_axes(gca()) subplot(512) stem(fg1) xlim(0,6) ylim(-0.1, 0.1) hide_axes(gca()) subplot(513) stem(fg2) xlim(0,6) ylim(-0.1, 0.1) hide_axes(gca()) subplot(514) stem(f) xlim(0,6) subplot(515) stem(g[::-1]) xlim(0,6) gcf().set_figheight(8) def apply_ir(f , n, g): fg = f[n] * g fg = r_[zeros(n), fg, zeros(len(g) + len(f) - len(fg) - n - 1 )] return fg fg = apply_ir(f, 6, g) stem(fg) convolved = zeros(9) for i in range(len(f)): convolved += apply_ir(f, i, g) stem(convolved) stem(convolve(f,g)) f = random.random(4096) - 0.5 specgram(f); g = ones(30)/30.0 plot(f) figure() stem(g) fg = convolve(f, g) Pxx, freqs, bins, im = specgram(fg); plot(Pxx[:, 10]) from scipy.signal import freqz w, h = freqz(g) plot(w, abs(h)) twinx() plot(w, angle(h), 'r') plot(abs(fft.rfft(g)), 'o-') plot(abs(fft.rfft(g, n=512))) twinx() plot(angle(fft.rfft(g, n=512)), 'r') window = f[:256] plot(abs(rfft(window))) plot(abs(rfft(window)*rfft(g, n=256)))