I would like to perform the operation of convolution of sinus signal and
rectangular pulse in scipy. I convolved sinus signal with cosinus signal
and plotted that on the graph,
but I would like to know how to create array with rectangular pulse,
something similar to this matlab expression
y = rectpulse(x,nsamp)
so I can convolve them. I use this to create my
sinus and cosinus signal
x=r_[0:50] (my array)
y01=sin(2*pi*x/49)
y02=cos(2*pi*x/49)
So i tried to create a nu.zeros(50), and manually changing the zeros from
position 15-25 from 0.0. to 0.9 so it looks like rectangle but convolution
on sinus array and this ‘rectangle’ array is weird,
It is supposed to be zero when there is no intersection but i get sinus
signal in return, here is the code:
from scipy import *
from pylab import *
x = r_[0:50]
y1 = sin(2*pi*x/49)
#y2 = cos(2*pi*x/49)
y2 = np.zeros(50)
for i in range(15,25):
y2[i] = 0.9
#print len(y1),len(y2)
y3 = convolve(y2,y1,mode="same")
subplot(2,2,1)
plot(x,y1)
hold(True)
plot(x,y2)
hold(True)
subplot(2,2,2)
print len(x),len(y3)
plot(x,y3)
hold(True)
show()
I apologize in advance, i feel like this is the easiest thing but I could
not find any reference on how to create a rectangular pulse.
You’ve done very well plotting the convolution and then analyzing it to see that it is not what you expect, +1! However, I think the concern you have is related to the
convolvefunction. Since you passed it the parametermode="same"it chops off the part of the convolution that equals zero and leaves you with the “interesting” part. You have constructed therect()function just fine, although I agree with you there should be an alternate way built in to scipy somewhere. When I allow the entire convolution to be plotted, I get:From this code:
Which is what I would expect given the documentation for this function. Let me know if there is something else I missed!