3
votes

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.

1
A sinus signal is that feeling you get before you have to sneeze - wim
In English, we call this function sine. - Karl Knechtel
Also in English the term sinusoidal is used to describe the general shape of a sine curve. - Mark Ransom
Aaaaaactually, the term "sinus signal" is common enough. In fact, I get twice as many results on Google for "sinus signal" than "sinusoidal signal". So chillax everyone! - Nicu Stiurca

1 Answers

2
votes

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 convolve function. Since you passed it the parameter mode="same" it chops off the part of the convolution that equals zero and leaves you with the "interesting" part. You have constructed the rect() 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:

Convolution

From this 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((y1.shape))
for i in range(15,25):
    y2[i] = 0.9
#print len(y1),len(y2)
#y3 = convolve(y2,y1,mode="same")
y3 = convolve(y2,y1)
subplot(2,2,1)
plot(x,y1)
hold(True)
plot(x,y2)
hold(True)
subplot(2,2,2)
print len(x),len(y3)
xx = r_[0:len(y3)]
print len(xx),len(y3)
plot(xx,y3)
hold(True)
show()

Which is what I would expect given the documentation for this function. Let me know if there is something else I missed!