0

I'm writing a research paper on the SIFT algorithm, and I want to create a graphic to help explain the concept of a Gaussian blur in the context of continuous functions before I describe the discrete process. The graphic I want to create is a graph of a standard Gaussian convolved with a sine function. I can plot a Gaussian, and I can plot sine, but I don't know how to plot their convolution. I don't know how to calculate their convolution to plot it, and I don't know of any software that will allow me to use a convolution operator in the plot. I am familiar with tikz and gnuplot, but I would not know how to do this with either of them. Any suggestions as to how I could go about this would be greatly appreciated. Thanks.

Luca
  • 515
  • 5
  • 17
  • 1
    For numeric computation of the convolution use programs like `octave`. Or simply plot the analytic solution in your case as function – Christoph Nov 27 '16 at 13:30
  • Gnuplot already provides the function desired called kdensity. It makes a Gauss-convolution on the data with variable bandwidth. See more on [this SO Q&A](https://stackoverflow.com/questions/44823631/smooth-line-on-histograms-in-gnuplot)! – DanielTuzes Mar 16 '18 at 02:18

1 Answers1

0

You could use python's matplotlib and np.convolve

Please see the following code

__author__ = 'kgeorge'
import os
import numpy as np
import math
import matplotlib.pyplot as plt
from matplotlib import gridspec

#create gaussian for the x values in x-axis
def create_gaussian(x_axis):
    sigma = 1.0
    denom = math.sqrt(2 * math.pi) * sigma
    twoSigmaSq = 2.0*sigma**2
    e=np.zeros_like(x_axis)
    for i,x in enumerate(x_axis):
        e[i]=math.exp (-(x*x)/twoSigmaSq)
    e = e / denom
    return e


def main():
    #x_axis
    sz=100
    halfW = int(sz/2)
    x_axis=np.linspace(-halfW, halfW, 1000)

    #cos fun
    cos_f=np.cos(x_axis)
    #gaussian
    gaussian_f=create_gaussian(x_axis)

    fig = plt.figure()
    gs = gridspec.GridSpec(3, 1)

    ax1 = fig.add_subplot(gs[0,0])
    ax1.plot(x_axis, cos_f)
    ax1.set_title('cos')

    ax2 = fig.add_subplot(gs[1,0])
    ax2.plot(x_axis, gaussian_f)
    ax2.set_title('gaussian')

    ax3 = fig.add_subplot(gs[2,0])
    convolved_ret=np.convolve(cos_f, gaussian_f, mode='same')
    ax3.plot(x_axis, convolved_ret)
    ax3.set_title('cos convolved with gaussian')

    gs.update(wspace=0.5, hspace=0.5)

    plt.show()

Please see the output here. output of convolustion of cos and gaussian

koshy george
  • 671
  • 6
  • 24