Convolution of rectangular function with sinc function

I'm trying to create a rectangular function, defined between -0.5 to 0.5

I'm then convoluting it with a sinc function (using numpy.sinc) and plotting the convoluted signal alongside the original signal.

My objective is to find error between two signals and I'm expecting the error to go down if I increase the samples taken for rectangular and sinc function.

Now, since convoluted signal would be of different shape than the original function, I've constructed another rect function of same shape as convoluted signal and I'm using numpy.trapz to find squared error between the two.

Here's what I got so far

import matplotlib.pyplot as plt
import numpy as np
from scipy import signal

fs=1000

# create the rect function
t = np.linspace(-5, 5, fs)
x = np.zeros(len(t))
x[abs(t)<0.5]=1
plt.plot(t,x)
plt.xlabel('t',size=15)
plt.ylabel('rect(t)',size=15)
plt.show()

# create the sinc function
sinc = np.sinc(t)
plt.plot(t, sinc)
plt.xlim(-5,5)
plt.xlabel('t')
plt.ylabel('sinc(fs)t')
plt.show()

# convolution with sinc
dx = t[1] - t[0]
convolved_signal = np.convolve(sinc, x) * dx
t_convolved = np.linspace(-5, 5, len(convolved_signal))
plt.plot(t_convolved, convolved_signal, 'r', label='convolved signal')
plt.xlabel('t')
plt.ylabel('xcap(t)')
plt.xlim(-5,5)

# recreate rect function according to the convolved output
x_sam = np.zeros(len(t_convolved))
x_sam[abs(t_convolved)<0.5]=1
plt.plot(t_convolved, x_sam, 'g', label='original signal')
plt.legend(loc='best')
plt.xlabel('t')
plt.ylabel('x(t)')
plt.show()

# squared sum for error
error = np.trapz((convolved_signal-x_sam)**2)
print("Error is="+ str(error))


Like I mentioned, I expect the error to go down as I increase the samples (fs), however, error increases with samples in the same proportion.

Am I doing something wrong?

• Can you clarify what is the "error between two signals"? – MBaz Sep 6 '20 at 18:48