Skip to content

Instantly share code, notes, and snippets.

@leonidk
Created October 13, 2016 19:03
Show Gist options
  • Save leonidk/f2bed4d7de638f4f4baa7dde86268492 to your computer and use it in GitHub Desktop.
Save leonidk/f2bed4d7de638f4f4baa7dde86268492 to your computer and use it in GitHub Desktop.
Convolution vs Correlation Example
#simple correlation vs convolution example
import numpy as np
from matplotlib.pyplot import *
style.use('seaborn-ticks')
for a in [[1,2,3],[1,2,1]]:
figure()
x=[-1,0,1]
b = [0,0,1]
avb = np.convolve(a,b,mode='full')
bva = np.convolve(b,a,mode='full')
arb = np.correlate(a,b,mode='full')
bra = np.correlate(b,a,mode='full')
names = ['signal a','signal b','a CONV b', 'b CONV a', 'a CORR b', 'b CORR a']
for idx, value in enumerate([a,b,avb,bva,arb,bra]):
subplot(3,2,idx+1)
if idx > 1:
x =[-2,-1,0,1,2]
vlines(x,[0]*5,value,lw=5)
xlim(-3,3)
ylim(0,4)
title(names[idx])
tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment